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!
Provider=SQLOLEDB;Data Source=" & ServerName & "; Initial Catalog=" & db & "; Integrated Security=SSPI;saor any other user?