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
In this tutorial:
- Visual Basic Fundamentals
- Visual Basic Development Environment
- Visual Basic Editor Window
- Relationship Between Access and Visual Basic
- Visual Basic Debugging Tools
- Working with the Watch Window
- Variables and Constants
- Variable and Constant Scope
- Declaring Constants and Variables
- Dim Statement
- Enum Statement
- Event Statement
- Private Statement
- Public Statement
- Static Statement
- Type Statement
- Collections, Objects, Properties, and Methods
- DAO Architecture
- ADO Architecture
- Referencing Collections, Objects, and Properties
- Use Exclamation Points and Periods
- Assigning an Object Variable-Set Statement
- Object Methods
- Manipulating Complex Data Types Using DAO
- Working with ADO Recordsets
- Functions and Subroutines
- Sub Statement
- Understanding Class Modules
- Property Let
- Property Set
- Controlling the Flow of Statements
- Do...Loop Statement
- For...Next Statement
- For Each...Next Statement
- If...Then...Else Statement
- RaiseEvent Statement
- Stop Statement
- With...End Statement
- Running Macro Actions and Menu Commands
- Executing an Access Command
- Trapping Errors
- Working with 64-Bit Access Visual Basic for Applications
- Using LongPtr Data Types
- Supporting Older Versions of Access
- Using LongLong Data Types