What I'm trying to do is use a pull information from SQL server into an excel spreadsheet using a query with parameters.
I've been able to query the database from excel but have not been able to figure out how to add parameters.
I need the parameter to be taken from a cell in the sheet.
Sub DataExtract()
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
Dim strConn As String
strConn = "PROVIDER=SQLOLEDB;"
strConn = strConn & "DATA SOURCE= MyServer ;INITIAL CATALOG = MyDatabase ;"
strConn = strConn & " INTEGRATED SECURITY=sspi;"
cnPubs.Open strConn
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
.ActiveConnection = cnPubs
.Open "SELECT * FROM [MyTable] WHERE ColA = ?"
Sheet1.Range("B12").CopyFromRecordset rsPubs
.Close
End With
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
End Sub
any help on how I could simply have the question mark in the query reference a cell in the sheet would be greatly appreciated. Thanks