Solved Recordset performance (1 Viewer)

ProgramRasta

Member
Local time
Today, 15:32
Joined
Feb 27, 2020
Messages
98
Hi All

I am attempting to import certain columns from Excel into an Access table.

However, I'm getting severe delays in updating the table using the below code.

I was wondering if my code is inefficient, Access is just slow at these type of updates or if I'm missing a trick somewhere.

For reference, there are 4000 rows in my Excel spreadsheet.

Code:
r = 2

    Do While Excel.Range("A" & r).Value <> Empty
    
            field1 = Excel.Range("A" & r).Value
            field2 = Excel.Range("B" & r).Value
            field3 = Excel.Range("C" & r).Value
            
            Set ImportRS = dbs.OpenRecordset("table1", dbOpenDynaset)
            
            With ImportRS
                .AddNew
                .Fields("Field_1") = field1
                .Fields("Field_2") = field2
                .Fields("Field_3") = field3
                .Update
            End With
    
    r = r + 1
    
    Loop

Your wisdom is appreciated!

Many thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:32
Joined
Feb 19, 2002
Messages
43,275
Four thousand rows shouldn't be a problem but action queries are almost always faster than VBA loops so Gasman's suggestion should solve the problem.

You are not doing any validation so there is no need to use VBA. Even with an append query, you might be able to do all the required validation.
 

cheekybuddha

AWF VIP
Local time
Today, 15:32
Joined
Jul 21, 2014
Messages
2,280
If you wish to use the recordset method, open the recordset outside of the excel row looping.

At the moment you open the table for each row of the worksheet.

Your code should look more like:
Code:
  r = 2

  Set ImportRS = dbs.OpenRecordset("table1", dbOpenDynaset)
  With ImportRS
    Do While Excel.Range("A" & r).Value <> Empty
      field1 = Excel.Range("A" & r).Value
      field2 = Excel.Range("B" & r).Value
      field3 = Excel.Range("C" & r).Value
      .AddNew
        .Fields("Field_1") = field1
        .Fields("Field_2") = field2
        .Fields("Field_3") = field3
      .Update
      r = r + 1
    Loop 
    .Close
  End With
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:32
Joined
Sep 21, 2011
Messages
14,306
You can also open a recordset append only, can you not?
Would that speed up things at all?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:32
Joined
May 7, 2009
Messages
19,245
looping through each Cells in Excel also make it slow.
you can, one time put the Range values to an Array and just loop through
the Array and update the recordset:
Code:
Dim last_row As Long
Dim values As Variant

'get the last non-blank row
last_row = Excel.Cells(Excel.Rows.Count, 1).End(-4162).Row
'put the range into an array
values = Excel.Range("a2:c" & last_row)

Set importrs = dbs.OpenRecordset("table1", dbOpenDynaset)

With importrs
    For r = 1 To UBound(values)
        .AddNew
        .Fields("Field_1") = values(i, 1)
        .Fields("Field_2") = values(i, 2)
        .Fields("Field_3") = values(i, 3)
        .Update
    Next
End With
 

ProgramRasta

Member
Local time
Today, 15:32
Joined
Feb 27, 2020
Messages
98
looping through each Cells in Excel also make it slow.
you can, one time put the Range values to an Array and just loop through
the Array and update the recordset:
Code:
Dim last_row As Long
Dim values As Variant

'get the last non-blank row
last_row = Excel.Cells(Excel.Rows.Count, 1).End(-4162).Row
'put the range into an array
values = Excel.Range("a2:c" & last_row)

Set importrs = dbs.OpenRecordset("table1", dbOpenDynaset)

With importrs
    For r = 1 To UBound(values)
        .AddNew
        .Fields("Field_1") = values(i, 1)
        .Fields("Field_2") = values(i, 2)
        .Fields("Field_3") = values(i, 3)
        .Update
    Next
End With
Good morning.

First of all, thank you to all who contributed, your time and knowledge was appreciated.

The above solution made the import almost instantaneous.

Have a great day.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:32
Joined
May 7, 2009
Messages
19,245
if ever you convert the Regular range in Excel to a Table, then you do not need
to know the "last row"

Dim values As Variant
values = Excel.ListObjects("Table1").DataBodyRange.Value
 

Users who are viewing this thread

Top Bottom