Technorati labels: VBA, loop, for, while, do
From: julitta korol, "access.2003.programming. By. example. With. VBA. xml. And. asp", by wordware Publishing, Inc. 2005, p91-p101
(1) using the do... While Loop
- Do While Condition
- Statement1
- Statement2
- Statementn
- Loop
(2) continuously display an input box until the user enters the correct password
- Do While pword <> "dada"
- Pword = inputbox ("what is the report password? ")
- Loop
(3) Another approach to the do... While Loop
- Do
- Statement1
- Statement2
- Statementn
- Loop while Condition
When you test the condition at the bottom of the loop, the statements inside the loop areExecuted at least once.
(4) infinite loops:
- Sub sayhello ()
- Do
- Msgbox "hello ."
- Loop
- End sub
To stop the execution of the infinite loop, you must pressCTRL + break. When Visual Basic displays the message box "code execution has been interrupted," click end to end the procedure.
(5) do... Until repeats a block of code as long as something is false.
- Do until Condition
- Statement1
- Statement2
- Statementn
- Loop
(6)
- Do
- Statement1
- Statement2
- Statementn
- Loop until Condition
If you want the statementsExecute at least once, No matter what the value of the condition, place the condition on the line with the loop statement.
(7)
- For counter = start to end [step increment]
- Statement1
- Statement2
- Statementn
- Next [Counter]
(8)
- For each element in group
- Statement1
- Statement2
- Statementn
- Next [element]
Element is a variable to which all the elements of an array or collection will be assigned. This variable has to be ofVariant data type for an array and of the object data type for a collection. Group is a name of a collection or an array.
(9) Exiting loops early:Exit for, exit do, exit sub, exit function.
(10) When writing nested loops, you must make sure that each inner loop is completely contained inside the outer loop. Also, each loop has to have a unique counter variable.