Programming Fundamentals Using VBA - Do Loop
(Page 7 of 9 )
You use the Do loop as the sentinel-controlled loop. In other words, it will continue until a specific condition occurs (something equals something, or is greater than something, for example).
There are two variations of the Do loop—the Do While and the Do Until. We will look at each one.
Do While The Do While loop tests to see if a condition is true. If it is true, the statements in the loop execute. For instance, let’s look at the following subroutine:
Sub doTest()
Dim intCounter As Integer
Dim intTest As Integer
intTest = 1
intCounter = 1
Do While intTest = 1
Debug.Print "This is loop number " & intCounter
If intCounter >= 5 Then
intTest = 0
End If
intCounter = intCounter + 1
Loop
End Sub
You will notice that the Do While loop is not as self-contained as the For…Next loop discussed earlier. In this case, you have to set up two variables, one for the counter and one for the conditional test. In this particular subroutine, I wanted the loop to end after the fifth pass.
Unlike the For loop from before, we are not running this based on a counter but, instead, until intTest changes to a value other than 1. I then nested an If…Then structure within the loop and tested the counter value. As soon as that value is equal to 5, the If structure will change the intTest value to 0 and will prevent the loop from running again.
Since it is testing for a condition rather than the number of iterations, you would not normally use a counter in a Do loop. In this case, I forced one with a second variable and incremented it as the last line of the loop. There are a variety of ways to do this.
Notice that this structure ends with the word Loop. Remember, we are not required to work with a counter here.
If you try running the Do While code, your Immediate window gives you these results:

However, here is a question: what happens if, for some reason, intTest never gets a value of 1? Will this Do loop ever run? (All right! Two questions!) The answer is no, it won’t. However, what happens if you need the loop to run at least once?
Sub ifTest()
Dim strMessage As String
intNum = InputBox("Enter a number between 1 and 15", _
"Testing the If structure")
If intNum >= 1 And intNum <= 15 Then
iftest2
Else
MsgBox "Sorry, the number must be between 1 and 15"
End If
End Sub
–––––––––––––––––––––––––––-
Sub iftest2()
If intNum > 10 Then
MsgBox intNum & " is greater than 10"
Else
MsgBox intNum & " is less than 10"
End If
End Sub
Notice, in the ifTest subroutine, there is a test to see if the user has entered a number between 1 and 15. However, if the user enters a number out of bounds, the program just stops, and you need to start it up again. We want to keep prompting the user until he or she enters a number in the correct range.
Change ifTest to look as follows:
Sub ifTest()
Dim strMessage As String
Dim intTest As Integer
intTest = 1
Do
intNum = InputBox("Enter a number between 1 and 15",_
"Testing the If structure")
If intNum >= 1 And intNum <= 15 Then
intTest = 0
iftest2
Else
MsgBox "Sorry, the number must be between 1 and 15"
End If
Loop While intTest = 1
End Sub
Notice that we are not using a counter of any sort here. Instead, what we are doing is testing for intTest. However, what is interesting here is where the test occurs. Unlike the previous example, we are testing at the end of the loop rather than the beginning. This has the effect of forcing the loop to run at least once, which can be very handy in situations such as this.
Do Until This is a subtle variation of what we just looked at. In a Do While loop, you run the loop while a condition is true. However, in a Do Until loop, you run the loop until a condition becomes true.
Using the first example of the Do While loop above, with a few changes, you can make it into a Do Until loop:
Sub doTest()
Dim intCounter As Integer
Dim intTest As Integer
intTest = 1
intCounter = 1
Do Until intTest <> 1
Debug.Print "This is loop number " & intCounter
If intCounter >= 5 Then
intTest = 0
End If
intCounter = intCounter + 1
Loop
End Sub
Notice that here we are saying to run the loop until intTest does not equal 1. From that point on, everything else in this example is identical. You should see the same result as with the Do While loop.
Like the Do While loop, you can place the Until condition at the end of the structure after the word Loop. This forces the loop to run at least once.
We will be revisiting loop structures frequently throughout the book. Like variables, they are an integral part of most programming situations today.
Next: Arrays >>
More Visual Basic.NET Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter six of the book Access VBA Programming, written by Charles E. Brown and Ron Petusha (McGraw-Hill/Osborne, 2004; ISBN: 0072231971). Check it out at your favorite bookstore today. Buy this book now.
|
|