Login failed for user (SQL Server) via VBA (Excel) Macro - sql-server

I'm creating a macro in an Excel spreadsheet that will connect to an instance of SQL server using SQL Server Authentication. This is a recycled function I've written previously that I know works. I suspect it's either the way I've constructed the connection string for the SQL authentication, or the way the user is configured (although I have successfully logged into the server through SSMS with the user account so I know the account works).
Public Function GetConnection(ServerLocation As String, db As String) As ADODB.Connection
Dim ServerName As String, cn As ADODB.Connection, un As String, pw As String
un = "MyUser"
pw = "MyPassword"
ServerName = "MyServer"
If cn Is Nothing Then
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={SQL Server};Server=" & ServerName & ";Database=" & db & ";UID='" & un & "';Pwd=" & pw & ";"
cn.CursorLocation = adUseClient
cn.Open
When I run the macro I get
Login Failed for user "MyUser"
at the cn.Open line.
I've checked that remote logins are allowed, and both SQL Server and Windows Authentication modes are enabled. I've also recreated the user account just in case.
Is there an issue with the connection string or am I missing something on the server?
Many thanks!

I found the solution: the single quotes around the UID are not required. I spotted these after viewing the Log and noticing the way the user name was formatted. Without the additional single quotes the connection is successful and my procedure executes successfully.
My final connection string looks like this:
cn.ConnectionString = "Driver={SQL Server};Server=" & ServerName & ";Database=" & db & ";UID=" & un & ";Pwd=" & pw & ";"

Related

Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication Access vba

I am trying to connect to SQL Server on the internet using VBA TCP/IP. First thing that I did was to attach tables using DSN Less connection:
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
Set td = CurrentDb.CreateTableDef(stLocalTableName, 0, stRemoteTableName, stConnect)
This is working perfectly fine and I can now use Access linked tables to view my table contents.
But when I try to create an ADODB connection:
Set cn = New ADODB.Connection
cn.ConnectionTimeout = 60
cn.CommandTimeout = 90
cn.Open "Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=False;Data Source=" & stServer & ";Integrated Security=SSPI;Initial Catalog=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword & ";Data Provider=SQLOLEDB.1"
With the variables parsed the above connection string looks something like this:
Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=False;Data Source=42.133.12.12,1433;Integrated Security=SSPI;Initial Catalog=MYDB;UID=MYDB_USER;PWD=MYDB_PASSWORD;Data Provider=SQLOLEDB.1
I get the error:
Login failed. The login is from an untrusted domain and cannot be used
with Integrated authentication.
I have looked at various places online but have not found any solutions.
Remove 'Integraged Security=SSPI' from your connection string. You are specifying to use integrated security in addition to a username and password in the connection.

Excel Visual Basic ADODB SQL connection string not working

I'm trying to connect to an SQL Server through Automatisation in VBA, so each time Excel starts, the SQL Statement updates the table in Excel.
Problem is, that my Connection string always throws an exception:
"Run-time error "-2147217843 (80040e4d)'; Automation error"
I have following data provided:
Servername, though it has a comma in it's Name
Database Name
I also have a Windows user (accountname & Password), that I run the Statements from, but I doubt, it is necessary to provide this information to the SQL Server.
the Connection string Looks as follows:
Public Sub OpenConnection2(pServer As String, pCatalog As String)
Dim mDataBase As New ADODB.Connection
Dim mRS As New ADODB.Recordset
Dim mCmd As New ADODB.Command
Call mDataBase.Open("Provider=SQLOLEDB;Initial Catalog=" & pCatalog & ";Data Source=" & pServer & ";")
mCmd.ActiveConnection = mDataBase
End Sub
As you, FunnyO, already stated, it is the connection string.
You probably do not have the correct driver definition.
Try something like that:
strCnn = "Provider=SQLNCLI11;Server=" & pServer & ";Database=" & pCatalog & ";Integrated Security=SSPI;"
Sure, a very simple connection string. But it should work out as it is a very common one...

Create database in SQL Server (only once) using VBA Excel

I would like to create a database in SQL server using VBA (Excel) just the first time that I will run the code. So the second time I run the code, the database will exist, and it will not create another one.
#abarisone
`Public Sub CreateDBTable()
Dim dbConnectStr As String
Dim Catalog As Object
Dim cnt As ADODB.Connection
Dim dbName As String
Dim tblName As String, ServerName As String, UserID As String, pw As String
tblName = shControl.Range("B5") 'Table Name
ServerName = "SERVICESWS15" 'Enter Server Name or IP
dbName = shControl.Range("B4") 'Enter Database Name
UserID = "" 'Leave blank for Windows Authentification
pw = "" 'Leave blank for Windows Authentification
dbConnectStr = "Provider=SQLOLEDB;Data Source=" & ServerName & ";Initial Catalog=" & dbName & ";User Id=" & UserID & ";Password=" & pw & ";"
Set Catalog = CreateObject("ADOX.Catalog")
Catalog.Create dbConnectStr
Set Catalog = Nothing
Set cnt = New ADODB.Connection
With cnt
.Open dbConnectStr
.Execute "CREATE TABLE " & tblName & " (KEY nvarchar(100) NOT NULL, " & _
"Date nvarchar(100) NOT NULL, " & _
"PRIMARY KEY (KEY));"
End With
Set cnt = Nothing
End Sub
`
There is an error in this line:
Catalog.Create dbConnectStr
Error: No such interface supported
It's not very complicated. First make sure that you are referring to the appropriate ADO library like here on the screenshot.
Then your have certain building blocks you will have to use: first make a Connection object (with a connection string), then make a Command object, and last but not least use the ExecuteNonQuery method of Command on your connection object. It does what the name says: executes an SQL command without having a RecordSet as a return object. See examples in the documentation starting from here.
I have not tried it before, but for this to happen without error, you will probably have to set your connection string to the system database called "Master" if working on MS SQL Server.
Of course, the SQL commands you will have to execute are (1) check if the database exists, (2) create db and tables if not.
Then you can create your "normal" Connection object to your database.
WARNING: to be able to create a database, your technical user defined in the VBA script must have high (system admin) privileges which is definitely a HUGE RISK even if you protect your excel. If it's not a sandbox environment, DO NOT DO IT!

Database fail to connect

I try to connect ms access database files named ShopTranfer.accdb but I got error "not a valid account name or password"
here is my connection string
Dim BD As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
con & ";Jet OLEDB:Database Password=" & pass &
";Jet OLEDB:System Database=" & dir & ";"
BD actual value is
Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=c:\users\ahsan.gulahmed\desktop\shoptransfer.accdb;Jet
OLEDB:Database Password=a;Jet OLEDB:System
Database=C:\inetpub\wwwroot\HMIS\Shop.mdw;
use this url to get example of all contents..
http://www.mybringback.com/tutorial-series/828/thenewboston-sample-projects/
this is an good example for android api demos for all things......................

Unable to connect to SQL2005 using VBScript

I have the following VBScript, which is supposed to connect to a SQL Server 2005 database. But, my connection is failing. Why?
Set dbConnection = CreateObject("ADODB.Connection")
dbConnString = "Provider=SQLOLEDB.1;Data Source=srv\test1;" & _
"Initial Catalog=tset_DB;user id ='abc';password='abc'"
'Open the connection
dbConnection.Open dbConnString
You don't need quotations around your username and password. Try this:
Set dbConnection = CreateObject("ADODB.Connection")
dbConnString = "Provider=SQLOLEDB.1;Data Source=srv\test1;" & _
"Initial Catalog=test_DB;user id =abc;password=abc"
'Open the connection
dbConnection.Open dbConnString
Also, your Initial Catalog was set to tset_DB, when I'm guessing it should be test_DB.
ConnectionStrings.com is a huge help for ensuring that your connection strings are valid.

Resources