Question

I am working on quering a database on a remote server and have my results in the excel spreadsheet. Say in Column A.

For that reason, I have created a button to allow for an 'at will' action and start setting up my ADODB objects.

The connection to the database is fine, however it is very unclear to me how to set up the .Recordset object (MyOutput) to output the results of my query in Column A. Here is my code:

Private Sub RunQuery_Click()

Dim MyOutput As ADODB.Recordset

Dim cnn As New ADODB.Connection                                 
Dim myCommand As ADODB.Command                                  

Dim stringSQL As String                                         
Dim stringConn As String                                        

cnn.Provider = "Microsoft.Jet.OLEDB.4.0;"
cnn.Properties("Jet OLEDB:System database") = "My path"
stringConn = "Data Source=\'my path';User Id='';Password='';"

cnn.Open stringConn                                             

Set myCommand = New ADODB.Command                               
myCommand.ActiveConnection = cnn                                

stringSQL = " My query"          
myCommand.CommandText = stringSQL 
myCommand.Execute                                                    
cnn.Close                                                       
Set cnn = Nothing
End Sub       

May I have some help here?

Thank you very much for your time guys!

Was it helpful?

Solution

You can use something like the below to do it:

Public Sub RunQuery_Click()

Dim oDB As ADODB.Connection
Dim oCM As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strConn As String

Set oDB = New ADODB.connectoin

With oDB
    .Provider = "Microsoft.Jet.OLEDB.4.0;"
    .Properties("Jet OLEDB:System database") = "My path"
    strConn = "Data Source=\'my path';User Id='';Password='';"
    .Open strConn
End With

    Set oCM = New ADODB.Command

With oCM
    .ActiveConnection = oDB
    .CommandText = "My Query"
    .CommandType = adCmdText
    Set oRS = .Execute
End With

Sheets(1).Range("A1").CopyFromRecordset oRS

oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing

End Sub

Alternativly, if you wish to return the field names as well, you could use:

Public Sub RunQuery_Click()

Dim oDB As ADODB.Connection
Dim oCM As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strConn As String
Dim iCols As Long

Set oDB = New ADODB.connectoin

With oDB
    .Provider = "Microsoft.Jet.OLEDB.4.0;"
    .Properties("Jet OLEDB:System database") = "My path"
    strConn = "Data Source=\'my path';User Id='';Password='';"
    .Open strConn
End With

    Set oCM = New ADODB.Command

With oCM
    .ActiveConnection = oDB
    .CommandText = "My Query"
    .CommandType = adCmdText
    Set oRS = .Execute
End With

For iCols = 0 To oRS.Fields.Count - 1
 Sheet(1).Cells(1, iCols + 1).Value = oRS.Fields(iCols).Name
Next

Sheets(1).Range("A2").CopyFromRecordset oRS

oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top