Tighten up vb code for Access Database


New Member
I have code in my Access Database that does the following:

In a form (MainForm) that has a record source from a table called SectionTable. The below code is looking at the first record in SectionTable, running a macro that is using the first record from the table, then moving on to the next record in the table and running the macro again. The only way I could get this to work is to repeat the below lines for the 150 records involved. I would like some code that will loop through all 150 records and then stop at the end. Loopuntilend? is there any code that can just repeat the two actions until the end of the source table?

Rich (BB code):
Function RunTotalTestLoop()
On Error GoTo Err_Form_Current_Error
DoCmd.GoToRecord , , acNext
DoCmd.RunMacro "ApdTabtoTotalTest" 'the commands that you want to run
DoCmd.GoToRecord , , acNext
DoCmd.RunMacro "ApdTabtoTotalTest" 'the commands that you want to run
DoCmd.GoToRecord , , acNext
DoCmd.RunMacro "ApdTabtoTotalTest" 'the commands that you want to run

Last edited:


Well-known Member
you could use a clone, but I think all you need is a counter based on the form recordset count and I don't be believe you need to clone the recordset for that. Maybe

Function RunTotalTestLoop()
Dim cntr as Long, i as Long


For i = 1 to Me.Recordset.RecordCount
   DoCmd.RunMacro "ApdTabtoTotalTest"
Next i

End Function
What is missing is an error handler of some sort because acNext will cause an error when you are at the last record. It could be as simple as On Error Resume Next after For i = ... but a full error handler would be better.
Wondering what the macro does...

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...