Message 1 of 6
Connect to SQL Database

Not applicable
12-07-2007
08:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I would like to be able to connect to an SQL database via code in VBA. I have been struggling the code to open the connection. Does anyone have experience doing this or some sample code on how to establish the connection? Here is what I have so far:
Public Sub GetData
Dim oConn As New ADODB.Connection
Dim strConn As String
strConn = "PROVIDER=SQLOLEDB;"
strConn = strConn & "Data Source=ServerName;Initial Catalog = XYZDatabase;"
strConn = strConn & " Integrated Security = SSPI;"
oConn.Open strConn
'Open recordset and manipulate data here
oConn.Close
End Sub
I get a User Type not defined error on Dim oConn As New ADODB.Connection. Thanks for any help. -Rob
Public Sub GetData
Dim oConn As New ADODB.Connection
Dim strConn As String
strConn = "PROVIDER=SQLOLEDB;"
strConn = strConn & "Data Source=ServerName;Initial Catalog = XYZDatabase;"
strConn = strConn & " Integrated Security = SSPI;"
oConn.Open strConn
'Open recordset and manipulate data here
oConn.Close
End Sub
I get a User Type not defined error on Dim oConn As New ADODB.Connection. Thanks for any help. -Rob