I am facing issues with connecting to a SQL Server database from Excel VBA. I primarily suspect that the error is occurring because of the incorrect handling of the password containing double quotes.
Scenario :
- SQL Server Name - FOO\BAR
- Initial Catalog - ScrollBar
- User - Test_User
- Password - tejg3kl!"
The connection string I am using is:
Public Const ConnectionStringONLINE = "Provider=SQLOLEDB.1;Data Source=FOO\BAR;Initial Catalog=ScrollBar;User Id=Test_User;Password=tejg3kl!"";"
An extra double quote is included in the Password value as an escape sequence character.
Code to connect to the database -
Dim DbConn As ADODB.Connection
Set DbConn = New ADODB.Connection
DbConn.Open ConnectionStringONLINE
When the above code is executed, I receive an error -
Run-time error'-2147217843 (80040e4d)':
Login failed for user 'Test_User'.
I did an UDL test for the same credentials specified in the connection string and the connection worked fine.
Any suggestions on why the error is occurring in VBA? Do I need to modify the connection string?
Edit - Just changed the password string to include an exclamation mark before the double quotes to make it appear exactly like the real password that I am using.