jwcolby
jwcolby at colbyconsulting.com
Mon Feb 9 19:33:16 CST 2009
In this lecture we will build a class to hold a control. The problem is that while we can build a
class for a generic control, we can’t sink events from generic controls because VBA has no idea what
kinds of event any class might raise. Thus the class has to be for a specific type of control, for
example a combo box or a text box.
• From the database window, click Insert / Class.
• Immediately save the new class as clsCtlCbo
• Place the following code in the header of the class
Private WithEvents mctlCbo As ComboBox
Private Const cstrEvProc As String = "[Event Procedure]"
This should be starting to look familiar. The first line dimensions a variable for a combo control,
and dimensions it WithEvents, which means that this class will sink events for the control stored in
that variable. cstrEvProc is already familiar and is the text which when placed in an event
property “hooks” that event and causes the control to Raise that event.
• In the left combo box select the class. The editor will insert an Initialize event stub. We will
not use the Initialize event at this time.
• In the right combo box, select the Terminate event. The editor will insert a Terminate event stub.
• In the Terminate event stub insert the following code:
Private Sub Class_Initialize()
Set mctlCbo = Nothing
End Sub
• Again, this code should be looking familiar. We are telling the class that when it terminates, to
clean up the pointer to the combo box.
• Create a mInit() function as follows:
Function mInit(lctlCbo As ComboBox)
Set mctlCbo = lctlCbo
mctlCbo.BeforeUpdate = cstrEvProc
mctlCbo.AfterUpdate = cstrEvProc
mctlCbo.OnGotFocus = cstrEvProc
mctlCbo.OnLostFocus = cstrEvProc
End Function
This mInit passes in a pointer to a control in lctlCbo. It then saves that pointer to mctlCbo,
dimensioned up in the class header. Finally it “hooks” four events of the combo, the BeforeUpdate,
AfterUpdate, GotFocus and LostFocus.
• In the left combo box in the editor, select mctlCbo. The editor will create the BeforeUpdate
event stub. In the right combo select the AfterUpdate event and the editor will create an event
stub for that event. Repeat for the GotFocus and LostFocus.
• In these events place the following code:
Private Sub mctlCbo_AfterUpdate()
MsgBox "AfterUpdate: " & mctlCbo.Name
End Sub
Private Sub mctlCbo_BeforeUpdate(Cancel As Integer)
MsgBox "BeforeUpdate: " & mctlCbo.Name
End Sub
Private Sub mctlCbo_GotFocus()
MsgBox "GotFocus: " & mctlCbo.Name
End Sub
Private Sub mctlCbo_LostFocus()
MsgBox "LostFocus: " & mctlCbo.Name
End Sub
• Save and close clsCtlCbo.
This lecture has created a new clsCtlCbo. We are starting to see a pattern emerge, where we have a
private variable in the top of the class dimmed WithEvents to hold a pointer to an object, in this
case a combo control. We have a Terminate event of the class that cleans up the pointer when the
class closes. We have an mInit() event that passes in a pointer to the object, sets the local
variable equal to the passed in pointer, and then “hooks” events of the object passed in by setting
the properties to a specific string "[Event Procedure]" by using a constant declared in the class
header. We then have event sink subs which will execute when the given event fires. At this time
all that the event sinks will do is pop up a messagebox but that is sufficient to varify that the
events are firing.
--
John W. Colby
www.ColbyConsulting.com