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
The If Statement's Else Branch
Whereas If executes code based on the comparison test's true condition, the Else statement executes code based on the comparison test's false condition. Else is an optional part of the If statement. Else specifies the code that executes if the comparison test is false. The complete format of the If statement with Else is as follows:
If comparisonTest Then One or more Visual Basic statements Else One or more Visual Basic statements End If
Typically, programmers call this full-blown If statement the If...Else statement. The If...Else statement is sometimes called a mutually exclusive statement. The term mutually exclusive simply means that one set of code or the other executes, but not both. The If...Else statement contains two sets of code—that is, two bodies of one or more Visual Basic statements—and only one set executes, depending on the result of the If. An If statement is either true or false because the If's comparison produces either a true or false result. Therefore, either the first or the second body of code in an If...Else executes.
Suppose that a salesperson receives a bonus if sales are high (more than $5,000.00) or suffers a pay cut if sales are low (less than $5,000.00). The If...Else shown next contains the code necessary to reward or punish the salesperson. The If code body computes the bonus, as you saw in the previous section. The code body of the Else subtracts $25 from the salesperson's pay, which is stored in the curPayAmt variable, if the sales quota isn't met. The following code computes such a payment amount based on the quota:
1: If (txtSales.Text > 5000.00) Then 2: sngBonus = .05 * txtSales.Text 3: Else 4: curPayAmt = curPayAmt - 25.00 5: End If 6: curTaxes = curPayAmt * .42
The fourth line of code might surprise you at first. The assignment appears to make the statement that the pay is equal to the pay minus 25. You know that nothing can be equal to itself minus 25. In math, the equal sign acts as a balance for the two sides of the equation. In Visual Basic, when the equal sign isn't used inside an If's comparison test, it is an assignment that takes everything to the right of the equal sign and stores that value in the variable to the left of the equal sign. Therefore, the fourth line subtracts the 25 from the value stored in curPayAmt and assigns that result back to curPayAmt. In effect, it lowers the value of curPayAmt by 25.
To further your understanding of the If...Else statement and to demonstrate testing for an input box's return value, study how Listing 7.1 uses If...Else to respond to an input box. The code asks the user for a company name and then accepts the name or recognizes that the user clicked Cancel to close the input box without answering it. (When a user clicks Cancel in response to an input box, the input box returns a null string, "".)
Example 7.1. Checking an input box's return value.
1: Dim strCompName As String
2: Dim intPress As Integer ' MsgBox return value
3: ' Ask the user for a name
4: ' Use XYZ, Inc. for the default name
5: strCompName = InputBox("What is the company name?",_
6: "Company Request", "XYZ, Inc.")
7: ' Check the return value
8: If (strCompName = "") Then
9: ' The user clicked Cancel
10: intPress = MsgBox("Thanks anyway")
11: Else
12: ' The user entered a company name
13: intPress = MsgBox("You entered " & strCompName)
14: End If
Compound Comparisons with the Logical Operators | Next Section

Account Sign In
View your cart