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
- [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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Repeat a task using a For…Next loop5m 5s
-
(Locked)
Step through all items of a collection using a For…Each loop4m 59s
-
(Locked)
Repeat a task using a Do…While loop5m 32s
-
(Locked)
Perform a task when conditions are met using an If…Then statement6m 20s
-
Select actions using a Case statement6m 17s
-
(Locked)
-
-
-
-
-
-
-