Suppose we wanted to work with a set of related variables with VBA code. Let's say these variables relate to a person. The information we want to store and process may include the person's name, age and gender. The best way to do this would be to create our own custom person object; with the above-mentioned variables as the object's properties, and any code to process or work with these properties, as the object's methods.
The advantage of creating a custom object is that a lot of related information, and the means to process it, is contained within a referenced entity that is easily accessed and used from different parts of the program.
There are two steps to creating a custom object. The first is to create a blueprint or design for the object which defines what information the object holds and the processing to be done on that information. To do this we create what is known as a Class Module. The second step is to create the actual object based on the class module - this is known as instantiation. Once the class module is in place, we can create as many instances, or objects based on the module, as we need. Hence, we may reuse the code we write in the class module over and over again.
The advantage of creating a custom object is that a lot of related information, and the means to process it, is contained within a referenced entity that is easily accessed and used from different parts of the program.
There are two steps to creating a custom object. The first is to create a blueprint or design for the object which defines what information the object holds and the processing to be done on that information. To do this we create what is known as a Class Module. The second step is to create the actual object based on the class module - this is known as instantiation. Once the class module is in place, we can create as many instances, or objects based on the module, as we need. Hence, we may reuse the code we write in the class module over and over again.
Creating a Class Module
- Open the database in which the class module is to be created.
- Click the CLASS MODULE icon in the MACROS & CODE group of the CREATE ribbon. This opens the VBA editor ready to enter code in our new class module.
- Click SAVE CLASS MODULES in the FILE menu of the VBA editor.
- Give the new class module the name "clsPerson" when the SAVE AS dialog window opens.
Save As dialog window
Although we haven't started to enter code, the new clsPerson Module shows up in the VBA Project in the CLASS MODULES node of the VBA Project Explorer, and in the MODULES section of the main Access Navigation Pane.
VBA Project Explorer |
Access Navigation Pane |
Code to Create an Instance of the Class Module
We will add code to our class module in the following section. Before doing so, however, let's look at how the class module is instantiated - ie how the blueprint becomes an actual object.Secondly, we instantiate the object with the New keyword; and link the object to the variable that we created above with the Set keyword. This is done as follows:
Setting and Getting Class Module Property Values
The person object we are creating has three properties - name, age and gender. Class properties are basically variables with CLASS MODULE level scope. As such, they are declared pretty much the same way as variables in the declarations section of the CLASS MODULE, except we use the PRIVATE keyword instead of DIM. As such, this is the code we need to write:
Private strName As String
Private intAge As Integer
Private strGender As String
Private strName As String
Private intAge As Integer
Private strGender As String
By restricting scope to CLASS MODULE level (as opposed to GLOBAL), the data stored in each variable cannot be accessed directly from outside the class module. This means we must set up a special sort of interface within the class module so we may get and set our object property values after the object has been instantiated. We do this through PROPERTY LET and PROPERTY GET. These are located in the main section of the class module code with it's SUB's and FUNCTIONS. Here are the blocks of code to set and get the NAME property stored in the strName class module variable.
Property Let Name(strNameIn As String)
strName = strNameIn
End Property
Property Get Name() As String
Name = strName
End Property
Let's examine how these work. Imagine we have created an instance of the clsPerson class and called it objPerson. To set the Name property of the object to, let's say, "Sarah", we would use the following code:
objPerson.Name = "Sarah"
Doing this invokes PROPERTY LET NAME in the clsPerson class module code (see code snippet above). The string value "Sarah" is received as the strNameIn argument, and is then assigned to the strName class module variable that we set up in the declarations section above. As such, PROPERTY LET has set the value of a private class module variable from a location external to the class module itself.
Then to retrieve the value of the objects Name property from outside the class module, we just need to reference the object name (eg objPerson) and property (eg Name) in the same way. For example, to retrieve the property value and store it in a variable called strGetName we would use the following code:
strGetName = objPerson.Name
Doing this invokes PROPERTY GET NAME in the clsPerson class module (see code snippet above). PROPERTY GET returns a returns the value of the property by assigning a value to NAME. As such, "Name = strName" assigns the value stored in the strName class module variable to the objects Name property and returns this value whenever the object's Name property is referenced outside of the class module. It works in a similar way to calling a VBA FUNCTION.
The Age and Gender properties in the clsPerson class are set and retrieved in the same way. The screenshot below shows all the class module code to set and get the clsPerson properties:
Class module code for clsPerson |
Adding SUB's and FUNCTION's to the Class Module
Class modules and objects are not just about storing and retrieving property values. As written above, we can also process and work with those property values in all kinds of ways. To do this, we add SUBs and FUNCTIONs to the class module. These are referred to as class methods, and providing they have been created using the PUBLIC keyword (as opposed to PRIVATE), they can be called from outside the class module after the object has been instantiated.We are going to create a simple method to categorize a person based on the information stored in the object properties. When the method is called after instantiation, it will return one of eight categories which describe the person stored in our object. These are:
- Male under 18
- Female under 18
- Male 18 to 30
- Female 18 to 30
- Male 31 to 64
- Female 31 to 64
- Male 65 and over
- Female 65 and over
To do this we shall write a PUBLIC FUNCTION called categorizePerson. Here is the code we shall use:
Public Function categorizePerson()
On Error GoTo myError
Dim strAgeCat As String
If intAge < 18 Then
strAgeCat = "under 18"
ElseIf intAge > 18 And intAge <= 30 Then
strAgeCat = "18 to 30"
ElseIf intAge > 30 And intAge <= 65 Then
strAgeCat = "31 to 64"
ElseIf intAge >= 65 Then
strAgeCat = "65 and over"
End If
categorizePerson = strGender & " " & strAgeCat
leave:
Exit Function
myError:
MsgBox Error$
Resume leave
End Function
If intAge < 18 Then
strAgeCat = "under 18"
ElseIf intAge > 18 And intAge <= 30 Then
strAgeCat = "18 to 30"
ElseIf intAge > 30 And intAge <= 65 Then
strAgeCat = "31 to 64"
ElseIf intAge >= 65 Then
strAgeCat = "65 and over"
End If
categorizePerson = strGender & " " & strAgeCat
leave:
Exit Function
myError:
MsgBox Error$
Resume leave
End Function
As we can see, the method uses IF .. THEN .. ELSEIF statements to select an age category based on the value of the age property stored in the intAge class module variable. The person's gender is then added to the description (thereby doubling the number of potential categories) and returned to the calling code.
The code to invoke the method after the object has been instantiated is as follows:
objPerson.categorizePerson()
Testing the Custom Module
In order to demonstrate how all this works in practice, I have created an unbound Access form to test our clsPerson class. The form allows us to enter the three property values used by clsPerson, and create a new instance of the class containing these values when the SET PERSON PROPERTIES control button is clicked.
After clicking SET PERSON PROPERTIES the btnSet_Click event (shown in the screenshot below) is invoked. It's code creates a new object instance called objPerson using the SET and NEW keywords. The values contained in the unbound textboxes are then referenced and set as the respective Name, Age and Gender properties for the object. Note how the objPerson variable is declared in the declarations section of the form module so it will persist as long as the form is still open or its value is reset to NOTHING when the DELETE PERSON button is clicked thereby invoking the btnDeleteObj_Click event.Once the object has been created, the btnSet button is disabled through the code logic in the setControls sub (see screenshot above). This also enables the other three buttons which let us retrieve the property values, categorize the person, or delete the object.
Clicking the GET PERSON PERSON PROPERTIES button invokes the btnGet_Click event where the property values are retrieved, added to a string variable, and displayed in the OUTPUT textbox (see screenshot below).
Finally, clicking the CATEGORIZE PERSON button invokes code in the btnCat_Click event and the category is displayed in the OUTPUT textbox (see below)
As such, we can see how the properties and methods in clsPerson (ie PROPERTY LET, PROPERTY GET, and CATEGORIZE PERSON) are invoked with statements such as objPerson.categorisePerson after the object has been instantiated in the TEST CUSTOM PERSON OBJECT form. I hope this gives you a good example of how VBA class modules and custom objects work in practice.
No comments:
Post a Comment