[AccessD] LOAD THE FORM CLASS IN THE FORM

jwcolby jwcolby at colbyconsulting.com
Mon Feb 9 13:22:57 CST 2009


This lecture will discuss how to get an instance of a class to load.  Remember that (In VBA) unlike 
a module, a class can’t do anything until an instance of it is loaded.  Also, unlike a module, you 
can load more than one instance of a class.  For the purposes of this lecture we will cause the form 
that we designed earlier to load one instance of this class.

•	Open frmDemoCtls in design view.
•	Click the Code icon in the toolbar to open the editor for the form’s class module.
•	In the form header type (or cut and paste) the following:

Public fclsFrm As clsFrm

Private Sub Form_Open(Cancel As Integer)
     Set fclsFrm = New clsFrm
     fclsFrm.mInit Me
End Sub

•	Save the form.

WARNING!!! If you TYPED this in then the code will probably NOT RUN.  Why?  Because the OnOpen 
property of the form will not contain the string "[Event Procedure]".  Remember that I said that the 
event property of an object MUST CONTAIN that exact string in order for the event sink to be 
executed in the object, the form in this case.  If that is the case you can do one of two things:

1 Open the form in design view and DOUBLE CLICK the OnOpen event of the form.  The fomr wizard will 
insert the string [Event Procedure] in the OnOpen property of the form.

2) Cut and paste the entire thing out and back in to the form class.

Private Sub Form_Open(Cancel As Integer)
     Set fclsFrm = New clsFrm
     fclsFrm.mInit Me
End Sub

Somehow or another this forces the form wizard to place that text into the property.

So... this code dimensions a variable fclsFrm. I use the fcls to denote a class in a form header, it 
is not required.  You could use lclsFrm, mclsFrm or whatever you like.

The Form_Open event will run when the form opens.  The Set statement is where an instance of the 
clsFrm is loaded.  The .Minit Me calls the mInit method of the class and passes in to the clsFrm 
instance a pointer to the containing form (Me).

•	Place a breakpoint on the Set statement and open the form.
•	Step through the code.  You should see the set statement execute, then the .Minit will pull you 
into the class mInit method where the two statements inside of the class will execute.

Pretty exciting eh?  NOT!

But we are breaking this stuff down into tiny steps so that you can see each piece and how easy each 
piece is.  This lecture has set up the code in your form’s class to dimension a fclsFrm variable, 
then when you open the form, the Form_Open sub executes, loads a single instance of the class, and 
calls the mInit() method, passing in a pointer to itself (the containing form, ME).  The class 
loaded, the pointer to the form was stored INSIDE of the class instance, a property of the form was 
loaded with a string, and the code stepped back out to the form’s code and finished running.

In general, these three steps have to be performed any time you want to use a class.

•	Dim an instance of the slass
•	SET the variable to the class (load an instance)
•	Call an mInit() method of the class to pass in parameters to the class to initialize itself.  VERY 
occasionally you will not need to Init a class but that is extremely rare.



-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list