Keith Wilby said:
Hi Baz,
I've read your code with interest but being a total newbie at class modules
I'm struggling to understand what it does. I'm reasonably comfortable with
VBA generally - is it fair to say that, as an alternative method, you could
loop through the controls in a stored function by calling it from the form
and passing the form object to the function?
Any chance you could give a numpty explanation of how your code works?
Always keen to lean new methods
Keith.
Hi Keith,
First of all, try thinking of an object created from a class module as being
like any other object, say a form, except it doesn't have a user interface.
When you "instantiate" an object from a class module, you are simply
creating an object, as in the statement:
Set mobjForm = New clsForm
How the object behaves is determined by the code in the class module. Like
any object, it can have properties (Let and Set denoting properties which
can have a value assigned by a caller, Get denoting a property which is
returned to a caller). In this case, clsForm has a Set property which is a
form, so this code says "OK, gimme a new object and tell it that it's Form
is me":
Set mobjForm = New clsForm
Set mobjForm.Form = Me
Within the object, there is a loop which, as I'm sure you can see, finds all
of the text boxes on the supplied form. For every text box, another object
is created, this time based on the class clsTextBox. Because I want all of
these objects to stay in existence at the same time (I don't want them going
out of scope) each one is added to a user-defined collection.
So, what I finish up with is an object of the class clsForm, and within that
a collection of objects of the class clsTextBox, representing all of the
text boxes on the form.
Now the clever bit: if you create an object variable of a specific type
(e.g. TextBox), and declare it "WithEvents", you can "sink" it's events i.e.
as well as, say, the text box's regular GotFocus event procedure, you can
write another procedure (an event sink) which also responds to that event
even though it isn't in the Access form's module. You can see this in the
class module clsTextBox. By this method, I can write a generic event
handler which will fire for ANY text box on a form. It doesn't replace the
regular event (e.g. GotFocus), it runs in addition to it.
I hope that helps. For these kinds of advanced techniques, I can't
recommend highly enough the Access Developers' Handbook and the VBA
Developers' Handbook from Sybex: expensive, but worth every penny.