Recordset Open taking a long time

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Lovely People

i have a database which is around 100kb in size

Within that i have table called Tbl_All_Data

If i run a simple query like this in Access - its quick
SELECT date_, dept, sum(total)
FROM tbl_Incoming
WHERE date_ >= #01/04/2020 AND date_ <= #30/04/2020
GROUP BY date_, dept

takes around 4-5 seconds

now if i run this same query in Excel using open recordset it takes around 4-5 minutes to run

rs.Open sqlString is the part it takes ages

the SQL string is exactly the same as I posted but put into the variable sqlString

so nothing has changed and my excel file is small and all other SQL statements etc run supee quuick - its just this from excel that takes long

is there anything you can advise i add to speed it up?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
It may help to replace:
WHERE date_ >= #01/04/2020 AND date_ <= #30/04/2020
with
WHERE date_ BETWEEN( #01/04/2020 AND #30/04/2020 )

Also, as the last argument in your rs.Open method (the options argument) use adCmdText.

I'm not real hopeful that this will solve your problem, but it may help.
 
Upvote 0
It's not immediately clear why it would take longer but I would advise making sure you are using a ready-only type of recordset. Something like a fast-forward or forward-only recordset if that is an option would be best. Also would probably need to know a little more about the connection - is it entirely local? Excel and Access on the same machine?
 
Upvote 0
Hi Xenou - here is the query in access

SELECT tblOut.Date_Ran, Nz(tblOut.Department,"Other") AS Area, Sum(tblOut.Total) AS Total_Outstanding
FROM tbl_AQs_Outstanding_Rolling AS tblOut
WHERE tblOut.Date_Ran between #01/04/2020# and #27/04/2020#
GROUP BY tblOut.Date_Ran, tblOut.Department;

this is the SQL string bit in Excel

VBA Code:
Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset

        With cn
            .Provider = "Microsoft.ACE.OLEDB.12.0"    'For *.ACCDB Databases
            .ConnectionString = myDB3
            .Open
        End With

        'On Error GoTo CloseConnection

        With rs
            .activeconnection = cn
            .Source = "SELECT tblOut.Date_Ran," & _
                      "tblOut.Department," & _
                      "Sum(tblOut.Total) AS Total_Outstanding," & _
                      "Sum([TOTAL_AHT])/Sum([Total]) AS Weighted_AHT" & _
                      " FROM tbl_AQs_Outstanding_Rolling AS tblOut" & _
                      " WHERE tblOut.Date_Ran >= #" & Format(DateValue(StartDate), "yyyy\/mm\/dd") & "# AND tblOut.Date_Ran <= #" & Format(DateValue(EndDate), "yyyy\/mm\/dd") & "#" & _
                      " GROUP BY tblOut.Date_Ran, tblOut.Department"
            .CursorType = adOpenForwardOnly
            .Open

            'On Error GoTo CloseRecordset

            Call DeleteDataAQs
            AQsSh.Range("A2").CopyFromRecordset rs
        End With
 
Upvote 0
Are you still having trouble? Where is the database? Is it entirely local? Excel and Access on the same machine?

Also you must test with a simpler query first - Select * from tblOut
 
Upvote 0
Hi Xenou - I’m still struggling with this

access and excel or on same folder

problem is the open Recordset to excel each time it’s doing that bit takes for ever

what can I do or add to speed it up the best I can?

I can be copying over 100,000 + data so potentially that could be issue
 
Upvote 0
SQL:
WHERE tblOut.Date_Ran between #01/04/2020# and #27/04/2020#
Hi, since you have the above line in your query you can also try indexing the column Date_Ran in your tblOut table. That is a general solution for problems with slow running queries that can make a large difference.
 
Upvote 0
SQL:
WHERE tblOut.Date_Ran between #01/04/2020# and #27/04/2020#
Hi, since you have the above line in your query you can also try indexing the column Date_Ran in your tblOut table. That is a general solution for problems with slow running queries that can make a large difference.
Hiya

my Date_Ran column table has Index yes duplicates in there - is that what you are on about?

I have created this same index on the date column on all my tables

On some of my Databases I have tables that do a join to another table using the name area column which has no index (would I need to add an index on that column in that table too)?
 
Upvote 0
Yes, that was the thinking.
You said you are copying over 100,000+ records. How many columns are in the table? How many records are in the table (totally)? When is the last time the access database was compacted?
 
Upvote 0
Hiya - there are 120000 records and 36 columns - data is appended each day

i compacted database yesterday
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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