Vba Excel ADODB 预加载现金数据集以更快地查询 500000 行的大型 Access 数据集

Vba Excel ADODB pre-load in cash dataset to query faster a big Access dataset 500000 rows

下午好 我有一个问题:Excel 从 Access 外部数据库检索数据的速度 (47 秒 --> 我想达到 3 秒) Excel 2010 访问 2000

打开事件代码我打开我运行这个宏:

'I define connection and dataset as public
Public conn As ADODB.Connection
Public mrs As ADODB.Recordset
Public cmd As ADODB.Command

Public DBPath As String
Public strConn As String
Public strSQL As String

Public Sub preloadDataset()

Set conn = New ADODB.Connection
Set mrs = New ADODB.Recordset

DBPath = ThisWorkbook.Path & Application.PathSeparator & "Exported.mdb"

    With conn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data source=" & DBPath
        .Open
    End With


Set cmd = New ADODB.Command
strSQL = "SELECT * FROM [Database_5000Rows] WHERE "PrNameHelp" = ? AND "ScCompHelp" LIKE ""%?%""

    Set cmd.ActiveConnection = conn
    cmd.CommandText = strSQL
    cmd.CommandType = adCmdText
    cmd.Prepared = True

        'MANUAL creation of parameters
        cmd.Parameters.Append cmd.CreateParameter("PrNameHelp", adLongVarChar, adParamInput)
cmd.Parameters.Append cmd.CreateParameter("ScCompHelp", adLongVarChar, adParamInput)

End sub

主要宏:

Public Sub ADO_retrieve_Ext_file()

cmd("PrNameHelp") = str01
cmd("ScCompHelp") = str02

cmd.Execute

Application.Calculation = xlCalculationManual
    Sheet1.Range("A1").CopyFromRecordset mrs
Application.Calculation = xlCalculationAutomatic

End sub

闭幕式中:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'**********************
    ''Close Recordset
    mrs.Close
    ''Close Connection
    conn.Close
    Set mrs = Nothing
    Set Conn = Nothing
'**********************

End sub

如何定义参数来处理 SQL 正确的 Access 字符串? 是使用参数的正确方法还是将整个数据库上传到 RAM 中更好?

谢谢 恩里科

将数据复制到 sheet 时,尽量避免在完成之前更新它。使用:

Application.ScreenUpdating = False 

Application.ScreenUpdating = true

已解决:以编程方式索引 Access

中 table 的第一列