Microsoft KB Archive/90814

= ACC: How to Create Do While and Do Until Loops in a Macro =

Article ID: 90814

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q90814



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
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 INFORMATION
There are several types of Do loops. Each type handles the looping procedure and conditions differently. The different types of Do loops are as follows:


 * 1) Do While ... Loop

This loop will execute while the condition is true. If the loop is encountered and the condition is already false, the loop will not be performed.
 * 1) Do Until ... Loop

This loop will execute until the condition is true. If the loop is encountered and the condition is already true, the loop will not be performed.
 * 1) Do... Loop While

This loop will execute the first time unconditionally, and then loop while the condition is true. If the loop is encountered and the condition is already false, the loop will be performed once.
 * 1) Do... Loop Until

This loop will execute the first time unconditionally, and then loop until the condition is true. If the loop is encountered and the condition is already true, the loop will be performed once.

To perform a Do While or Do Until loop in a macro, use a macro similar to the following examples. To create the following examples, open a new macro and click Macro Names on the View menu. Add the following macro names and actions and save this macro group as Do_Loops.

Do While ... Loop and Do Until ... Loop
  Macro Name       Action -  Do_Loop1         RunMacro Loop1           MsgBox

Do_Loop1 Actions --  RunMacro Macro Name: Do_Loops.Loop1 Repeat Expression: 

Loop1 Actions ---  MsgBox Message: =&quot;Loop&quot;

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  and Do... Loop Until 
  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: 

Loop1 Actions ---  MsgBox Message: =&quot;Loop&quot;

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.

Conditions
The 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=True

If 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 Loops
Whether 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: kbhowto kbusage KB90814

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.