I want to access / retrieve / create recordset from SQL Server in Excel vba.
I tried following methods but they return an error.
Code 1:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
sConnString = "Provider=sqloledb; Server=192.168.0.204; Database=REPORTdb2"
Set Conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open sConnString
Set rs = conn.Execute("select * from Table1;")
at the line conn.Open sConnString
an error occurs:
Invalid authorization specification
Code2:
sConnString = "Provider=SQLOLEDB;Data Source=192.168.0.204;" & _
"Initial Catalog=ReportDB2;" & _
"Integrated Security=SSPI;"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open sConnString
Set rs = conn.Execute("SELECT * FROM Table1;")
It throws an error
Cannot generate SSPI context
The following code requires in the VBE a reference to
Microsoft Active Data Objects 2.8 Library
or above:With the included error handling for
you should be able to easily troubleshoot the problem for connecting to the server. The above code will only return a 1 upon success (for a test run). Afterwards, you can substitute your SQL command for the one in the above example.