【问题标题】:Create a RecordSet Object to output query results into Excel cells创建 RecordSet 对象以将查询结果输出到 Excel 单元格
【发布时间】:2014-03-26 18:08:23
【问题描述】:

我正在查询远程服务器上的数据库,并将结果保存在 Excel 电子表格中。在 A 列中说。

出于这个原因,我创建了一个按钮来允许“随意”操作并开始设置我的 ADODB 对象。

与数据库的连接很好,但是我不清楚如何设置 .Recordset 对象 (MyOutput) 以在 A 列中输出我的查询结果。这是我的代码:

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       

我可以在这里帮忙吗?

非常感谢你们的时间!

【问题讨论】:

    标签: sql vba adodb


    【解决方案1】:

    你可以使用类似下面的方法来做到这一点:

    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
    

    或者,如果您也希望返回字段名称,您可以使用:

    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
    

    【讨论】:

    • 谢谢加雷斯。第一个代码实现是通过返回运行时错误 91:对象变量或未设置块变量。指向 .ActiveConnection = oDB
    猜你喜欢
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多