Sub DataExtract()
' Create a connection object.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=QCSVR02;INITIAL CATALOG=QCLIVE;"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
cn.Open strConn
' Create a recordset object.
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
' Assign the Connection object.
.ActiveConnection = cn
' Extract the required records.
.Open "SELECT * FROM [Quick Corporate$Salesperson_Purchaser]"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A2").CopyFromRecordset rs
' Tidy up
.Close
End With
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
|