From the course: Access: VBA

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Repeat a task using a For…Next loop

Repeat a task using a For…Next loop

From the course: Access: VBA

Start my 1-month free trial

Repeat a task using a For…Next loop

- [Instructor] Many of the code segments you write, such as assigning a value to a variable or displaying a message box, will just need to run one time. If you work with record sets such as tables or queries, though, you might need to repeat a segment several times. One common way to repeat code in Access VBA is to create a ForNext loop. I'll demonstrate how to use ForNext loops in this movie. I have opened the course database file, and I'll press Alt + F11 to move to the Visual Basic Editor. Here in the Visual Basic Editor I need a code module, so I will open the Insert menu, and click Module. Now I can create my ForNext loop. I'll start by typing Sub ForNextEx, just for ForNext Example, followed by an open and closed parentheses with nothing in between because I'm not expecting any outside variables, and I'll give myself a little room to work. First, I'll declare a couple of variables. So I'll type Dim iCaseCount as Integer. This could indicate the number of cases of light bulbs…

Contents