[AccessD] LOAD THE FORM CLASS IN THE FORM

McGillivray, Don [IT] Donald.A.McGillivray at sprint.com
Mon Feb 9 13:50:33 CST 2009


Hi, John

First of all, thanks for stepping through this (once again) for us duffers out here.  I've downloaded and read all your stuff about classes and sinking events, but never have taken the time to thoroughly understand them or quite get my head around how I might implement them in the stuff I do.  This discussion looks to be a bit more elementary in approach, and I'm following it closely, hoping that something clicks this time.

Now a bief question . . .

Why declare the variable fclsFrm as "public" in the header of the form's module?  Is this just a standard practice thing - declare things public unless there's a compelling reason not to, or something else?

Thanks

Don

-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Monday, February 09, 2009 11:23 AM
To: Access Developers discussion and problem solving
Subject: [AccessD] LOAD THE FORM CLASS IN THE FORM


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
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


This e-mail may contain Sprint Nextel Company proprietary information intended for the sole use of the recipient(s). Any use by others is prohibited. If you are not the intended recipient, please contact the sender and delete all copies of the message.





More information about the AccessD mailing list