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:

  1. 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.
  2. 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.
  3. 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.

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.

caution

If you put code such as Listing 13.1 in an external module, you must precede all control names with the form name that contains those controls. Therefore, precede the text boxes and labels with the form name that contains those text boxes and labels (for example, frmSales.txtTotalInv.Text and frmSales.lblCost.Caption).

Listing 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

note

Use default property values for the text boxes and labels if you want to shorten your code somewhat. Coding just txtTotalInv accomplishes the same purpose as coding txtTotalInv.Text because Text is the default property for all text boxes. Caption is the default property for labels.

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.

Top Home