VBA - 从电子表格的内容创建ADODB.Recordset

时间:2010-03-20 19:58:50

标签: excel vba adodb recordset

我正在研究一个查询SQL数据库的Excel应用程序。查询可能需要很长时间才能运行(20-40分钟)。如果我错过了编码,可能需要很长时间才能出错或达到断点。我可以将结果保存到工作表中,当我使用记录集时,事情会爆炸。

当我调试跳过查询数据库时(第一次之后),有没有办法将数据加载到ADODB.Recordset中?

我会使用这样的东西吗?

Query Excel worksheet in MS-Access VBA (using ADODB recordset)

3 个答案:

答案 0 :(得分:6)

我必须安装MDAC来获取msado15.dll,一旦我拥有它,我就从(在Win7 64bit上)添加了对它的引用:

C:\ Program Files(x86)\ Common Files \ System \ ado \ msado15.dll

然后我创建了一个函数,通过传入当前活动工作簿中存在的工作表名称来返回ADODB.Recordset对象。如果他们需要,这是其他任何代码,包括Test()Sub以查看它是否有效:

Public Function RecordSetFromSheet(sheetName As String)

Dim rst As New ADODB.Recordset
Dim cnx As New ADODB.Connection
Dim cmd As New ADODB.Command

    'setup the connection
    '[HDR=Yes] means the Field names are in the first row
    With cnx
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source='" & ThisWorkbook.FullName & "'; " & "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
        .Open
    End With

    'setup the command
    Set cmd.ActiveConnection = cnx
    cmd.CommandType = adCmdText
    cmd.CommandText = "SELECT * FROM [" & sheetName & "$]"
    rst.CursorLocation = adUseClient
    rst.CursorType = adOpenDynamic
    rst.LockType = adLockOptimistic

    'open the connection
    rst.Open cmd

    'disconnect the recordset
    Set rst.ActiveConnection = Nothing

    'cleanup
    If CBool(cmd.State And adStateOpen) = True Then
        Set cmd = Nothing
    End If

    If CBool(cnx.State And adStateOpen) = True Then cnx.Close
    Set cnx = Nothing

    '"return" the recordset object
    Set RecordSetFromSheet = rst

End Function

Public Sub Test()

Dim rstData As ADODB.Recordset
Set rstData = RecordSetFromSheet("Sheet1")

Sheets("Sheet2").Range("A1").CopyFromRecordset rstData

End Sub

Sheet1数据: Field1 Field2 Field3 红A 1 蓝B 2 绿C 3

应该复制到Sheet2的内容: 红A 1 蓝B 2 绿C 3

每次我想要进行更改并测试时,这样可以节省大量的时间来查询SQL ...

- 罗伯特

答案 1 :(得分:2)

最简单的方法是使用rs.Save "filename"rs.Open "filename"将客户端记录集序列化为文件。

答案 2 :(得分:0)

Recordset获取Range的另一种方法是从目标XMLDocument创建Range并使用以下命令从该文档中打开Recordset Range.Value()属性。

' Creates XML document from the target range and then opens a recordset from the XML doc.
' @ref Microsoft ActiveX Data Objects 6.1 Library
' @ref Microsoft XML, v6.0
Public Function RecordsetFromRange(ByRef target As Range) As Recordset
        ' Create XML Document from the target range.
        Dim doc As MSXML2.DOMDocument
        Set doc = New MSXML2.DOMDocument
        doc.LoadXML target.Value(xlRangeValueMSPersistXML)

        ' Open the recordset from the XML Doc.
        Set RecordsetFromRange = New ADODB.Recordset
        RecordsetFromRange.Open doc
End Function

如果要使用上面的示例,请确保同时设置对Microsoft ActiveX Data Objects 6.1 LibraryMicrosoft XML, v6.0的引用。如果需要,您也可以将此功能更改为后期绑定。

示例呼叫

' Sample of using `RecordsetFromRange`
' @author Robert Todar <robert@roberttodar.com>
Private Sub testRecordsetFromRange()
    ' Test call to get rs from Range.
    Dim rs As Recordset
    Set rs = RecordsetFromRange(Range("A1").CurrentRegion)

    ' Loop all rows in the recordset
    rs.MoveFirst
    Do While Not rs.EOF And Not rs.BOF
        ' Sample if the fields `Name` and `ID` existed in the rs.
        ' Debug.Print rs.Fields("Name"), rs.Fields("ID")

        ' Move to the next row in the recordset
        rs.MoveNext
    Loop
End Sub