MS-Access / Getting Started

Do...Loop Statement

Use a Do...Loop statement to define a block of statements that you want executed multiple times. You can also define a condition that terminates the loop when the condition is false.

Syntax

Do [{While | Until} <condition>]
    [<procedure statements>]
    [Exit Do]
    [<procedure statements>]
Loop

or

Do
    [<procedure statements>]
    [Exit Do]
    [<procedure statements>]
Loop [{While | Until} <condition>]

Notes

The <condition> is a comparison predicate or expression that Visual Basic can evaluate to True (nonzero) or False (zero or Null). The While clause is the opposite of the Until clause. If you specify a While clause, execution continues so long as <condition> is true. If you specify an Until clause, execution of the loop stops when <condition> becomes true. If you place a While or an Until clause in the Do clause, the condition must be met for the statements in the loop to execute at all. If you place a While or an Until clause in the Loop clause, Visual Basic executes the statements within the loop before testing the condition. You can place one or more Exit Do statements anywhere within the loop to exit the loop before reaching the Loop statement. Generally you'll use the Exit Do statement as part of some other evaluation statement structure, such as an If...Then...Else statement.

Example

To read all the rows in the tblCompanies table until you reach the end of the recordset (that is, the EOF property is true), enter the following:

Dim dbContacts As DAO.Database
Dim rcdCompanies As DAO.RecordSet
Set dbContacts = CurrentDb
Set rcdCompanies = dbContacts.OpenRecordSet("tblCompanies")
Do Until rcdCompanies.EOF
    <procedure statements>
    rcdCompanies.MoveNext
Loop
[Previous] [Contents] [Next]