Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Optimize Recordset
Sami Shamma 
             
2 years ago
Dear Friends.
This code works. however, it is not efficient. I have no doubt it can be optimized.
I feel that I am opening and closing the Recordset way too much. can anyone tell me if I can write it with opening the recordset only once or twice?

It is a simple update from a table that gets its data from the mainframe in the morning and update my InmateT (my main table), it checks if the Inmate exists, if so, it updates the record otherwise, it creates a new record.

Private Sub UpdateInmate()

    Status ".......Start time: " & Now()
    Dim db As Database
    Dim INumber As String       'Inmate Number
    Dim rsR As Recordset        'AlphaRawT
    Dim rsI As Recordset        'InmateT
    Dim NewRecords As Long   'count of new records
    Dim UpdatedRecords As Long 'count of Existing records
    
    NewRecords = 0
    UpdatedRecords = 0
    Set db = CurrentDb
    Set rsR = db.OpenRecordset("AlphaRawT")
    While Not rsR.EOF
        INumber = Nz(DLookup("InmateNumber", "InmateT", "InmateNumber =""" & rsR![Inmate Number] & """"), 0)
        If INumber <> 0 Then
            'Inmate exists - update the record
            Set rsI = db.OpenRecordset("SELECT * FROM InmateT WHERE InmateNumber = """ & INumber & """")
            rsI.Edit
            rsI!FacilityCode = Trim(rsR![Facility Code])
            rsI!Housing = Trim(rsR![Housing Unit Code])
            rsI!HousingCode = Trim(rsR![Housing Code])
            rsI!InmateName = Trim(rsR![Name (Full)])
            rsI!ReligionCode = Trim(rsR![Religion Affiliation Code])
            UpdatedRecords = UpdatedRecords + 1
            If UpdatedRecords Mod 2000 = 0 Then Status ".......Records processed = " & UpdatedRecords
            rsI.Update
            'rsI.Close
         Else
        'Inmate does NOT exist create a new record
            Set rsI = db.OpenRecordset("InmateT")
            rsI.AddNew
            rsI!InmateNumber = rsR![Inmate Number]
            rsI!FacilityCode = Trim(rsR![Facility Code])
            rsI!Housing = Trim(rsR![Housing Unit Code])
            rsI!HousingCode = Trim(rsR![Housing Code])
            rsI!InmateName = Trim(rsR![Name (Full)])
            rsI!ReligionCode = Trim(rsR![Religion Affiliation Code])
            NewRecords = NewRecords + 1
            rsI.Update
            'rsI.Close
        End If
        rsR.MoveNext
        rsI.Close
    Wend
    rsR.Close
    db.Close
    Set rsI = Nothing
    Set rsR = Nothing
    Set db = Nothing
    Status ".......New Records     = " & NewRecords
    Status ".......Updated Records = " & UpdatedRecords
    Status ".......End time: " & Now()
    
End Sub
Richard Rost  @Reply  
          
2 years ago
If your table is properly indexed (no duplicates) then you can skip checking to see IF he exists. Just add it and ignore the error generated by trying to add a duplicate record. That'll save you one read operation per loop.
Richard Rost  @Reply  
          
2 years ago
It might also be faster to just use a query to pull down ALL of the records from the mainframe and then process it locally instead of doing read/write operations on a linked table. Use a local temp table.
Sami Shamma OP  @Reply  
             
2 years ago
As for your second point, my raw table is a local table.
So, if I skip the checking for the inmate to exist (my table is indexed correctly), should I have two complete loops, one to add and one to update?
Richard Rost  @Reply  
          
2 years ago
One loop. Two recordsets.
Sami Shamma OP  @Reply  
             
2 years ago
I think I got what you are saying. Read one record from my raw data, and then add and update, ignoring errors.
Richard Rost  @Reply  
          
2 years ago
Bingo
Sami Shamma OP  @Reply  
             
2 years ago
I gained about 10%.

Thanks
Richard Rost  @Reply  
          
2 years ago
I'm fact, you're not doing any crazy calculations inside the loop, so why don't you just use an append query? It will probably run a lot faster.
Kevin Yip  @Reply  
     
2 years ago
Hi Sami, if AlphaRawT is a large table (hundreds of rows or more), then that DLookup() function will run hundreds of times or more, for every single row in AlphaRawT.  That seems like an area that could be optimized.  If it were me, I would add a Yes/No field to AlphaRawT to indicate if an inmate number exists or not, then run an update query to update that field.  That would find all the existing and non-existing inmate numbers in one go instead of using multiple DLookup() functions.

"Optimizing" usually means using as few table lookups (queries, recordsets, domain aggregate functions, etc.) as possible to do the same job.  That usually entails finding the ones with the most usages and reduce their use.
Richard Rost  @Reply  
          
2 years ago
Kevin's idea is sound, provided you have access to modify that table. If you're pulling from a mainframe and you don't have the ability to do that, I'd say just use an append query. As long as your field is indexed (no dup) by that InmateNumber field, then you won't get duplicates and they'll just be ignored.

Sami Shamma OP  @Reply  
             
2 years ago
Thank you Kevin, but Richard is correct, I do not know what data I am getting before I receive the Raw file.
Sami Shamma OP  @Reply  
             
2 years ago
I will try the append query option this week.
Richard Rost  @Reply  
          
2 years ago
Only downside with a query is that you can't see it running if it's a long process unless you use DoCmd.RunSQL and you'll get a progress bar.
Juan C Rivera  @Reply  
            
2 years ago
Sami are you a CO?  My brother and father are in NY.
Sami Shamma OP  @Reply  
             
2 years ago
I am in CT, Connecticut.

We should get together next time you are in NY.
Juan C Rivera  @Reply  
            
2 years ago
I work at West Point USMA.  Let connect this summer. How far are you from Orange County?
Sami Shamma OP  @Reply  
             
2 years ago
From my house to West Point is 124 miles, about a 2:15 drive. Not bad.

Yes, absolutely, let's connect.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/17/2026 10:33:06 AM. PLT: 1s