Copy Access database query into Excel spreadsheet

六眼飞鱼酱① 提交于 2020-01-05 02:53:11

问题


I have an Access database and an Excel workbook. What I need to do is query the database and paste the query into a worksheet.

The issue is Runtime. I have stepped throught the program and everything works, but it works extremely slow, we're talking up to 30 second run times per query, although most of this run time is coming with the CopyFromRecordset call.

The database has over 800k rows in the table I'm querying. Currently at my company there are people every morning who manually query the tables and copy and paste them into excel. I'm trying to remove this process.

Here is what I have:

Sub new1()

    Dim objAdoCon As Object
    Dim objRcdSet As Object

    ' gets query information '
    Dim DataArr()
    Sheets("Data2").Activate
    DataArr = Range("A1:B40")

    For i = 1 To UBound(DataArr)

        job = DataArr(i, 1)
        dest = DataArr(i, 2)

        If InStr(dest, "HT") > 0 Then
            OpCode = "3863"
        ElseIf InStr(dest, "HIP") > 0 Then
            OpCode = "35DM"
       End If

       strQry = "SELECT * from [BATCHNO] WHERE ([BATCHNO].[Job]='" & job & "') AND ([BATCHNO].[OperationCode] = " & "'" & OpCode & "')"

       Set objAdoCon = CreateObject("ADODB.Connection")
       Set objRcdSet = CreateObject("ADODB.Recordset")

       objAdoCon.Open "Provider = Microsoft.Jet.oledb.4.0;Data Source = C:\Users\v-adamsje\Desktop\HTmaster.mdb"
       'long run time
       objRcdSet.Open strQry, objAdoCon
       'very long run time
       ThisWorkbook.Worksheets(dest).Range("A2").CopyFromRecordset objRcdSet

      Set objAdoCon = Nothing
      Set objRcdSet = Nothing

   Next i

End Sub

Any help is appreciated. I am new to VBA and Access so this could be an easy fix. Thanks


回答1:


Excel is very good at getting data for itself, without using VBA.

On the DATA ribbon

  1. create a connection to a table or view of data somewhere (eg mdb or SServer)

  2. then use the "existing connections" button to add data from your connected table to a worksheet table (ListObject).

  3. You can even set the workbook (ie connection) to refresh the data every 12 hours.

Repeat for all the tables /view you need to grab data for. You can even specify SQL as part of the connection.

Let excel look after itself.

I just grabbed a 250,000 row table from a "nearby" disk in 2 secs.

It will look after itself and has no code to maintain!




回答2:


I don't see how the CopyFromRecordset can be improved. You could copy the recods programmatically (in VB) record-by-record but that will probably be slower than the CopyFromRecordset.

You can move the CreateObject statements out of the loop, With the connection and RecordSet already created, this could be faster:

Set objAdoCon = CreateObject("ADODB.Connection")
Set objRcdSet = CreateObject("ADODB.Recordset")

For i = 1 To UBound(DataArr)
    ...
next i
Set objRcdSet = Nothing
Set objAdoCon = Nothing

You could also try ADO instead of DAO. ADO seems to perform faster on large record sets.

But also the server could be an issue, for example, are there indexes on Job and OperationCode? If not, then the slowness could be the server selecting the records rather than Excel placing them in the worksheet.




回答3:


Whelp, never found out why the CopyFromRecordset runtime was obsurd, but solved my problem by pulling the whole table into excel then into an array, looping through that and putting them in respective sheets. From 30min runtime to <1min



来源:https://stackoverflow.com/questions/31988815/copy-access-database-query-into-excel-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!