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
- Comparison Operators
- The If Statement
- The If Statement's Else Branch
- Compound Comparisons with the Logical Operators
- Multiple Choice with Select Case
- Two Additional Select Case Formats
- Summary
- Q&A
- Workshop
- 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
Compound Comparisons with the Logical Operators
Visual Basic supports three additional operators—And, Or, and Not—that look more like commands than operators. And, Or, and Not are logical operators. Logical operators let you combine two or more comparison tests into a single compound comparison.
Table 7.3 describes the logical operators, which work just like their spoken counterparts.
Table 7.3. The logical operators.
| Operator | Use | Description |
| And | If (A > B) And (C < D) | Produces True if both sides of the And are true. Therefore, A must be greater than B and C must be less than D. Otherwise, the expression produces a false result. |
| Or | If (A > B) Or (C < D) | Produces True if either side of the Or is true. Therefore, A must be greater than B or C must be less than D. If both sides of the Or are false, the entire expression produces a false result. |
| Not | If Not(strAns = "Yes") | Produces the opposite true or false result. Therefore, if strAns holds "Yes", the Not turns the true result to false. |
As you can see from Table 7.3, the And and Or logical operators let you combine more than one comparison test in a single If statement. The Not negates a comparison test. You can often turn a Not condition around. Not can produce difficult comparison tests, and you should use it cautiously. The last If in Table 7.3, for instance, could easily be changed to If (strAns <> "Yes") to eliminate the Not.
Your code often must perform an assignment, print a message, or display a label if two or more conditions are true. The logical operators make the combined condition easy to code. Suppose that you want to reward the salesperson if sales total more than $5,000 and if the salesperson sells more than 10,000 units of a particular product. Without And, you have to embed an If statement in the body of another If statement like this:
If (sngSales > 5000.00) Then
If (intUnitsSold > 10000) Then
sngBonus = 50.00
End If
End If
Here is the same code rewritten as a single If. It is easier to read and to change later if you need to update the program:
If (sngSales > 5000.00) And (intUnitsSold > 10000) Then sngBonus = 50.00 End If
How can you rewrite this If to pay the bonus if the salesperson sells either more than $5,000 in sales or if the salesperson sells more than 10,000 units? Here is the code:
If (sngSales > 5000.00) Or (intUnitsSold > 10000) Then sngBonus = 50.00 End If
Listing 7.2 contains an If...Else that tests data from two divisions of a company and calculates values from the data.
Example 7.2. Calculating sales figures for a company's divisions.
1: If (intDivNum = 3) Or (intDivNum = 4) Then 2: curDivTotal = curDivSales3 + curDivSales4 3: curGrandDivCosts = (curDivCost3 * 1.2) + (curDivCost4 * 1.4) 4: Else 5: curDivTotal = curDivSales1 + curDivSales2 6: curGrandDivCosts = (curDivCost1 * 1.1) + (curDivCost5 * 1.9) 7: End If
If intDivNum contains either a 3 or a 4, the user is requesting figures for the East Coast, and the code in the first If branch executes to produce an East Coast pair of values. If intDivNum doesn't contain a 3 or a 4, the program assumes that intDivNum contains a 1 or a 2, and the West Coast pair of values is calculated in the Else portion.
Multiple Choice with Select Case | Next Section

Account Sign In
View your cart