Class Modules

Class modules are special modules within VBA that allow you to build code objects that have all the abilities of built-in Access objects: methods, properties, multiple instances, and data persistence. These special capabilities of class modules are interesting, but they are rarely needed in normal application programming. However, if you're familiar with class modules, you'll find times when they come in very handy. One reason to use class modules is their ability to remember data across multiple times they are called. This can also be done using the Static keyword when dimensioning variables in regular modules, or by using Global variables, but using a class module is a cleaner way.

A related benefit to this great memory that class modules have is that you can set multiple properties of an instance of a class, then ask it to do something with a method. These sound very technical, but in reality a class module uses a property Let procedure to merely remember a value in a variable. And a method is really just a procedure that does something—similar to the Subs we've already covered. The best way to show how class modules work is with a simple example.

Class Module Example: Using a Multilevel Hourglass

This is a very simple example, but one that you might actually need to use sometime. If you have time-consuming processing in your application, you may want to turn on the Hourglass so that the user knows that you're working on something and they should wait patiently.

However, sometimes multiple time-consuming functions and subs have to run in succession or in a nested structure, and sometimes one procedure is run without the others. Keeping track of the Hourglass status can be difficult in these cases. You need to be sure that it is on while the processing is occurring, but also that it is turned off at the end.

A good approach is to keep track of how "on" the Hourglass is. Every time a procedure needs the Hourglass, it increases the hourglass level—1 means that one procedure turned it on, 2 means that two nested procedures have turned it on, and so on. As each procedure finishes, it decrements the level by 1. When the hourglass level reaches 0, the Hourglass itself is turned off.

This class module, called MultiLevelHourglass, demonstrates how you can use class modules to remember the state of an object (in this case, the hourglass level) and manipulate it over time. It consists of three procedures:

□ TurnOn—This increments the hourglass level and turns the Hourglass on, if necessary

□ TurnOff—This decrements the hourglass level and turns the Hourglass off if necessary, plus an override parameter to force the Hourglass off regardless of level

□ IsOn—This is a property that returns whether the Hourglass is currently on

Here's the code from the class module called MultiLevelHourglass. First, note the local module level variable. This keeps track of the current level. Because this is a class module, this variable's value will be retained across multiple uses of the object.

Option Compare Database Option Explicit

Private mintHourglassLevel As Integer

The following is the simple property, called a Property Get in a class module. It merely sets the IsOn property to True if the level is greater than 0 or False if it isn't.

Public Property Get IsOn() As Boolean

IsOn = (mintHourglassLevel > 0) End Property

The following method turns the Hourglass on, or to be more accurate, increases the level of the Hourglass. If the hourglass level becomes 1 during a call to this procedure, the Access Hourglass itself is turned on. If the level is already greater than 1, there's no need, because the Access Hourglass is already on.

Public Function TurnOn()

On Error GoTo Error_Handler

'Increment the hourglass level. mintHourglassLevel = mintHourglassLevel + 1 'Turn on the hourglass if the level is exactly 1 If mintHourglassLevel = 1 Then

DoCmd.Hourglass True End If

Exit_Procedure:

Exit Function Error_Handler:

MsgBox "An error has occurred in this application. " _ & "Please contact your technical support person and tell them this information:" _

& vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err.Description, _

Buttons:=vbCritical, title:="My Application" Resume Exit_Procedure Resume End Function

Finally, here is the method that lowers the hourglass level, and actually turns off the Hourglass if level 0 is reached. Note the optional parameter to force the Hourglass off. This is really just a safety valve—you shouldn't really need it if each procedure you run is consistent about decreasing the level using TurnOff when it is finished. However, if you know that you are at the end of a long-running process and you want to be sure, you can use ForceOff:=True to ensure that the Hourglass isn't stuck on.

Public Function TurnOff(Optional ForceOff As Boolean) On Error GoTo Error_Handler

'Decrement the hourglass level. mintHourglassLevel = mintHourglassLevel - 1

'Turn off the hourglass if the level is less than or equal to zero 'or if it is being forced off regardless of level If mintHourglassLevel <= 0 Or ForceOff Then mintHourglassLevel = 0 DoCmd.Hourglass False End If

Exit_Procedure:

Exit Function Error_Handler:

MsgBox "An error has occurred in this application. " _ & "Please contact your technical support person and tell them this information:" _

& vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err.Description, _

Buttons:=vbCritical, title:="My Application" Resume Exit_Procedure Resume End Function

That's it for the class module called MultiLevelHourglass. Now all we need is some code to test it out.

Option Compare Database

Global objHourGlass As New MultiLevelHourglass

Public Sub HourglassTest()

objHourGlass.TurnOn

'lots of time consuming code here

objHourGlass.TurnOn

objHourGlass.TurnOn

objHourGlass.TurnOff

objHourGlass.TurnOff

'any called functions should also use .TurnOn and

.TurnOff

Debug.Print objHourGlass.IsOn 'just for debugging

purposes

objHourGlass.TurnOff ForceOff:=True 'all done

End Sub

If you want to use this technique, you can call objHourGlass.TurnOn and objHourGlass.TurnOff to control the Hourglass throughout your application. You'll need to make sure that no other procedures are controlling the Hourglass directly by using DoCmd.Hourglass, because that would be circumventing this multilevel approach.

0 0

Post a comment