VBA: Querying Access with Excel. Why so slow?

后端 未结 10 806
情话喂你
情话喂你 2021-01-18 03:24

I found this code online to query Access and input the data into excel (2003), but it is much slower than it should be:

Sub DataPull(SQLQuery, CellPaste)
Dim         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 04:05

    I would recommend you to create the Recordset explicitly rather than implicitly using the Execute method. When creating explicitly you can set its CursorType and LockType properties which have impact on performance.

    From what I see, you're loading data in Excel, then closing the recordset. You don't need to update, count records, etc... So my advice would be to create a Recordset with CursorType = adOpenForwardOnly & LockType = adLockReadOnly:

    ...
    RST.Open SQLQuery, Con, adOpenForwardOnly, adLockReadOnly
    Range(CellPaste).CopyFromRecordset RST
    ...
    

    Recordset Object (ADO)

提交回复
热议问题