Subform to Form Relationship

  • Thread starter Rajesh B. Patel
  • Start date
R

Rajesh B. Patel

Does the Form object within the Subform have a reference back to the subform
object? If so, how can I get that reference inside the Form. For example, I
have the Questions Form inside the QuestionOne Subform. From the OnLoad
event of the Questions Form can I get a reference to the QuestionOne Subform
or at least the name of QuestionOne Subform.

Thanks in advance.
 
M

Marshall Barton

Rajesh said:
Does the Form object within the Subform have a reference back to the subform
object? If so, how can I get that reference inside the Form. For example, I
have the Questions Form inside the QuestionOne Subform. From the OnLoad
event of the Questions Form can I get a reference to the QuestionOne Subform
or at least the name of QuestionOne Subform.

You can't do it in a subform's Load event because subform's
load before the main form so the main form doesn't have the
focus yet. After the main form completes loading, then the
subform can use:
Parent.ActiveControl.Name
to get the name of the subform control displaying the
currently active subform.

You could also do this kind of thing in the main form by
using the subform control's GotFocus event to store its name
in a module level variable.
 
A

Allen Browne

No.

Me.Parent refers to the main form, not the subform control (which is what I
think you are after).

You could loop through the Controls collection of Me.Parent, identifying
those that have a ControlType of acSubform, and comparing their SourceObject
against Me.Name until you get a match. That should work unless have multiple
instances of the same form in different subform controls.

If you are trying to distinguish multiple instances, you could use the Open
event of the main form to set the value of something in each subform which
allows you to tell the difference from within the subforms.
 
R

Rajesh B. Patel

Sorry,

I should have told you what I am trying to do. I have three or more subforms
in different tabs on a single main form. I also use the same subform on a
window that pops up as the result of clicking a button. I want to set the
RecordSource of each of the subforms dynamically. Currently I do this in the
main form. Should I keep it this way? I thought doing it in the Subform's
Form might be a little cleaner, but I would still need information from the
main form to build the query to set the RecordSource so maybe it does not
matter. Thanks for your opinion and your previous replies.
 

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