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

时间:2015-05-25 15:47:20

标签: sql excel vba access-vba adodb

下午好 我有一个问题:Excel中从Access外部数据库检索数据的速度 (47秒 - >我希望达到3秒) Excel 2010 Access 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

如何定义用于Access的SQL正确字符串的参数? 这是使用参数的正确方法还是最好在RAM中上传整个数据库?

谢谢 恩里科

2 个答案:

答案 0 :(得分:0)

将数据复制到工作表时,请尽量避免更新,直到完成为止。使用:

Application.ScreenUpdating = False 

Application.ScreenUpdating = true

答案 1 :(得分:0)

已解决:以编程方式为Access中表的第一列编入索引