Generic reference to form's control containing a subform

D

DoDev

Hi,

From within subform's code I would like to access the tag property of the
parent's form control which contains the subfrom.

I know how to do it when the subform code knows the exact name of the
control which contains the subform:
Tag = Me.Parent!subformContainerControlName.Tag

However, in my case the same subform can be included in many different
subformContainer controls. So I woulld like to have a generic code in which
I refer to the subformContainer control wihtout using its specific name.
Similarly to how I can refer to a parent form (e.g. me.Parent) without using
the name of the parent form.

Thanks for your help.

DoDev
 
R

RoyVidar

DoDev said:
Hi,

From within subform's code I would like to access the tag property of
the parent's form control which contains the subfrom.

I know how to do it when the subform code knows the exact name of the
control which contains the subform:
Tag = Me.Parent!subformContainerControlName.Tag

However, in my case the same subform can be included in many
different subformContainer controls. So I woulld like to have a
generic code in which I refer to the subformContainer control
wihtout using its specific name. Similarly to how I can refer to a
parent form (e.g. me.Parent) without using the name of the parent
form.

Thanks for your help.

DoDev

Try something like

TheTag = Me.Parent.Controls(Me.Name).Tag
 
M

Marshall Barton

DoDev said:
From within subform's code I would like to access the tag property of the
parent's form control which contains the subfrom.

I know how to do it when the subform code knows the exact name of the
control which contains the subform:
Tag = Me.Parent!subformContainerControlName.Tag

However, in my case the same subform can be included in many different
subformContainer controls. So I woulld like to have a generic code in which
I refer to the subformContainer control wihtout using its specific name.
Similarly to how I can refer to a parent form (e.g. me.Parent) without using
the name of the parent form.


Try using:

Parent.ActiveControl.Tag
 
R

RoyVidar

RoyVidar said:
Try something like

TheTag = Me.Parent.Controls(Me.Name).Tag

On second thought, that won't work, hmm, try something along the lines
of following

Dim sf As Access.Control
Dim f As Access.Form
Dim TheTag As String

Set f = Me.Parent
For Each sf In f.Controls
If sf.ControlType = acSubform Then
If sf.SourceObject = Me.Name Then
TheTag = sf.Tag
End If
End If
Next sf

Debug.Print TheTag

Set sf = Nothing
Set f = Nothing

I e, loop the controls, find the subforms, test if it is the correct
subform by checking it's source object property, then access the
tag.
 
D

DoDev

Marshall,
Thanks for the advice.

I tried it but I get the following error: "The expression you entered
requires the control to be in the active window"

BTW, I am trying to read the tag in the subform's code which handles the
"load event". I want to use the tag to determine what info should be
displayed in the subform.

Regards.

DoDev
 
D

DoDev

Roy,
Thanks for trying. Unfortunately, it did not solve my problem.

The code you suggested works but it makes me retrieve multiple tags, i.e.
the tags stored in every control containing my generic subform. This is
becuase Me.Name of the subfrom is the same for all the instances of the
subform.

Any other ideas?

Regards.

DoDev
 
D

Dirk Goldgar

DoDev said:
Roy,
Thanks for trying. Unfortunately, it did not solve my problem.

The code you suggested works but it makes me retrieve multiple tags, i.e.
the tags stored in every control containing my generic subform. This is
becuase Me.Name of the subfrom is the same for all the instances of the
subform.

Any other ideas?


A variation on Roy's code will work, though. Try this:

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

You don't have to go to this length if the subform in question is the parent
form's ActiveControl, but if you can't rely on that, the above code ought to
work.
 
D

DoDev

Dirk,

Your are correct. Your solution does indeed work.
Thanks so much.

Regards.

DoDev
 
M

Marshall Barton

DoDev said:
I tried it but I get the following error: "The expression you entered
requires the control to be in the active window"

BTW, I am trying to read the tag in the subform's code which handles the
"load event". I want to use the tag to determine what info should be
displayed in the subform.


I knew dat :-\
Unlike reports, subforms are loaded before the main form
does anything so the main form does not have an active
control until much later.

Good thing Dirk's idea deals with your situation ;-)
 
D

DoDev

Thanks for your help.
DoDev

Marshall Barton said:
I knew dat :-\
Unlike reports, subforms are loaded before the main form
does anything so the main form does not have an active
control until much later.

Good thing Dirk's idea deals with your situation ;-)
 

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