Access Database - vba Adding Records is painfully slow

Posted on 2012-09-10
Medium Priority
Last Modified: 2012-09-10
I have a large table and I am adding records using the following code. It chugs away at an incredibly slow pace. I've tried compacting the database, and tried using an append query, but that dopes not speed up the process.

Can anybody offer a suggestion to "speed-up" the process:
Set Rst = Nothing

StrSql = "SELECT Compare00MasterBackupForProcessingImprtTbl.RawData, Compare00MasterBackupForProcessingImprtTbl.ZipFileNm, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.ZipFilePath, Compare00MasterBackupForProcessingImprtTbl.ZipFileFileDate, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.OrderImported, Compare00MasterBackupForProcessingImprtTbl.Length, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.Size, Compare00MasterBackupForProcessingImprtTbl.Ratio, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.FileDate, Compare00MasterBackupForProcessingImprtTbl.FileTime, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.Path, Compare00MasterBackupForProcessingImprtTbl.FileNm, "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl.FileExt, Compare00MasterBackupForProcessingImprtTbl.DateEntered From "
StrSql = StrSql & "Compare00MasterBackupForProcessingImprtTbl ORDER BY Compare00MasterBackupForProcessingImprtTbl.OrderImported;"

Set Rst = dbs.OpenRecordset(StrSql)

If Not Rst.EOF Then

  While Not Rst.EOF
     StrSql = "SELECT Compare00MasterBackupArchiveTbl.RawData, Compare00MasterBackupArchiveTbl.ZipFileNm, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.ZipFilePath, Compare00MasterBackupArchiveTbl.ZipFileFileDate, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.OrderImported, Compare00MasterBackupArchiveTbl.Length, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.Size, Compare00MasterBackupArchiveTbl.Ratio, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.FileDate, Compare00MasterBackupArchiveTbl.FileTime, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.Path, Compare00MasterBackupArchiveTbl.FileNm, "
     StrSql = StrSql & "Compare00MasterBackupArchiveTbl.FileExt, Compare00MasterBackupArchiveTbl.DateEntered "
     StrSql = StrSql & "FROM Compare00MasterBackupArchiveTbl;"
     Set Rst2 = dbs.OpenRecordset(StrSql)
     On Error Resume Next ‘in case a new record duplicates a key index
     With Rst2
            If Not IsNull(Rst![RawData]) Then ![RawData] = Rst![RawData]
            If Not IsNull(Rst![ZipFileNm]) Then ![ZipFileNm] = Rst![ZipFileNm]
            If Not IsNull(Rst![ZipFilePath]) Then ![ZipFilePath] = Rst![ZipFilePath]
            If Not IsNull(Rst![ZipFileFileDate]) Then ![ZipFileFileDate] = Rst![ZipFileFileDate]
            If Not IsNull(Rst![orderimported]) Then ![orderimported] = Rst![orderimported]
            If Not IsNull(Rst![Length]) Then ![Length] = Rst![Length]
            If Not IsNull(Rst![Size]) Then ![Size] = Rst![Size]
            If Not IsNull(Rst![Ratio]) Then ![Ratio] = Rst![Ratio]
            If Not IsNull(Rst![FileDate]) Then ![FileDate] = Rst![FileDate]
            If Not IsNull(Rst![FileTime]) Then ![FileTime] = Rst![FileTime]
            If Not IsNull(Rst![Path]) Then ![Path] = Rst![Path]
            If Not IsNull(Rst![FileNm]) Then ![FileNm] = Rst![FileNm]
            If Not IsNull(Rst![FileExt]) Then ![FileExt] = Rst![FileExt]
            ![DateEntered] = Now()
     End With
     On Error GoTo 0


Question by:rogerdjr
LVL 28

Expert Comment

ID: 38384029
<<and tried using an append query, but that dopes not speed up the process.>>

You tried using an append query SQL statement within the procedure or you tried using a pre-compiled (saved query object) append query?

Access needs to compile queries at run-time when the query is constructed in a code procedure.  Saved queries are pre-compiled.  For complex SQL statements I have seen significant time differences in execution between a pre-compiled (saved) query and the same query issued as a SQL statement in a procedure.  Try testing by outputting the SQL statement to the Immediate window and then copying the SQL statement and using it to construct a sqved query.  Does the query execute considerably faster?

OM Gang
LVL 85
ID: 38384095
What's the reason for two recordsets? In most cases, you are much better off doing this:

INSERT INTO Compare00MasterBackupArchiveTbl(RawData, FileNumber, etc ect) (SELECT RawData, FileNum, etc etc FROM Compare00MasterBackupForProcessingImprtTbl)

Note that the number of columns must match, and also the datatype.
LVL 58
ID: 38384111
It's not even so much that it's saved or not, but that your executing a query for every interation for the loop.  And I don't see any where restriction?  so your processing the entire archive table for every record in the first query.  

But beyond that, you need to change your first query and do an outer join to Compare00MasterBackupArchiveTbl so you have both records available (if one in Compare00MasterBackupArchiveTbl exists).

Maybe you should also state what the process is that your trying to accomplish.

LVL 17

Accepted Solution

ramrom earned 2000 total points
ID: 38384243
"a large table"? You show 2 tables. Which is the "large" one and in what way is it "large"?

does Compare00MasterBackupForProcessingImprtTbl contain fields that are not in Compare00MasterBackupArchiveTbl?

ORDER BY will have no effect on the results, as tables are not necessarily stored in the order of insertion.

The most obvious fix is to move
  Set Rst2 = dbs.OpenRecordset(strSQL)
outside the while..wend loop.

Also you don't need a select statement for rst2.
Set Rst2 = dbs.OpenRecordset("Compare00MasterBackupArchiveTbl") is sufficient.
To improve readability, eliminate the table name qualifiers from the fields in the selects.

Author Closing Comment

ID: 38384389
Perfect solution - makes things run like greased lightning

The most obvious fix is to move
  Set Rst2 = dbs.OpenRecordset(strSQL)
outside the while..wend loop.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question