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
Perhaps the most important statement in a program is the If statement and its cousin statements. With logic that If provides, your application can begin to analyze data and make decisions based on that analysis. For example, your program can display a three-button message box and determine, with the If statement, which command button the user clicked to close the message box.
If uses the comparison operators you learned earlier in this lesson to test data values. If performs one of two possible code actions, depending on the result of the comparison. In other words, If uses comparison operator results to test data. If might execute one or more lines of subsequent code, depending on the result of a comparison.
Before If, the code you wrote executed sequentially, one statement after another. If lets your program be more decisive and execute only parts of the program if the data warrants partial execution. For example, suppose you were writing an invoicing system. In such a system, no sales tax should be computed for tax-exempt organizations, so your program would skip over the tax computation code when processing such organizations.
If makes decisions. If a comparison test is true, the body of the If statement executes. (In fact, the previous sentence is almost identical to Visual Basic's If statement!) Here is one format of If:
If comparisonTest Then One or more Visual Basic statements End If
End If tells Visual Basic where the body of the If statement ends. Suppose that the user enters a sales figure into a Text Box control named txtSales. The following If computes a bonus amount based on the sales:
If (txtSales.Text > 5000.00) Then sngBonus = txtSales.Text * .12 End If
Remember that Visual Basic stores 0 in all numeric variables that you don't first initialize. Therefore sngBonus has a 0 before the If executes. When If executes, the code changes the sngBonus variable only if the value of the txtSales.Text property is more than 5000.00. In a way, the If reads like this:
If the sales are more than $5,000.00, then compute a bonus based on that sales value.
Visual Basic stores a null zero in string variables that you have not yet initialized. If you use an uninitialized Variant datatype variable, the variable holds a null value that becomes zero if you assign the variable to a numeric variable.
The body of an If can have more than one statement. The body is often known as a block. The following If calculates a bonus, the cost of sales, and a reorder amount based on the value of the txtSales text box entry:
If (txtSales.Text > 5000.00) Then sngBonus = txtSales.Text * .12 curCostOfSales = txtSales.Text * .41 curReorderCost = txtSales.Text * .24 End If
The three statements that make up the body of the If execute only if the condition txtSales.Text > 5000.00 is true. Suppose that this code contains another assignment statement immediately after End If. That assignment statement is outside the body of the If, so the true or false result of the condition affects only the body of the If. Therefore, the tax computation in the following routine executes regardless of whether the sales are more or less than $5,000.00:
If (txtSales.Text > 5000.00) Then sngBonus = txtSales.Text * .12 curCostOfSales = txtSales.Text * .41 curReorderCost = txtSales.Text * .24 End If sngTax = .12 * txtSales.Text
Can you see how the program makes decisions using If? The body of the If executes only if the comparison test is true. Otherwise, the rest of the program continues as usual.
There is a shortcut form of If that you might run across. The single-line If statement has a format that looks like this:
If comparisonTest Then VBStatement
The single-line If doesn't require an End If statement because the comparison test and the body of the If reside on the same line. Single-line If statements don't provide for easy program maintenance. If you decide that you want to add to the body of the If, you must convert the single-line If to a multiple-line If, and you might forget to then add End If. Therefore, even if the body of an If statement takes only one line, code the If as a multiple-line If...End If statement to make the program more maintainable.
The If Statement's Else Branch | Next Section

Account Sign In
View your cart