Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to For Next Loop    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Total records affected
Jeff Bartuch 
     
3 years ago
Richard has shown many times how to make the total records evaluated in a loop (X=X+1) display in a status box, but is there a way to display the total number of records impacted by the loop? For example, 10 records ina table. Said loop goes through the entire recordset of 10 records, but the loop only impacts 4. I would like to see the 4 records impacted, not the 10 that makes up the entire recordset..
Alex Hedley  @Reply  
           
3 years ago
Create another counter and increase that when the condition you want matches
Jeff Bartuch OP  @Reply  
     
3 years ago
Alex, I appreciate you getting back to me, but I'm more of a visual learner. Any chance I could get some sort of example?
Alex Hedley  @Reply  
           
3 years ago
Example from page:
For Counter = Start to End [Step Increment]
    ' Your Code Here
    [If Condition Then Exit For]
Next [Counter]
Alex Hedley  @Reply  
           
3 years ago
Untested but hopefully gives you an idea

Dim x

For I = 1 To 10
  If I % 2 = 0
    x = x + 1
  End If
Next I
Jeff Bartuch OP  @Reply  
     
3 years ago
Thank you Alex. The hard part is I don't know how many records will be affected prior to the loop running. The loop will run through the records in a table changing a value based on a few factors. The loop runs great, but the counter I have setup totals how many records were evaluated. I'm looking for the total number of records the value was changed as a result of the loop. That is a number I don't have prior to the loop running.
Kevin Yip  @Reply  
     
3 years ago
A For-Next loop can only be used if you know the lower and upper limits.  If you don't, use the Do-Until loop instead:

Put a counter inside the recordset loop, and increment it by 1 whenever the condition is met.  E.g.:

    Dim r As Recordset, c As Integer
    c = 0
    Set r = CurrentDb.OpenRecordset( ... )
    If r.RecordCount > 0 Then
        r.MoveFirst
        Do Until r.EOF
            If X = Y Then c = c + 1
            r.MoveNext
        Loop
    End If
    MsgBox "Number of records affected: " & c    


P.S. The older Do-While loop can also be used, and it works oppositely to Do-Until:

     Do While Not r.EOF
          ...
     Loop
Jeff Bartuch OP  @Reply  
     
3 years ago
Perfect! Thank you

This thread is now CLOSED. If you wish to comment, start a NEW discussion in For Next Loop.
 

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/14/2026 6:23:02 PM. PLT: 1s