Microsoft Access 2010 Bible

(Rick Simeone) #1

Part II: Programming Microsoft Access


404


Using the Case Else statement is optional, but it’s always a good idea. The Case Else clause is
always the last Case statement of Select Case and is executed when none of the Case values
matches the expression at the top of the Select Case statement.

In some procedures, you might want to execute a group of statements more than one time. VBA
provides some constructs for repeating a group of statements.

Looping
Another very powerful process that VBA offers is repetitive looping β€” the capability to execute a
single statement or a group of statements over and over. The statement or group of statements is
repeated until some condition is met.

VBA offers two types of looping constructs:

l Do...Loop

l (^) For...Next
Loops are commonly used to process records within a recordset, change the appearance of controls on
forms, and a number of other tasks that require repeating the same VBA statements multiple times.
The Do...Loop statement
Do...Loop is used to repeat a group of statements while a condition is True or until a condition
is True. This statement is one of the most commonly used VBA looping constructs:
Do [While | Until Condition]
[VBA statements]
[Exit Do]
[VBA statements]
Loop
Alternatively, the While (or Until) may appear at the bottom of the construct:
Do
[VBA statements]
[Exit Do]
[VBA statements]
Loop [While | Until Condition]
Notice that Do...Loop has several options. The While clause causes the VBA statements within
the Do...Loop to execute as long as the condition is True. Execution drops out of the Do...
Loop as soon as the condition evaluates to False.
The Until clause works in just the opposite way. The code within the Do...Loop executes only
as long as the condition is False.
Placing the While or Until clause at the top of the Do...Loop means that the loop never exe-
cutes if the condition is not met. Placing the While or Until at the bottom of the loop means

Free download pdf