我在网上找到了这段代码来查询Access并将数据输入到excel(2003)中,但它比应该的要慢得多:
Sub DataPull(SQLQuery, CellPaste)
Dim Con As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim DBlocation As String, DBName As String
Dim ContractingQuery As String
If SQLQuery = "" Then
Else
DBName = Range("DBName")
If Right(DBName, 4) <> ".mdb" Then DBName = DBName + ".mdb"
DBlocation = ActiveWorkbook.Path
If Right(DBlocation, 1) <> "\" Then DBlocation = DBlocation + "\"
Con.ConnectionString = DBlocation + DBName
Con.Provider = "Microsoft.Jet.OLEDB.4.0"
Con.Open
Set RST = Con.Execute(SQLQuery)
Range(CellPaste).CopyFromRecordset RST
Con.Close
End If
End Sub
问题是这段代码需要很长时间。如果我打开 Access 并在其中运行查询,则大约需要 1/10 的时间。有没有办法加快这个速度?或者有什么原因可能需要这么长时间?我所有的查询都是简单的选择查询,带有简单的 where 语句并且没有连接。即使是select * from [test]
查询也比它应该花费的时间要长得多。
编辑:我应该指定该行
Range(CellPaste).CopyFromRecordset RST
是一个需要很长时间的人。