ACC: How to Create Do While and Do Until Loops in a MacroLast reviewed: August 6, 1997Article ID: Q90814 |
The information in this article applies to:
SUMMARYModerate: Requires basic macro, coding, and interoperability skills. To perform a Do While or Do Until loop in a Microsoft Access macro, you need to use a combination of correct macro structure and the RunMacro action with appropriate information supplied in the Repeat Expression parameter. These requirements are described in more detail later in this article.
MORE INFORMATIONThere are several types of Do loops. Each type handles the looping procedure and conditions differently. The different types of Do loops are as follows:
Do While <Condition>... Loop and Do Until <Condition>... Loop
Macro Name Action ------------------------- Do_Loop1 RunMacro Loop1 MsgBox Do_Loop1 Actions -------------------------------------- RunMacro Macro Name: Do_Loops.Loop1 Repeat Expression: <your_condition> Loop1 Actions ------------------- MsgBox Message: ="Loop"In this example, the Do_Loop1 macro calls the Loop1 macro while the Repeat Expression parameter of the RunMacro action is true.
Do... Loop While <Condition> and Do... Loop Until <Condition>
Macro Name Action ------------------------- Do_Loop1 RunMacro RunMacro Loop1 MsgBox Do_Loop1 Actions -------------------------------------- RunMacro Macro Name: Do_Loops.Loop1 RunMacro Macro Name: Do_Loops.Loop1 Repeat Expression: <your_condition> Loop1 Actions ------------------- MsgBox Message: ="Loop"In this example, the Do_Loop1 macro calls the Loop1 macro once unconditionally, and then continues to call the Loop1 macro while the Repeat Expression parameter of the RunMacro action is True.
ConditionsThe condition used in the loop, which is supplied in the Repeat Expression parameter of the RunMacro action, can be based on a value in a field on a form, a property of a control on a form, or the value returned from a Visual Basic or Access Basic function. If the condition is based on a field in a form or a property of a control on a form, it will have syntax similar to:
Forms![CounterForm]![Counter]<=10 -or- Forms![EntryForm]![InvoiceNo].Visible=TrueIf the condition is based on the value returned from a Visual Basic or Access Basic function, it will have syntax similar to:
Time()>=TimeEntry() Do While Loops vs. Do Until LoopsWhether a loop is a Do While loop or a Do Until loop depends on the condition. The RunMacro action runs the macro until the condition in the Repeat Expression parameter is false. This behavior is exactly what is needed for a Do While loop. It is, however, the opposite of what is needed for a Do Until loop. Therefore, to make a condition for a Do Until loop work correctly, precede the condition in the RunMacro action with the NOT operator. Keywords : McrCond kbusage Version : 1.0 1.10 2.0 7.0 97 Platform : WINDOWS Hardware : x86 Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |