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 Subroutines
You'll find uses for subroutines as you begin writing larger applications. For example, suppose you were writing a company sales status program. You might need a specialized routine that calculates a cost of sales value and displays that value in a label. By putting that code in a subroutine procedure, you help separate the task from other tasks and make the application more manageable. In addition, if several procedures in the application need the calculation, you can call the procedure from every place that needs it instead of repeating the same code in every place.
To create a subroutine procedure, perform these steps:
- Make up an appropriate name for the procedure using the same naming rules as you use for variables. Give the procedure a meaningful name such as CostOfSales.
- Determine whether you want to put the procedure in the form module or in a separate external module. If you think you'll use the code in other applications, add a new module to your Project Explorer window, but if the code goes with this application only, you can add the code to the current form module.
- Open the Code window and scroll to the bottom. On a blank line below the last line type Private Sub CostOfSales(). (If you fail to type the parentheses, Visual Basic adds them for you because all procedure names terminate with the parentheses to hold possible arguments.) As soon as you press Enter, Visual Basic adds the end of the procedure, as shown in Figure 13.2's Code window.
After Visual Basic creates the place for the procedure, you can add the body of the code. For example, Listing 13.1 shows how you might code a cost of sales subroutine procedure. The procedure's job is to calculate the cost of sales from text box values and assign the cost to a label named lblCost.
Figure 13.2 You must fill in the procedure's body.
Example 13.1. A cost of sales subroutine.
1: Private Sub CostOfSales() 2: ' Computes a cost of sales and 3: ' displays that code in a label 4: Dim curGrossSales As Currency 5: Dim curCostSales As Currency 6: Dim sngOverHead As Single 7: Dim sngInventoryFctr As Single 8: Dim sngPilferFctr As Single 9: 10: ' Store initial variable values from controls 11: curGrossSales = txtGross.Text 12: sngInventoryFctr = txtTotalInv.Text * 0.38 13: sngPilferFctr = txtPilfer.Text 14: sngOverHead = 0.21 ' Fixed overhead percentage 15: 16: curCostSales = curGrossSales - (sngInventoryFctr * curGrossSales) 17: curCostSales = curCostSales - (sngPilferFctr * curGrossSales) 18: curCostSales = curCostSales - (sngOverHead * curGrossSales) 19: lblCost.Caption = Format(curCostSales, "Currency") 20: End Sub
To call this procedure, another procedure (such as a Click() event procedure or another standard procedure) can issue either of these statements:
Call CostOfSales() ' Calls the CostOfSales() subroutine CostOfSales ' Calls the CostOfSales() subroutine
If the subroutine uses no arguments, you don't need to use Call and the parentheses to trigger the subroutine's execution. If CostOfSales() did use one or more arguments, you would not need Call or the parentheses around the list of arguments.
Coding Functions | Next Section

Account Sign In
View your cart