What is the best way to interact with a subform?

E

Edo2008

I'm finding that the form_load procedures of the subforms execute
before the parent's. Interesting fact.

I'd like to call a function in a subform whenever there is an event
on the parent form. Is this possible?
Also, I'd like the subform to reference a variable of its parent form.
is this possible?

In another post I was attemping to use a seperate class module with a
public variable which has a Get function and Set subroutine associated
with it (as per a book I read) but have not been able to get that to
work either :).


Thanks
-Ed
 
B

bcap

Yep, subforms load before their parent.

Let's suppose that a button on the parent form is clicked. Here's how to
call a function in a subform:

Private Sub cmdSomeButton_Click()

MySubform.Form.SomeFunction

End Sub

This requires that the subform function is declared as Public. If you don't
want it to be public, the subform will need to "sink" the parent form's
event in order to call the function itself. This is more complicated, and
is a concept that some people seem to struggle with. Plus, it requires the
subform to reference it's parent, something I don't like to do because, IMO,
it compromises re-usability and maintainabilty by making the subform
dependent on a particular parent form.

The second part of your question can't be done without doing exactly that
(having the subform reference it's parent). Once you accept that this is
what you are going to do, the easiest way is to (i) declare the variable as
public at module level, and (ii) refer to it like this:

Me.Parent.SomeVariable

The public variable is both readable and writable. If you don't want this,
then declare it as private and implement a Property Get or a Property Let
(or Set) procedure in the parent form as appropriate.
 
E

Edo2008

This is what I expected... but my subform is withint a TAB of a
form. In other words, my parent form as 4 tabs. Of of the tabs
contains a subform. I cannot find the path down :).

And I agree that making the child dependant on the parent is not good
from a reusability standpoint so I will avoid it. Thanks for the tip.
-Ed
 
E

Edo2008

I've read on MSDN, and confirmed thru debug, that all controls on the
tab controls are member of the form's controls so I should not have a
problem just because my subform is in a tabControl.

From the form, I still cannot access a public function in the
subform... Member not found. Help??

-Ed
 
B

bcap

Yes that's right, the tab control is irrelevant when referencing the
controls it contains.

Please post the code for the function, and the code where the error occurs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top