How to Create Sub Procedure or Function Procedure

How to Create Sub Procedure or Function Procedure

A Sub procedure is a series of Visual Basic statements that start with the Sub and end with End Sub. A Function procedure is a series of Visual Basic statements that start with the Function and end with End Function. The Sub and Function procedure perform a task and then returns control to the calling code. However, the Sub procedure does not return a value to the calling code, but the Function procedure returns a value to the calling code.

A Sub procedure and a Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

You can define a Sub and Function procedure in modules, classes, and structures. By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it.

In this How To, I will demonstrate how to convert from regular event procedure to Sub Program and how to create different type of Sub and Function procedure.



On Click Event Procedure:

Before we create a Sub Procedure We need to create a blank form with textboxes and Calculate button. I will use the previous form from the How to Find the Total (sum). Or you can create a new form with Calculate button and insert three textboxes as shown below. I will name textboxes as txtChk1 for check#1, txtChk2 for Check#2, and txtTotal for a Total. You will see the code under the Calculate button to find the sum of two checks.

on-click-event

Private Sub cmdCalculate_Click()
Dim Total As Double
If Not IsNull(Me.txtChk1) Then
   Total = Me.txtChk1.Value
End If
If Not IsNull(Me.txtChk2) Then
   Total = total + Me.txtChk2.Value
End If
Me.txtTotal = Total
End Sub

Convert On Click Event Procedure to Sub Procedure

Per above code, we have the working code to find the sum of two checks. We will use the code above for a new Sub procedure. We will need to change the Private Sub cmdCalculate_Click() to Sub FindTotal() procedure and keep the rest of all codes. The completed Sub Procedure is displaying below.

Sub FindTotal()
Dim Total As Double
If Not IsNull(Me.txtChk1) Then
   Total = Me.txtChk1.Value
End If
If Not IsNull(Me.txtChk2) Then
   Total = total + Me.txtChk2.Value
End If
Me.txtTotal = Total
End Sub

Call Sub Procedure

After we have a Sub procedure, we will call the Sub procedure under the On Click event procedure of Calculate button as shown below. We use a word “Call” before the Sub name as Call FindTotal.

Private Sub cmdCalculate_Click()

Call FindTotal

End Sub

Create Sub Procedure with Input Parameter and Output Control

The above Sub FindTotal() is a collection of statement(code). As stated at the beginning, the Sub procedure does not return a value to the calling code. However, we can work around to use parameter as a return value. We can create a Sub procedure with the input and output parameter similar to the function. Per Sub below, I add Num1 and Num2 as a parameter for input and txtTotal as an output field. When we add the value of input Num1 and Num2 then we need the sum to display on another control field of txtTotal.

Sub FindTotal(Num1 As Integer, Num2 As Integer, txtTotal As Control)
Dim Total As Double
Total = 0
If Not IsNull(Num1) Then
    Total = Num1
End If
If Not IsNull(Num2) Then
    Total = Total + Num2
End If
txtTotal = Total
End Sub

Sub procedure can also be created in the Module that can be called anywhere in the program. However, we just need to add a word “Public” in front of Sub like:

Public Sub FindTotal(Num1 As Integer, Num2 As Integer, txtTotal As Control)

 

Call Sub Procedure with Input Parameter and Output Control

The advantage of Sub is similar to Function in which it can be called many times. For example below, I add another three textboxes; txtCash1, txtCash2, and txtTotalCash. I also want to display the sum of cash #1 (txtCash1) and cash #2 (txtCash2) under the Total Cash textbox (txtTotalCash). I don’t have to type all codes again as I already have a Sub FindTotal() procedure. I can find the sum of two checks and two cash same time under the Calculate button as shown below.

  • I start with a word “Call” each time I call the Sub Procedure
  • Replace Num1 with Me.txtChk1 (for Check #1)
  • Replace Num2 with Me.txtChk2 (for Check #2)
  • Replace txtTotal with Me.txtTotalChk (for Total Check)
  • Completed as : Call FindTotal(Me.txtChk1, Me.txtChk2, Me.txtTotalChk)
  • Replace Num1 with Me.txtCash1 (for Cash #1)
  • Replace Num2 with Me.txtCash2 (for Cash #2)
  • Replace txtTotal with Me.txtTotalCash (for Total Cash)
  • Completed as : Call FindTotal(Me.txtCash1, Me.txtCash2, Me.txtTotalCash)

Private Sub cmdCalculate_Click()

        Call FindTotal(Me.txtChk1, Me.txtChk2, Me.txtTotalChk)

        Call FindTotal(Me.txtCash1, Me.txtCash2, Me.txtTotalCash)

End Sub

sub-example

Example of Sub Procedure

After we enter a number for check #1, check #2, Cash #1, and Cash #2 then click Calculate button. The result for Total Check and Total Cash will display same time as we call the Sub procedure under the Calculate button.

sub-cal-with-parameter



Convert to Function Procedure

The Function procedure similar to Sub procedure, however it can return a value to the calling code. If it is used under the current form as local, it will start with Function and end the statement with End Function. Mostly, the function will be placed under the Module for universal use. It will start with Public Function and end the statement with End Function. For example below, I change from Sub procedure to Function procedure by change Sub to Function at beginning and the end.

Function FindTotal(Num1 As Integer, Num2 As Integer, txtTotal As Control)
Dim Total As Double
Total = 0
If Not IsNull(Num1) Then
   Total = Num1
End If
If Not IsNull(Num2) Then
   Total = Total + Num2
End If
txtTotal = Total
End Function

Call Function Procedure

The Function procedure can be called and the result will be the same as Sub procedure described above.

 Private Sub cmdCalculate_Click()

      Call FindTotal(Me.txtChk1, Me.txtChk2, Me.txtTotalChk)

      Call FindTotal(Me.txtCash1, Me.txtCash2, Me.txtTotalCash)

End Sub

Create Function Procedure to return a value

The example of Function procedure below is the standard format of Function. It starts with Function, Function name (FindTotal), the parameters (Num1 and Num2) inside the parenthesis or brackets (), and the type of value (As Double) that is returned from the function. Num1 and Num2 are the input parameters. The value from the input parameters will be assigned to function name FindTotal on line FindTotal = Total before the end of function.

Function FindTotal(Num1 As Integer, Num2 As Integer) As Double
FindTotal =0
If Not IsNull(Num1) Then
   Total = Num1
End If
If Not IsNull(Num2) Then
   Total = Total + Num2
End If
FindTotal = Total
End Function

Call Function Procedure to get a return value

When we call a function, the function is just returning the value from the input parameter. The returned value can be assigned to any variable or control field. For example below, I assign the value from the sum of check #1 and check #2 from the function FindTotal to textbox txtTotalChk and the sum value of cash #1 and cash #2 to textbox txtTotalCash.

Private Sub cmdCalculate_Click()

     Me.txtTotalChk = FindTotal(Me.txtChk1, Me.txtChk2)

     Me.txtTotalCash = FindTotal(Me.txtCash1, Me.txtCash2)

End Sub

Now I add another textbox called CheckAndCash with a label of Total Check + Cash to get the sum of all checks and cash. We can find the sum for CheckAndCash by adding the value of txtTotalChk and txtTotalCash or we can use the function FindTotal with the input parameters of txtTotalChk and txtTotalCash as shown below.

Private Sub cmdCalculate_Click()

      Me.txtTotalChk = FindTotal(Me.txtChk1, Me.txtChk2)

     Me.txtTotalCash = FindTotal(Me.txtCash1, Me.txtCash2)

     Me.CheckAndCash = Me.txtTotalChk.Value + Me.txtTotalCash.Value

     ‘ or Me.CheckAndCash = FindTotal(Me.txtTotalChk.Value, Me.txtTotalCash.Value)

End Sub

cub-with-parameter

 Example of FindTotal() Function

function-ex

Create Function Procedure under the Module

We can put the function under the Module if we think we will call this function from another form. That means we can call this function from anywhere in this program. We will need to add a word “Public” in front of the function as displayed below. However, the function name should not be the same as the Module name.

Public Function FindTotal(Num1 As Integer, Num2 As Integer) As Double
FindTotal =0
If Not IsNull(Num1) Then
    Total = Num1
End If
If Not IsNull(Num2) Then
    Total = Total + Num2
End If
FindTotal = Total
End Function

Create Function Procedure from the Module

Call the function from Module is the same as a regular function in the local form. See code below.

Private Sub cmdCalculate_Click()

Me.txtTotalChk = FindTotal(Me.txtChk1, Me.txtChk2)

Me.txtTotalCash = FindTotal(Me.txtCash1, Me.txtCash2)

Me.CheckAndCash = Me.txtTotalChk.Value + Me.txtTotalCash.Value

‘ or Me.CheckAndCash = FindTotal(Me.txtTotalChk.Value, Me.txtTotalCash.Value)

End Sub