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
- 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
Two Additional Select Case Formats
The two additional formats differ only slightly from the standard Select Case that you learned about in the previous section. They enable you to extend the power of Select Case so that Visual Basic can make Case matches on both comparison tests and ranges of values. Here is the first additional format:
Select Case Expression
Case Is Relation:
One or more Visual Basic statements
Case Is Relation:
One or more Visual Basic statements
[Case Is Relation:
One or more Visual Basic statements]
[Case Else:
One or more Visual Basic statements]
End Select
Relation can be whatever comparison test you want to perform against Expression at the top of the Select Case. The standard Select Case statement, discussed in the previous section, compared the Expression value against an exact Case match. When you use the comparison Is Select Case option, each Case can be matched on a comparison test.
The format of the second extra Select Case format is as follows:
Select Case Expression
Case expr1 To expr2:
One or more Visual Basic statements
Case expr1 To expr2:
One or more Visual Basic statements
[Case expr1 To expr2:
One or more Visual Basic statements]
[Case Else:
One or more Visual Basic statements]
End Select
The Case lines require a range, such as 4 To 6. The To Select Case option enables you to match against a range instead of a relation or an exact match.
Study Listing 7.5 to learn how to combine different Select Case statements to test for various values.
Example 7.5. Using Select Case to simplify complex nested If...Else statements.
1: Rem The following Select Case to End Select code 2: Rem assigns a student's grade and school name 3: Rem to the label on the form. The code checks 4: Rem to make sure that the student is not too 5: Rem young to be going to school. 6: Select Case intAge 7: ' Check for too young... 8: Case Is <5: lblTitle.Text = "Too young" 9: 10: ' Five-year olds are next assigned 11: Case 5: lblTitle.Text = "Kindergarten" 12: 13: ' Six to eleven... 14: Case 6 To 11: lblTitle.Text = "Elementary" 15: lblSchool.Text = "Lincoln" 16: 17: ' Twelve to fifteen... 18: Case 12 To 15: lblTitle.Text = "Intermediate" 19: lblSchool.Text = "Washington" 20: 21: ' Sixteen to eighteen 22: Case 16 To 18: lblTitle.Text = "High School" 23: lblSchool.Text = "Betsy Ross" 24: 25: ' Everyone else must go to college 26: Case Else: lblTitle.Text = "College" 27: lblSchool.Text = "University" 28: End Select
If the age is less than 5, the title label becomes Too young, and the school name remains blank. If the age is exactly 5 (intAge is obviously an integer value), the title gets Kindergarten, and the school name still remains blank. Only if the child is 5 or older are both the title and school name initialized.
If you were to rewrite this code using embedded If...Else logic, the code would become a nightmare. The Select Case's range testing, such as Case 16 to 18, saves a tremendous amount of If...Else logic.
Summary | Next Section

Account Sign In
View your cart