Do While

The Do While loop uses a condition at the top of the loop to determine how many times statements inside the loop will execute. Because the loop's condition is checked first, it is possible that the statements inside the loop never execute.

Dim x As Integer Dim y As Integer

MsgBox "x is less than y"


In this loop, it's possible that x is less than y, preventing the statement inside the loop from ever executing.

In the Do While loop, the statements are executed so long as the condition evaluates to true. In other words, the loop stops when the condition is false.

In the next example, the Do While loop uses an increment statement to satisfy the condition, which allows the loop to iterate five times.

Dim x As Integer Dim y As Integer x = 0

MsgBox "The value of x is " & x x = x + 1


Knowing that the loop executes five times, what do you think the value of x is after the last iteration? The value of x is 4 after the last iteration. If you're having trouble seeing this, try placing this code in the Click event of a command button so you can step through it one iteration at a time, with each click of the command button.

0 0


  • columbus baggins
    Do while loop for incrementing values in access vba?
    7 years ago

Post a comment