Single parent form contains multiple instances of the same subform

D

DoDev

Hi,
I need to display multiple instances of the same subform within a single
parent form. Each instance of the subform will display different data.
Thus, the parent form needs to pass an argument (a paramter) to the subform
to indicate what it neeeds to display.

1. The subform query and display is coded with VBA so I don't think I can
use Master/CHild Link fields, unless somehow I would be able to read the
Master/Child fields in the VBA code.

2. I tried to use a tag property for the subform control within the parent
form. So from the subfrom code I can read
Tag = Me.Parent!subfrmControlInstanceA.Form.Tag
Tag = Me.Parent!subfrmControlInstanceB.Form.Tag

However, the above solution assumes that the subform code somehow already
knows that it is executing as instance A or instance B.. So it does not solve
my problem.

3. I could create a separate subform for each invokation instance (brute
force solution) but such solution would entail the pain of maintaining the
code for multiple subforms.

Do you have any ideas how the parent form can pass a different parameter to
each instance of the subform?

Any help wil be very appreciated.
- DoDev
 
D

Douglas J. Steele

Not sure I understand what you're trying to do, but there shouldn't be an
issue reading the LinkMasterFields/LinkChildFields using VBA.

Remember that they're properties of the subform control on the parent form,
so you'd need to use something like

Forms!NameOfParentForm!NameOfSubformControl.LinkChildFields

and

Forms!NameOfParentForm!NameOfSubformControl.LinkMasterFields
 
M

Marshall Barton

DoDev said:
I need to display multiple instances of the same subform within a single
parent form. Each instance of the subform will display different data.
Thus, the parent form needs to pass an argument (a paramter) to the subform
to indicate what it neeeds to display.

1. The subform query and display is coded with VBA so I don't think I can
use Master/CHild Link fields, unless somehow I would be able to read the
Master/Child fields in the VBA code.

2. I tried to use a tag property for the subform control within the parent
form. So from the subfrom code I can read
Tag = Me.Parent!subfrmControlInstanceA.Form.Tag
Tag = Me.Parent!subfrmControlInstanceB.Form.Tag

However, the above solution assumes that the subform code somehow already
knows that it is executing as instance A or instance B.. So it does not solve
my problem.

3. I could create a separate subform for each invokation instance (brute
force solution) but such solution would entail the pain of maintaining the
code for multiple subforms.

Do you have any ideas how the parent form can pass a different parameter to
each instance of the subform?


You don't need an argument for a subform to know which
subform control to know which instance it is. You could do
it using this kind of code:

Select Case Parent.ActiveControl.Name
Case "subfrmControlInstanceA:
,,,
Case "subfrmControlInstanceB"
...
End Select

But, when you say you need "Each instance of the subform
will display different data", I start to hear alarm bells
about un normalized table designs. Maybe you should think
about that and if that's the case, spend your energy fixing
the fundamental problem instead of dealing with workarounds
all over the place.

BTW you can also get to the subform control link properties
using ActiveControl.
 
D

DoDev

Marsh,

Think of an example in which each subform instance displays a different set
of table records, however all the records do have the same layout. For
exmaple, each subform could display invoices for a different month.

Unfortunately, I am afraid that the activecontrol approach does not work for
me. I want to populate the data before subforms are displayed on the screen.

Thanks for your help.

DoDev
 
D

DoDev

Doug,
The trouble is that when the subform is executing I don't know the name of
its control within the parent form. That's exactly what I am trying to find
out. If I somehow could find the name of the control containing the subform
then I would know what specific parameters I ashould apply to the particular
instance of the subform.

I have multiple instances of the same subform code executing within the same
parent form. I would like each instance to choose a different set of records
for display. The choice could be made based on the name of the
subformcontrol, or tag or any other way which would enable me to pass
parameters to the generic subform code.

Thanks a lot for your help.

Regards.
DoDev
 
D

Douglas J. Steele

Can't you control it from the parent form, in which case you'd know which
subform is which?
 
M

Marshall Barton

DoDev said:
Think of an example in which each subform instance displays a different set
of table records, however all the records do have the same layout. For
exmaple, each subform could display invoices for a different month.

Unfortunately, I am afraid that the activecontrol approach does not work for
me. I want to populate the data before subforms are displayed on the screen.


Either that is a poor example or you can filter each subform
to a different month by having three main form text boxes
with different month dates as the LinkMaster.

The ActiveControl approach could also do it by using code in
the subform's Load event to set the subform's Filter
property.
 
D

DoDev

Doug,

The challenge was that i want to know from within the subform code which
control contains the subform.

Dirk Goldgar suggested the following solution:

Dim ctl As Control
Dim TheTag As String

For Each ctl In Me.Parent.Controls
If ctl.ControlType = acSubform Then
If ctl.Form Is Me Then
TheTag = ctl.Tag
Exit For
End If
End If
Next ctl

Debug.Print TheTag

It meets my needs.

Thanks a lot for your help.

DoDev
 
D

DoDev

Marsh,
Perhaps it was not the best example. I was just trying to use an example to
alliviate your concerns about the unormalized table designs.

Anyway, the problem is solved.

Thanks a lot for your help.
DoDev
 

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