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
Coding Functions
You can write your own general-purpose function procedures that are not tied to specific events. You can call these functions from any Visual Basic application just as you can subroutine procedures. Function procedures work just like subroutine procedures in every way; you call them from elsewhere in the code. Unlike subroutine procedures, however, a function procedure always returns a value.
If you run across a needed calculation and Visual Basic has no built-in function equivalent, you can write your own function that returns that calculated value. When you call the function, you must do something with the returned value. You cannot put a function call on a line by itself as you can with a subroutine. If CalcTax() is a function, you cannot call the function like this:
CalcTax () ' Problem!
The CalcTax() function will return a value and you must do something with that value. Therefore, you'll usually assign the return value like this:
lblAmt.Caption = CalcTax() ' Okay
You can also use the function call inside an expression, like this:
curAmount = Estimate * .2 + CalcTax() * .14
The functions that you write aren't quite as built-in as Visual Basic's built-in functions, but they behave the same way. Your functions never become part of VB's repertoire, but you can put them in any module that needs to access them. Over time, you will write many general-purpose function and subroutine procedures and you might want to keep a module library of common routines that you'll use throughout different applications. To use one of the procedures that you write, you can add that procedure's module to whatever application needs the procedure.
You will write new function procedures the same way you write new subroutine procedures (with Tools | Add Procedure or by typing the first function procedure's line at the end of the module). Use the Function keyword in place of Sub. The following statements would code the beginning and ending statements from a CalcTax() function:
Public Function CalcTax () As Single End Function
You'll notice something extra on that function's opening statement: As Single. In addition to using the Function keyword, you must also specify the function's return value datatype in the function's opening declaration line. Therefore, this CalcTax() function returns a single-precision datatype.
Listing 13.2 contains a function that computes the postage for a letter or package using the following rules:
- The post office charges 32 cents for 8 ounces or less.
- Add 15 cents for each 4 ounces above the first 8.
- The weight cannot exceed 24 ounces.
The function's code assumes that the letter or package weight appears in a text box control named txtWeight.Text. In addition, the weight must appear as ounces. Therefore, any application that uses this function must make sure these conditions are met before calling the function.
Example 13.2. Calculating postage with a function procedure.
1: Public Function Postage() As Currency
2: ' Calculate postage based on the
3: ' weight of a letter or package
4: Dim curPostHold As Currency
5: Dim intWeight As Integer
6: Dim intPress As Integer ' MsgBox() return
7:
8: ' Grab the weight from the text box
9: ' and convert to number for comparison
10: intWeight = Val(txtWeight.Text)
11:
12: Select Case intWeight
13: Case Is <= 8: curPostHold = 0.32
14:
15: Case Is <= 12: curPostHold = 0.47
16:
17: Case Is <= 16: curPostHold = 0.62
18:
19: Case Is <= 20: curPostHold = 0.77
20:
21: Case Is <= 24: curPostHold = 0.92
22:
23: Case Is > 24:
24: intPress = MsgBox("Weight is too heavy" , _
25: vbExclamation, "Error")
26: curPostHold = 0#
27: End Select
28:
29: Postage = curPostHold ' Return the value
30: End Function
Listing 13.2 demonstrates the way you return the value from a function. There is no Postage variable declared, yet the second-to-last line assigns a value to Postage. Postage is the name of the function, not a variable. Inside a function procedure, when you assign a value to the function's name, the function uses that value as the return value. This function doesn't actually end until the End Function statement is reached, but the return value is set right before the terminating statement.
Coding Arguments | Next Section

Account Sign In
View your cart