6

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

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

我会用这样的东西吗?

在 MS-Access VBA 中查询 Excel 工作表(使用 ADODB 记录集)

4

3 回答 3

8

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

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 Red A 1 Blue B 2 Green C 3

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

每次我想进行更改并对其进行测试时,这为我节省了大量的查询 SQL 的时间......

——罗伯特

于 2010-03-21T22:23:09.270 回答
2

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

于 2010-03-21T22:29:10.380 回答
0

Recordset从 a获取 a 的另一种方法是从目标Range创建并使用该属性从该文档打开。XMLDocumentRangeRecordsetRange.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
于 2020-06-03T22:03:59.730 回答