Cannot open database "ABC" requested by the login. The log in failed - sql

I am using the following code to connect to SQL with VBA and getting the error at the title
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim dbms As String
Dim strConn As String
' Specify the OLE DB provider.
cnPubs.Provider = "SQLNCLI"
'Set SQLOLEDB connection properties.
cnPubs.Properties("Data Source").Value = Worksheets(1).Range("B3")
cnPubs.Properties("Initial Catalog").Value = Worksheets(1).Range("B4")
' Windows NT authentication.
cnPubs.Properties("Integrated Security").Value = "SSPI"
'Now open the connection.
cnPubs.Open
I didn't have issues before while I was using SQL authentication as I could specify username and password. Now I need to use Windows authentication and I am getting an error.
I think the issue is with the autofill of username when I select Windows authentication
at SQL login. Cross referenced with SQL login page
Server type: Database engine
Servername: Server\Database
Authentication: Windows Authentication
Locked selection:
Username: NT.COMPANY.COM\username
password:
anyone know how I can log in to Server\Database?

The error you are getting can be caused by one of two things:
The database you are trying to open does not exist
The account you are using has not bee granted access to the database
Because you are saying that connecting to the database works with SQL Authentication, you are most likely running into 2. To fix this you need to grant access to that database to your windows login.

Related

Accessing Oracle SQL Developer database from VBA

New to VBA and returning to SQL after 15 years. I'm am trying to use VBA to run SQL commands to download data into excel for further manipulation.
I have SQL Developer installed on my machine and I am able to access the database using the UI. I have figured out how to connect to the database and run a query within the SQL Developer interface, but honestly, I'm not an IT guy.
Any ideas how to do this? Even basic command line commands to connect to the database and run the query would be helpful.
I found this code on another site, but I'm having trouble getting my connection string to work. My macro errors out on the cnn.Open statement and says the Provider is not installed. I think it's because PROVIDER is set up for a different SQL Database type, but I can't seem to get the connection string to work.
I know my username and password, which I have successfully used to connect in the SQL Developer UI. I'm not sure what to put for remote IP address or database. Would that be the hostname and the SID in the connection properties dialog in SQL Developer? (The hostname looks more like a url without the http than an ip address. Not sure if hostname and ip address is an interchangeable term).
Sub Download_Reports()
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
'Setup the connection string for accessing MS SQL database
'Make sure to change:
'1: PASSWORD
'2: USERNAME
'3: REMOTE_IP_ADDRESS
'4: DATABASE
ConnectionString = "Provider=ORAOLEDB.ORACLE;Password=PASSWORD;Persist Security Info=True;User ID=USERNAME;Data Source=REMOTE_IP_ADDRESS;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=DATABASE"
'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
StrQuery = "SELECT TOP 10 * FROM tbl_table"
'Performs the actual query
rst.Open StrQuery, cnn
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub
I'm needing help with my connection string. Can anyone help me with this?
I'm connecting to a database via Oracle SQL Developer. I am able to connect within the UI by providing the following items:
Connection Name - got it.
myUsername - got it.
myPassword - got it.
Connection Type = Basic, Role = default
myHostname - got it.
myPort - got it.
mySID - got it.
However, I am unable to get my connection string to work in VBA. When I run the script I get
"Run-time Error '3076'. Provider cannot be found.
It may not be properly installed" on the line beginning with cnn.open.

Unable to connect Teradata server with authentication mechanism as LDAP

It would be a great help if anyone provides a VBA connection string to connect Excel with Teradata where Authentication mechanism = LDAP.
VBA code used before configuring Authentication mechanism = LDAP is given below. But the code doesn't work when my organisation moved our credentials to support only AUTHENTICATION MECHANISM = LDAP.
Please help!
Code:
Public cn As ADODB.Connection
Public rs As ADODB.Connection
Sub test_ter()
TDCONSTR1 = "DSN &_ =<dsn_name> ; uid = <usr>; PWD = <PWD>;"
Set cn = new ADODB.Connection
cn.Open TDCONSTR1
MsgBox("Connection established")
End Sub
The following is what I use to make a connection to Teradata over ODBC using the ADODB library in VBA via LDAP:
"SessionMode=Teradata;Driver=Teradata;Authentication=LDAP;DBCName=<your database IP/Domain name>;Database=<Your default database for connection>;Uid=<username>;Pwd=<pass>"
Really you just need to specify the Authentication Mechanism via the "Authentication" setting in the connection string Authentication=LDAP

Classic ASP Error 80004005 when trying to connect to SQL Server

I am trying to connect a classic ASP webpage to SQL Server 2008, and whatever I do I keep getting this...
error '80004005'
It then proceeds to tell me the line where the error is, and that is all.
My connection code is as follows
<%
Dim Stock
Dim Stock_cmd
Dim Stock_numRows
Set Stock_cmd = Server.CreateObject ("ADODB.Command")
Stock_cmd.ActiveConnection = MM_DVerto_STRING
Stock_cmd.CommandText = "SELECT Location, Part_Number FROM dbo.Stock_Header WHERE Part_Number = ?"
Stock_cmd.Prepared = true
Stock_cmd.Parameters.Append Stock_cmd.CreateParameter("param1", 200, 1, 15, Stock__MMColParam2) ' adVarChar
Set Stock = Stock_cmd.Execute
Stock_numRows = 0
%>
Connection File
<%
' FileName="Connection_odbc_conn_dsn.htm"
' Type="ADO"
' DesigntimeType="ADO"
' HTTP="false"
' Catalog=""
' Schema=""
Dim MM_DVerto_STRING
MM_DVerto_STRING = "dsn=CMD SQL;uid=web;"
%>
Any help would be greatly accepted!
Error 80004005 is:
Data source name not found and no default driver specified
Most likely your web server is missing an ODBC connection named CMD SQL.
If not, here is the full list of possible causes from that MSDN link:
The Microsoft Windows NT 4.0 or Windows 2000 user account that is used to process the request for the ASP page does not have sufficient
permissions to read the registry key that stores the specified DSN's
configuration information.
The System ODBC DSN that is specified in the ADO connection string does not exist on the IIS server.
The ASP Application or Session variable that is initialized in the ASP application's Global.asa file (or in an ASP page that is accessed
before the page that contains the database connection code) is used to
specify the ADO connection string. This connection string variable is
not initialized when the code to open the database connection is run.
To confirm this, add a Response.Write statement before the line of
code that opens the database connection to display the connection
string that is stored in the variable.

Excel VBA - Connection to SQL database fails

I've got this code I'm trying to use to export data from Excel to an SQL Database and I'm receiving this error when I try to open the connection.
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for the user 's_accounting'
This is the code I'm trying to use(variables already Dimmed of course)
ServerName = "192.168.168.34"
DatabaseName = "Accountingnumbers"
TableName = "Projects"
UserID = "s_accounting"
Password = "password123"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
";Uid=" & UserID & ";Pwd=" & Password & ";"
Note that I've renamed some sensitive stuff in there. Also, I've set the permissions on the database using this SQL Query:
use [db]
go
create user [myDomain\Groupname] for login [myDomain\Groupname]
go
grant select on schema::dbo to [myDomain\Groupname]
go
The user I'm trying to connect with (s_accounting) is member of a domain group, which is granted access to the database using the query above.
Any ideas or suggestions? Do I have to specifically give permissions to each table as well?
You're mixing two authentication methods - you created/enabled AD (domain) user, but you're using SQL authentication to access server.
Either you need to access server with current domain user credentials (so called integrated security; cannot present correct syntax atm) OR you need to enable SQL authentication on SQL server (if not enabled already) and create user "accounting" (and other needed ones) with specific passowrd.

sql server 2008: setting up a username properly in order to connect from VBA

i set up a login (reviewer) under security for SQL SERVER
and under the specific database i set up a User (qcvalues_reviewer) that references this login
i have tried all these different ways to login into the database from vba:
'cn.Open "Provider=SQLOLEDB;Data Source=hermes;Initial Catalog=test;User ID=qcvalues_reviewer; Password=92127; Trusted_Connection=yes"
'cn.Open "Provider=SQLOLEDB;Server=hermes;Database=test;User Id=qcvalues_reviewer;Password=92127;"
'cn.Open "Provider=SQLOLEDB;Data Source=hermes;Initial Catalog=test;User Id=qcvalues_reviewer;Password=92127;"
'cn.Open "Provider=SQLOLEDB;Data Source=10.20.10.18,1433;Network Library=DBMSSOCN;Initial Catalog=test;User ID=qcvalues_reviewer;Password=92127;"
each time i get the error message:
login failed for user
can someone please help me connect!
As #GSerg points out you should log in with the LOGIN and not the USER, I read that wrong.
cn.Open "Provider=SQLOLEDB;Data Source=hermes;Initial Catalog=test;User Id=reviewer;Password=92127;"
As a stab in the dark - are you using SQL Express and if so have to enabled remote connections?
Incorrect Password
You should be using the login (reviewer), not the user name (qcvalues_reviewer).
Also try using the second and the third parameters of Connection.Open for User and Login, respectively.