Tighten up vb code for Access Database

Stevep4

New Member
Joined
Aug 28, 2015
Messages
30
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


Thanks
 
Last edited:

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
1,781
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

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

Docmd.GoToRecord,,acFirst

For i = 1 to Me.Recordset.RecordCount
   DoCmd.RunMacro "ApdTabtoTotalTest"
   DoCmd.GoToRecord,,acNext
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...
 

Forum statistics

Threads
1,085,210
Messages
5,382,369
Members
401,784
Latest member
Jonnyboisme

Some videos you may like

This Week's Hot Topics

Top