[AccessD] SubForm and form

jwcolby jwcolby at colbyconsulting.com
Wed Sep 5 12:32:02 CDT 2007


The biggest thing to understand is that because the subform loads first, its
events fire first BEFORE the class for the main form is even loaded.  Thus
the main form is incapable of responding to the OnOpen (for example) of the
subform simply because the main form class is not yet loaded.  This is NOT
true if you use JIT subforms, since in that case the click on the tab causes
the subform to load (and its class) and of course the main form class is
already loaded by then so it can sink the subform events, even the load
events. By and large though it is not the OnOpen or OnLoad of the subform
that you are trying to sink in the main form but rather things like the
AfterUpdate and control events in the subform.

John W. Colby
Colby Consulting
www.ColbyConsulting.com 
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Wednesday, September 05, 2007 12:58 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] SubForm and form

I wasn't talking about the sequence when the subform loads, which happens
before the load of the parent form.  I'm talking about the code behind the
event you're monitoring, like a button click event on the subform.

Charlotte Foust 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Dan Waters
Sent: Wednesday, September 05, 2007 9:50 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] SubForm and form

Thanks Charlotte - that's just what I need.

And I've always seen the subform's code run first - and it always runs twice
for some reason.

Dan

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Wednesday, September 05, 2007 11:28 AM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] SubForm and form

No, what I posted was for an independent popup form.  A subform is a little
different.  The difference would be that you would declare your variable as
a subform and set it to Me.mysubform.Form to give you access to the events
of the subform itself, not just the subform control.  
 
Then, in the IDE dropdown where you see the controls of the parent form, you
will also see your "withevents" subform object, i.e., mySubform.

Dim WithEvents mySubForm As Form 

In the Open or Load event of the parent form, do this:

	Set mySubForm = Me.mysubform.Form

The easiest way to create the shell is to select the mySubForm object in the
controls dropdown and then select the event you want to intercept.
VBA will create the shell with the proper signature and all you need do it
add the code you want to run for that event.  Please note that is you also
have code in the subform for that event, both sets of code will run,
although I don't recall which set runs first.

Charlotte Foust

 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Dan Waters
Sent: Wednesday, September 05, 2007 9:14 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] SubForm and form

Charlotte,

I know I'm dense on this.  

The form you're setting - is that being done in the Main form's module and
myForm is a subform in that main form?

How do I create the 'shell' routines?

Dan

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Wednesday, September 05, 2007 10:46 AM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] SubForm and form

All you need to do is declare a form variable at the module level
WithEvents, like this

Dim WithEvents myForm as Form 

That will give you a myForm object in the list of objects in your form and
if you select myForm from the dropdown list, you'll be able to create shell
routines to handle any of the exposed events of a Form.
So you can have a myForm_Load event, for example, which would contain code
you want to run when myForm is loaded.

Charlotte Foust

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Dan Waters
Sent: Wednesday, September 05, 2007 8:31 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] SubForm and form

John,

Believe it or not, I initially figured that this could be done because I do
try to learn from your postings! :-)

I gave my suggestion simply because that's what I know how to do.

Could you post a short example code of how you would set up the main form's
monitoring of the subform's events?  I sure would like to manage a subform's
load and open events.

Thanks!
Dan

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Wednesday, September 05, 2007 7:43 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] SubForm and form

You can also declare a pointer to the form WithEvents and then sink any of
the form's events in the main form.  Doing that allows you to see the
subforms events from up in the main form and select them.  When you select
one of the events, VBA creates an event handler up in the main form.

Click events and enter/exit events usually do not have a problem but you
should remember that subforms load before the main form's class module so
events like OnOpen and such cannot reference code up in the main form until
the main form's class has finished loading. 


John W. Colby
Colby Consulting
www.ColbyConsulting.com




More information about the AccessD mailing list