A combo control wrapper for the framework

In this post I will add a combo wrapper class and tie it in to the form.  This won’t be fancy, the only thing it will do for now is the back color change.

Create a new access container called clsCtlCboDemo.  Import the form and classes from the database clsFrmWrapperDemo.  We will start from there since it is so easy to do.

Insert a new class.  Immediately save it as clsCtlCbo.  Into that class insert the following code:

Option Compare Database
Option Explicit

‘clsCtlCbo

Dim WithEvents mcbo As ComboBox
Const cnEvProc As String = “[Event Procedure]”
Dim lngBackColor As Long    ‘A place to store the original back color so it can be restored when done

Private Sub Class_Terminate()
‘MsgBox “unload class for: ” & mcbo.Name
Set mcbo = Nothing
End Sub

Function mInit(lcbo As ComboBox)
Set mcbo = lcbo
lngBackColor = mcbo.BackColor   ‘Save the back color of the control
‘MsgBox “Init the class for: ” & mcbo.Name

‘The text string “[Event Procedure]” placed into any event property
’causes Access to begin raising events for that event property

mcbo.OnGotFocus = cnEvProc ‘Cause Access to start raising this event
mcbo.OnLostFocus = cnEvProc  ‘Cause Access to start raising this event

End Function

Private Sub mcbo_GotFocus()
mcbo.BackColor = vbCyan         ‘I hard coded this to make it simple.  You can use some method to pass this in
End Sub

Private Sub mcbo_LostFocus()
mcbo.BackColor = lngBackColor   ‘Restore the back color as we leave
End Sub

Next open clsFrm.  Replace mFindControls() with the following, saving immediately.


‘Iterate the form’s control collection looking at various control types

Private Function mFindControls()
Dim ctl As Control
For Each ctl In mFrm.Controls
Select Case ctl.ControlType

‘If we find any text boxes, load a text box wrapper class
Case acTextBox
‘Create a text box wrapper variable
Dim cCtlTxt As clsCtlTxt
‘Instantiate it
Set cCtlTxt = New clsCtlTxt
‘Pass in a pointer to the text box to the class
cCtlTxt.mInit ctl
‘Store the class pointer to the collection
mcolClsCtls.Add cCtlTxt, ctl.Name
Case acComboBox
‘Create a combo box wrapper variable
Dim cCtlCbo As clsCtlCbo
‘Instantiate it
Set cCtlCbo = New clsCtlCbo
‘Pass in a pointer to the combo box to the class
cCtlCbo.mInit ctl
‘Store the class pointer to the collection
mcolClsCtls.Add cCtlCbo, ctl.Name

Case acCheckBox
Case acListBox
Case Else
End Select
Next ctl
End Function

Notice that I have fleshed out the case acComboBox but that it does exactly the same thing as the case acTextBox.

Case acComboBox
‘Create a combo box wrapper variable
Dim cCtlCbo As clsCtlCbo
‘Instantiate it
Set cCtlCbo = New clsCtlCbo
‘Pass in a pointer to the combo box to the class
cCtlCbo.mInit ctl
‘Store the class pointer to the collection
mcolClsCtls.Add cCtlCbo, ctl.Name

It dimensions a clsCtlCbo, instantiates it (Set command), calls mInit passing in the control itself, and then adds the class instance to the collection, keyed on the control name.
Open the form in design view.  Add three combos to the form.  Notice that my code no longer cares about their names.  If you want to name them something specific fine, or just leave them with whatever Access called them.  Save the form and open it.
Tab down through the controls.  The text boxes and the combos should all change back color as they get the focus.  One thing I am trying to impress is that this is not magic, no smoke or mirrors.  Just easy to do stuff.  When I created the clsCtlCbo I simply copied the contents of the clsCtlTxt and changed txt to cbo wherever I needed to.  When I created case acComboBox I simply copied the case acTextBox and changed txt to cbo.  Dead simple and in five minutes I had a new combo wrapper class and it was hooked up and functioning.
Please be patient, it will get better.  I am intentionally keeping the behaviors simple so that you can see the structure, the basics without getting bogged down in too much detail.  The important thing to understand is that we now have a wrapper class for two Access controls and the form.  Each class acts as a container to hold code and data required to make some specific thing happen.  What that thing is depends on my needs.  It can be as simple as changing the back color to something as complex as opening a form when I double click the combo.
BTW you should set a break point in the form’s Open event and step through the code.  It is critical to see how everything initializes, the sequence and so forth.  As a programmer, you want to really understand this stuff, not just copy code out of a blog and watch it run.  Set a breakpoint in the clsFrm’s Close event sink.
Step through the code as the form closes.  All the way through, from the event sink grabbing control to the collections being set to nothing which causes the objects in the collections to unload.  Step through it all down to the form actually closing from view.I can tell you from memory the sequence of events, and you should strive to get to that point.Next up, that combo box double click behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *