Visual Basic 6.0 Guide
Procedures

What is A Procedure?

You will need to be able to use 3 types of procedure in Visual Basic,

  1. Event Procedures
  2. Sub Procedures
  3. Function Procedures

So far, you have only used the first type, event procedures. The example below is from the exercises on loops.

Private Sub Form_Load()
Form1.Show
Dim intCounter As Integer
For intCounter = 1 To 10
Print intCounter
Next intCounter
End Sub

The code above is executed when the form loads. You have also written procedures that execute when a button or a listbox is clicked or double clicked.

No Need To Repeat Yourself

There are many reasons to write your code in procedures. One of the main reasons is to avoid having to write the same code over and over again in the same program.

1. Create a new project in Visual Basic.
2. Add 3 horizontal scrollbars to the form. (Do not copy and paste)
3. Name them hsbRed, hsbGreen, hsbBlue and set the min property to 0 and the max property to 255.
4. Add labels to the form to label the scrollbars, red, green and blue.

Your form should now look like the example below.

scrollbar form design

Colours can be made up of differing amounts of Red, Green and Blue form 0 to 255. Our program will set the background colour of the form to the mixture of red, green and blue indicated by the scrollbars.

5. Double click the first scroll bar and type the following code.

Private Sub hsbRed_Change()
Form1.BackColor = RGB(hsbRed.Value, hsbGreen.Value, hsbBlue.Value)
End Sub

6. Double click the second and third scrollbars and type the same line of code in the event procedures This will mean that you have the following code in the code window.

Private Sub hsbBlue_Change()
Form1.BackColor = RGB(hsbRed.Value, hsbGreen.Value, hsbBlue.Value)
End Sub

Private Sub hsbGreen_Change()
Form1.BackColor = RGB(hsbRed.Value, hsbGreen.Value, hsbBlue.Value)
End Sub

Private Sub hsbRed_Change()
Form1.BackColor = RGB(hsbRed.Value, hsbGreen.Value, hsbBlue.Value)
End Sub

Although this is not a great deal of code, you have to repeat yourself nonetheless. We can do this more efficiently if we have only one procedure to change the form's colour.

7. Return to the code window and type the following three lines.

Public Sub changeFormColour()
Form1.BackColor = RGB(hsbRed.Value, hsbGreen.Value, hsbBlue.Value)
End Sub

8. Change the event procedures so that they are as follows.

Private Sub hsbRed_Change()
changeFormColour
End Sub

This will call our newly-written procedure. In this program, having the procedure doesn't make a huge saving in time. It does mean, however, that if we have made a mistake, there is only one part of the code that needs to be corrected.

Passing Information To A Procedure

We can send data to a procedure when it is called in our code. The data that we pass to a procedure is called a parameter.

Create a new project and add 3 text boxes to the form. These should be called txtFirstNumber, txtSecondNumber and txtAnswer. Add a command button called cmdAdd. We are going to create a whizz-bang procedure for adding together two numbers.

1. Click on View Code on the menu and type in the following procedure.

Private Sub AddNumbers(intNum1 As Integer, intNum2 As Integer)
Dim intAnswer As Integer
intAnswer = intNum1 + intNum2
MsgBox intAnswer
End Sub

Look carefully at the first line. Our procedure has 2 parameters, intNum1 and intNum2. Both are integers. These names are used within the procedure to refer to the numbers that we are going to pass to it.

2. Now, time to write the code that calls the procedure. Double click on the command button and enter the following lines.

Private Sub cmdAdd_Click()
Dim intFirst As Integer, intSecond As Integer
intFirst = txtFirstNumber.Text
intSecond = txtSecondNumber.Text
AddNumbers intFirst, intSecond
End Sub

Simple Tasks

1. Add another button to the form. Write a new procedure that will MessageBox the value of the smallest number.

2. Do the same to get the largest number.

3. Write one more procedure that will multiply the two numbers together.

Function Procedures

Function procedures differ from Event and Sub procedures in that they return a value to the program. This can make them especially useful.

Visual Basic has a number of built-in procedures that can be used.

For example, the Left function returns a number of characters from the left hand side of a string variable.

strName= "Jeff"
strFirstLetter = Left(Jeff, 1)

In the above example, the variable strFirstLetter will store the letter J.

Writing Your Own Functions

We will redesign the program for adding two numbers so that it uses a function instead of a Sub procedure.

1. Create a new project and add 3 text boxes to the form. These should be called txtFirstNumber, txtSecondNumber and txtAnswer. Add a command button called cmdAdd. We are going to create a whizz-bang function procedure for adding together two numbers.

2. Click on View Code on the menu and type in the following Function procedure.

Private Function AddNumbers(intNum1 As Integer, intNum2 As Integer) As Integer
Dim intAnswer As Integer
AddNumbers = intNum1 + intNum2
End Function

Look carefully at the first line of the code. We have to say what type of value is being returned by the function. In this case, it's an integer.

3. The code for the button event procedure is now as follows.

Private Sub cmdAdd_Click()
Dim intFirst As Integer, intSecond As Integer
intFirst = txtFirstNumber.Text
intSecond = txtSecondNumber.Text
txtAnswer.Text = AddNumbers(intFirst, intSecond)
End Sub

Check that your program works.

4. Time to crank things up a little by adding some new functions. Try this one for starters.

Private Function isSmaller(intNum1 As Integer, intNum2 As Integer) As Boolean
If intNum1 lt; intNum2 Then
isSmaller = True
Else
isSmaller = False
End If
End Function

5. Add another button. This one is to tell you which of the two numbers is the larger. The code you will need in this button's event procedure is as follows.

Dim intFirst As Integer, intSecond As Integer
intFirst = txtFirstNumber.Text
intSecond = txtSecondNumber.Text
If isSmaller(intFirst, intSecond) Then
Msgbox "First Number Is Smaller"
Else
MsgBox "Second Number Is Smaller"
End If

An Example Using Strings

Create a new project. Add a textbox to the form and name it txtWord. Add a command button named cmdWordLength.

1. Here is a function to tell you how long a string is.

Private Function howLong(strWord As String) As Integer
howLong = Len(strWord)
End Function

2. This is the event procedure for the command button.

Private Sub cmdWordLength_Click()
MsgBox howLong(txtWord.Text)
End Sub

Check that this code works. It should MessageBox the number of letters in the word(s).

The following function will tell you the first letter in the word that has been entered.

Private Function firstLetter(strWord As String) As String
firstLetter = Left(strWord, 1)
End Function

3. Add another button that will MessageBox the first letter of the word entered.

You can get the last letter of the string by using Right(strWord, 1). Do this as well.

4. The following function will convert the string to Upper Case.

Private Function makeUpper(strWord As String) As String
makeUpper = UCase(strWord)
End Function

Add a button to incorporate this function into the program.

5. You could make a lower case version of the previous function by using LCase instead of UCase. Do it!

6. The following function returns the string with the first letter capitalised. Do something with it.

Private Function firstLetterUCase(strWord As String) As String
Dim strFirstLetter As String
Dim strNew As String
strNew = strWord
strFirstLetter = Left(strWord, 1)
strFirstLetter = UCase(strFirstLetter)
Mid (strNew, 1, 1) = strFirstLetter
firstLetterUCase = strNew
End Function