Sub ConnectToSQLServer()
Dim conn As Object
Dim rs As Object
Dim strSql As String
Dim strConn As String
strConn = "Provider=SQLOLEDB;Data Source=YourServerName;" & _
"Initial Catalog=YourDatabaseName;" & _
"User ID=YourUsername;Password=YourPassword;"
strSql = "SELECT * FROM YourTableName;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConn
Set rs = CreateObject("ADODB.Recordset")
rs.Open strSql, conn
If Not rs.EOF Then
Do Until rs.EOF
Debug.Print rs.Fields(0).Value
rs.MoveNext
Loop
Else
MsgBox "No records found."
End If
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub