Access Database - vba Adding Records is painfully slow

Posted on 2012-09-10
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

    <<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 84
    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 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    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

    "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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    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.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now