Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- The Do While Loops
- The Do Until Loop
- The Other Do Loops
- The For Loop
- Summary
- Q&A
- Workshop
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
The Do While Loops
Visual Basic supports several versions of the Do statement. The Do While loop is perhaps the most common looping statement that you'll put in Visual Basic programs. Do While works with comparison expressions just as the If statement does. Therefore, the six comparison operators that you learned about in the previous lesson work as expected here. Rather than controlling the one-time execution of a single block of code, however, the comparison expression controls the looping statements.
Like the If statement (covered in Hour 7, "Making Decisions" ) that ends with an End If statement, a loop will always be a multiline statement that includes an obvious beginning and ending of the loop. Here is the format of the Do While loop:
Do While (comparison test) Block of one or more Visual Basic statements Loop
The block of code continues looping as long as comparison test is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, for the block of code to somehow change a variable used in comparison test . The block of code keeps repeating as long as the Do While loop's comparison test continues to stay true. Eventually, comparison test must become false or your program will enter an infinite loop and the user will have to break the program's execution through an inelegant means, such as pressing the Ctrl+Break key combination.
An infinite loop is a loop that never terminates.
The Do While loop continues executing a block of Visual Basic statements as long as comparison test is true. As soon as comparison test becomes false, the loop terminates.
Listing 8.1 contains a section of an event procedure that contains a Do While loop that asks the user for an age. If the user enters an age less than 10 or greater than 99, the program beeps at the error and displays another input box asking for the age. The program continues looping, asking for the age, as long as the user enters an age that's out of range.
Example 8.1. The Do While loop executes as long as comparison test is true.
1: Dim strAge As String
2: Dim intAge As Integer
3: Dim intPress As Integer
4:
5: ' Get the age in a string variable
6: strAge = InputBox("How old are you?", "Age Ask")
7: ' Check for the Cancel command button
8: If (strAge = "") Then
9: End ' Terminates the application
10: End If
11:
12: ' Cancel was not pressed, so convert Age to integer
13: ' The Val() function converts strings to integers
14: intAge = Val(strAge)
15:
16: ' Loop if the age is not in the correct range
17: Do While ((intAge < 10) Or (intAge > 99))
18: ' The user's age is out of range
19: intPress = MsgBox("Your age must be between" & _
20: "10 and 99", vbExclamation, "Error!")
21: strAge = InputBox("How old are you?", "Age Ask")
22:
23: ' Check for the Cancel command button
24: If (strAge = "") Then
25: End ' Terminate the program
26: End If
27: intAge = Val(strAge)
28: Loop
Figure 8.1 shows the message box error that Listing 8.1 displays if the user enters an age value that's less than 10 or greater than 99. Listing 8.1 does nothing with MsgBox()'s return value stored in intPress. The user simply presses Enter to close the message box, so checking for intPress's value would not help this particular section of code.
Figure 8.1 The user sees this message as long as the age is out of range.
The code contains some redundancy. For example, two lines contain almost the same InputBox() function, and the same check for a Cancel button press appears twice in the program. There are other looping statements that you'll learn about later in this lesson; those statements can help simplify this code by removing some of the redundancy.
Perhaps the most important thing to note about the Do While loop in Listing 8.1 is that the body of the loop provides a way for comparison test to terminate. The code contains an intAge variable that the body of the loop reassigns each time the loop's block of code executes. Therefore, assuming that the user enters a different value for the age, the loop will test against a different set of comparison values, the comparison test will fail (which would mean that the age is inside the range), and the program will stop looping. If the loop body did nothing with the comparison test variable, the loop would continue forever.
The Do Until Loop | Next Section

Account Sign In
View your cart