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
Error Checking
A big problem still exists with the application if you plan to distribute it to others. The problem is not in the logic, calculation, or form. The problem is the application's lack of error checking. If the user doesn't enter a value in one or more of the text boxes, the calculation will not work properly. Even worse, an error such as a divide-by-zero error could occur and stop the running program. Mathematically, one cannot divide by zero, and Visual Basic stops the program and issues a runtime error message if a divide-by-zero occurs. Any time you write an application that performs division, you should check to make sure that you never divide by zero.
You'll need to add error checking to the application to ensure that the user enters positive values greater than 0 in each of the text boxes before clicking the computational command button.
The error checking can be fairly simple. Convert the text box values to numbers, and if any text box contains zero or less, perform the following:
- Tell the user about the problem with a message box.
- When the user closes the message box, set the focus on the control with the bad value so the user can more easily enter a corrected value.
- Test the controls again before any calculation is performed to ensure that the problem is fixed.
Several approaches exist for handling this error. The approach this lesson uses is slightly advanced, but it gives you a chance to see an external standard module added to an application (an external code module that is different from the form module), and you'll get a glimpse of the function-writing code you'll learn in Hour 13, "Modular Programming." You will actually create your own function instead of using one of the built-in functions supplied with Visual Basic.
First, assign the hook to the other function in your cmdCompute_Click() event procedure like this:
Private Sub cmdCompute_Click() ' Use a For loop to calculate a final total ' investment using compound interest. ' ' intNum is a loop control variable ' sngIRate is the annual interest rate ' intTerm is the number of years in the investment ' curInitInv is the investor's initial investment ' sngInterest is the total interest paid Dim sngIRate As Single, sngInterest As Single Dim intTerm As Integer, intNum As Integer Dim curInitInv As Currency ' Error-checking If ErrorCheck() = 1 Then Exit Sub End If sngIRate = txtRate.Text / 100# intTerm = txtTerm.Text
ErrorCheck() is a procedure you'll add that checks the form for bad values. You'll add this procedure in a separate module, not at the bottom of the form module. Notice that you use the ErrorCheck() procedure just as you use built-in functions: You call the function with an empty argument list (no arguments are necessary), and the function returns a value. If that value is 1, the form contains an error, so you use the Exit Sub statement to terminate the event procedure and return the user to the form. (The previous lesson described other forms of the Exit statement such as Exit For.) If no error exists, the ErrorCheck() procedure will not return a 1, and the processing continues as normal.
A function procedure is a procedure that you write that accepts zero or more arguments and returns a single value. A subroutine procedure is a procedure that you write that accepts zero or more arguments but does not return a value.
You must now add the ErrorCheck() procedure. Unlike the event procedures you've been writing until now, the ErrorCheck() procedure is a function procedure and not a subroutine procedure. A function procedure always returns a value, whereas a subroutine procedure never returns a value. (Again, you'll learn more about function and subroutine procedures in Hour 13.)
To add an extra module to your project, perform these steps:
- Select Project | Add Module to add a new module (that you can view in a Code window) to the project. You could also right-click over your Project window and add the module from the pop-up menu.
- Click the Module icon that appears in the Add Module dialog box. Visual Basic adds a new module with the default name Module1 (and the filename Module1.bas). Figure 9.6's Project window shows the new module in your project. Your Code window will now display a blank module where you can type the module's code.
Figure 9.6 You've now added another code module to the project.
- Maneuver between modules and the form by double-clicking the Project window object you want to work with. For now, however, stay inside the new module.
Type the following function procedure inside the new module's Code window:
Public Function ErrorCheck() As Integer
IntPress As Single
' Error-checking for the form
If Val(frmInterest.txtRate.Text) <= 0 Then
intPress = MsgBox("Enter a value for the rate", vbCritical)
frmInterest.txtRate.SetFocus
ErrorCheck = 1
Exit Function
End If
If Val(frmInterest.txtTerm.Text) <= 0 Then
intPress = MsgBox("Enter a value for the term", vbCritical)
frmInterest.txtTerm.SetFocus
ErrorCheck = 1
Exit Function
End If
If Val(frmInterest.txtInvest.Text) <= 0 Then
intPress = MsgBox("Enter a value for the investment", vbCritical)
frmInterest.txtInvest.SetFocus
ErrorCheck = 1
Exit Function
End If
' No error occurred if execution gets here
ErrorCheck = 0
End Function
The first difference you'll notice between the function procedure and the event subroutine procedures you've seen so far is the difference between the opening and closing statements. The keyword Function distinguishes the function procedure from a subroutine procedure. The procedures you've seen until now were subroutine procedures that used the Sub keyword to define them. In addition, the function procedure's opening line ends with As Integer. This qualifier tells Visual Basic that the function procedure will return an integer value. Functions can return any datatype as long as you define the function to return the proper datatype with the As keyword.
The function then checks each text box on the form. All form references include the form name. Therefore, instead of referring to the interest rate text box as txtRate, the code qualifies the text box with a form name as follows: frmInterest.txtRate. Remember that an external standard module, such as this one, is not part of a form's code found in the form module. A standard module might need access to several forms in the same project, so the standard module needs the qualifying form name before each control name. After a text box is found to hold a bad value, a message box describes the problem. The module then sets the focus to that control. Therefore, the focus goes straight to the problem so the user can edit the control without having to find it first when the error message box goes away.
Finally, the code sets the function's return value to 1 if an error occurs and exits the function (and therefore the module) and returns the return value to the calling code, which is the form module. To return a value from a function procedure, simply assign the value that you want to return to the function name.
Figure 9.7 shows the message box that appears if the user enters a term value that's zero or less.
Figure 9.7 The user must enter valid data.
Save your project (Lesson 9 Int Proj would be a good name). When you save the project, Visual Basic asks for a form name and a module name as well (use Lesson 9 Int Form and Lesson 9 Int Mod to follow the naming convention that this 24-hour tutorial uses).
Summary | Next Section

Account Sign In
View your cart