Private Sub Command1_Click()
Dim adoConn As ADODB.Connection
Set adoConn = New ADODB.Connection
Dim sConn As String
sConn = "Provider=SQLOLEDB; Data Source=<your data source>;" & _
"Initial Catalog=Northwind;User ID=<user name>;Password=<strong password>;"
adoConn.ConnectionString = sConn
adoConn.CursorLocation = adUseServer
adoConn.Open
Dim sQuery As String
sQuery = "SELECT * FROM CUSTOMERS WHERE CUSTOMERID='ALFKI'"
Dim adoRec As ADODB.Record
Set adoRec = New ADODB.Record
'Get one Row of data only
On Error GoTo RecError
adoRec.Open sQuery, adoConn, adModeReadWrite, , adOpenExecuteCommand
Dim col As ADODB.Field
For Each col In adoRec.Fields
Debug.Print col.Name & ": " & col.Value
Next col
GoTo Bye
RecError:
Debug.Print Err.Number & ": " & Err.Description
If adoRec.State = adStateOpen Then
For Each col In adoRec.Fields
Debug.Print col.Name & ": " & col.Status
Next col
End If
Bye:
If adoRec.State = adStateOpen Then
adoRec.Close
End If
If adoConn.State = adStateOpen Then
adoConn.Close
End If
Set adoRec = Nothing
Set adoConn = Nothing
End Sub