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 Other Do Loops
Another pair of Do loops work almost exactly like the two previous loops. Do...Loop While and Do...Loop Until look very much like their counterparts that you learned about earlier. But these new loop formats check their comparison tests at the bottom of the loop rather than at the top.
If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of Do...Loop While:
Do Block of one or more Visual Basic statements Loop Until (comparison test)
That Do looks lonely by itself, doesn't it? The purpose of the Do is to signal the beginning of the loop. The loop continues until the Loop Until statement. The comparison test appears at the bottom of the loop if you use the Do...Loop While loop statement. The body of the loop always executes at least once. The body of the loop executes more than once as long as the comparison test stays true. There is a corresponding Do...Loop Until statement that checks for a false condition at the bottom of the loop's body.
Notice that the Do...Loop While loop's comparison test appears at the bottom of the loop instead of at the top of the loop. You'll use the Do...Loop While loop when you want the body of the loop to execute at least one time. Often, by placing comparison test at the bottom of the loop, you can eliminate redundant code that otherwise might be required if you used Do While.
To complete the loop statements, Visual Basic also supports a Do...Loop Until statement. Like the Do...Loop While, the Do...Loop Until statement tests comparison test at the bottom of the loop. Therefore, the body of the loop executes at least once, no matter what comparison test turns out to be. The loop continues as long as the comparison test result stays false.
Listing 8.3 contains the age-checking procedure that's much shorter than the previous versions. comparison test appears at the bottom of the loop, so the extra InputBox() function call isn't needed.
Example 8.3. Using the Do...Loop While to check the comparison at the bottom of the loop.
1: Dim strAge As String
2: Dim intAge As Integer
3: Dim intPress As Integer
4:
5: Do
6: strAge = InputBox("How old are you?", "Age Ask")
7: ' Check for the Cancel command button
8: If (strAge = "") Then
9: End ' Terminate program
10: End If
11: intAge = Val(strAge)
12:
13: If ((intAge < 10) Or (intAge > 99)) Then
14: ' The user's age is out of range
15: intPress = MsgBox("Your age must be between" & _
16: "10 and 99", vbExclamation, "Error!")
17: End If
18: Loop While ((intAge < 10) Or (intAge > 99))
The loop begins almost immediately. The loop's body will always execute at least once, so InputBox() appears right inside the loop. By placing the InputBox() function inside the loop, you eliminate the need to put this function in the code twice (once before the loop and once inside the loop, as was necessary using the previous looping statements in Listings 8.1 and 8.2).
The For Loop | Next Section

Account Sign In
View your cart