Tighten up vb code for Access Database

Stevep4

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

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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...
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,205
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top