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 Until Loop
Whereas the Do While loop continues executing the body of the loop as long as the comparison test is true, the Do Until loop executes the body of the loop as long as the comparison test is false. The program's logic at the time of the loop determines which kind of loop works best in a given situation.
Do Until works almost exactly like the Do While loop except that the Do Until loop continues executing the body of the loop until the comparison test is true. Like the Do While, the Do Until is a multiline looping statement that can execute a block of code that's one or more lines long.
Here is the format of Do Until:
Do Until (comparison test) Block of one or more Visual Basic statements Loop
You can use the Do While or the Do Until for almost any loop. Listing 8.2 contains the age-checking event procedure that contains a Do Until loop. The loop ensures that the age falls between two values. As you can see, comparison test for the Do Until is the opposite of that used in Listing 8.1's Do While loop.
Example 8.2. The Do Until loops until comparison test becomes 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:
8: ' Check for the Cancel command button
9: If (strAge = "") Then
10: End ' Terminate the program
11: End If
12:
13: ' Cancel was not pressed, so convert Age to integer
14: intAge = Val(strAge)
15: ' Loop if the age is not in the correct range
16: Do Until ((intAge >= 10) And (intAge <= 99))
17: ' The user's age is out of range
18: intPress = MsgBox("Your age must be" & _
19: "between 10 and 99", vbExclamation, "Error!")
20: strAge = InputBox("How old are you?", "Age Ask")
21: ' Check for the Cancel command button
22: If (strAge = "") Then
23: End ' Terminate the program
24: End If
25: intAge = Val(strAge)
26: Loop
The 16th line provides the only real difference between Listing 8.1 and Listing 8.2. The age must now fall within the valid range for the loop to terminate.
The Other Do Loops | Next Section

Account Sign In
View your cart