Speeding up Access query to Excel

Vbanoob98

Board Regular
Joined
Sep 13, 2019
Messages
128
Hi guys,

I have the following code

VBA Code:
Sub test((
'This macro imports query into Excel

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim myValue As Variant
Dim strSQL As String

DBPass = InputBox("Enter Password")
Set dbs = OpenDatabase("U:\TEST1\test.accdb", False, False, ";pwd=" & DBPass & "")
'Get the parameter query
Set qdf = dbs.QueryDefs("Query_test1")


myValue = InputBox("Enter Code")

'Supply the parameter value
qdf.Parameters("Enter Code") = myValue


'Open a Recordset based on the parameter query
Set rst = qdf.OpenRecordset()

 While Not rst.EOF
         Sheets.Add.Name = "test"
         With ActiveSheet.Range("A1") 'create field headers
           lOffset = 0
           For Each fld In rst.Fields
               .Offset(0, lOffset).Value = fld.Name
               lOffset = lOffset + 1
           Next fld
         End With
         ActiveSheet.Range("A2").CopyFromRecordset rst, 150000
     Wend

MsgBox "Access query export completed."
End Sub

I'm just wondering if there is a faster way to do the same. Maybe without using a loop? Its currently taking 1min
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
copyFromRecorset, is much faster than anything else.
tho, you dont need the while Not rst.eof.
no need to loop it.
 
Upvote 0
I don't think there's anything you can do codewise to speed it up. You may investigate the query itself (depending on a number of factors) to see if it can be made more efficient (but if the query is just select * from table then it can't really be improved per se). Also location, speed of network, local vs. remote are things that effect query speed.
 
Upvote 0
Oh I see, yes why are you looping the recordset ...
Anyway, it seems if so you have a very large amount of data so that's the main thing anyway (each loop is writing 150,000 records). But of course if you can write all the records at once then go ahead and do that. Not sure how much time is consumed here just writing data vs. retrieving data ... but with a lot of data presumably both take some time.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top