Control Parent

J

JMS

The parent of a control may be another control, a subform, a form, maybe something else. Is there anything that will definitively deliver the parent type? That is, without a searching through the various lineages looking for a NAME match or using ON ERROR handling to back out of the possible, but incorrect types?
 
T

Tim Ferguson

The parent of a control may be another control, a subform, a form,
maybe something else. Is there anything that will definitively
deliver the parent type?

I haven't tried this, but it might work

Dim obj as object

Set obj = mycontrol.parent

Select case typename(obj)
case "form"

case "option group"

case else

end select

HTH


Tim F
 
J

JMS

Tim
I couldn't get your suggestion to work, but I'm no less appreciative of your effort to help

----- Tim Ferguson wrote: ----

The parent of a control may be another control, a subform, a form
maybe something else. Is there anything that will definitivel
deliver the parent type?

I haven't tried this, but it might wor

Dim obj as objec

Set obj = mycontrol.paren

Select case typename(obj
case "form

case "option group

case els

end selec

HT


Tim
 
J

JMS

TypeOf makes sense to me (even though I can't get this to work either). Is it because I have never grasped some inherent benefit of OOP that the name of the parent is sufficient to determine the class type?
 
T

TC

Sorry, the text of the post to which you have replied, is no longer on my
newsserver. But from the reference to TypeOf, I think that it was one of
mine.

What are you actually asking?

TC


JMS said:
TypeOf makes sense to me (even though I can't get this to work either).
Is it because I have never grasped some inherent benefit of OOP that the
name of the parent is sufficient to determine the class type?
 
T

TC

I don't understand what you're trying to do. If the control is in the
application that is running the code, you'd use the win32 FindWindow API to
locate your >application's< window handle, no? And if the control is >not<
in the application that is running the code, you're probably out of luck,
because many Access controls do not have a window handle (hWnd) unless they
actually have the focus.

What on earth are you actually trying to do? :)

TC
(off for the day)


JMS said:
TC,
My original question is at the top of this thread. Rephrased, my problem
is as follows: I would like to generically chase the lineage of a control
back to the desktop. Best I can tell, the only thing that Access can
reliably deliver is ctrl.Parent.Name. But the name of what? Is it another
control? subform? form? report? I don't really want to set up loops for the
various objects looking for names that match. And I would prefer not to
define a bunch of objects, set each obj = ctrl.Parent, and concede that the
best I could do was execute code until either I guessed correctly or Access
just got tired of bothering with me. Anything of the form TypeOf
(ctrl.Parent) or ctrl.Parent.Type would be absolutely wonderful.
 
T

TC

Um, perhaps I misunderstanmd what you want. You have an active control of
some kind, & you want to get something like:

the parent of this control is: tab page ABC,
whose parent is subform DEF,
whose parent is main form GHI,
& so on; correct?

If that is so, ctl.Parent is the way to go. That gives you a reference to
the parent "thing" of the control. Of course, that "thing" might be another
control, a form, or whatever. TypeOf should give you more information about
that. I don't have Access here to check, but I believe you would need
TypeOf(ctl.parent) - not TypeOf(ctl.parent.NAME).

But you will probably come to grief when you get into subforms. There is no
way, AFAIK, for code tat is executing within a sunform module, to accurately
determine the name of the relevant paent form's subform control. You can
check all the parent form's subform control's recordsources, looking for a
match - but this doesn;'t work if the parent form has >several< subform
controls that all refer to the same subform; and there are various realistic
cases were this can occur.

Hope these random ramblings help!
TC
(off for the day)
 
J

JMS

TC
My original question is at the top of this thread. Rephrased, my problem is as follows: I would like to generically chase the lineage of a control back to the desktop. Best I can tell, the only thing that Access can reliably deliver is ctrl.Parent.Name. But the name of what? Is it another control? subform? form? report? I don't really want to set up loops for the various objects looking for names that match. And I would prefer not to define a bunch of objects, set each obj = ctrl.Parent, and concede that the best I could do was execute code until either I guessed correctly or Access just got tired of bothering with me. Anything of the form TypeOf (ctrl.Parent) or ctrl.Parent.Type would be absolutely wonderful.
 
W

Wayne Morgan

Probably the easiest way to solve your problem is to use naming conventions.
Have all your forms start with "frm" as in frmFormName. Now, when you get
the name, you'll know what you have. You can find a list of recommended
naming conventions at

http://www.mvps.org/access/general/gen0012.htm
http://www.mvps.org/access/tencommandments.htm

--
Wayne Morgan
Microsoft Access MVP


JMS said:
TC,
My original question is at the top of this thread. Rephrased, my problem
is as follows: I would like to generically chase the lineage of a control
back to the desktop. Best I can tell, the only thing that Access can
reliably deliver is ctrl.Parent.Name. But the name of what? Is it another
control? subform? form? report? I don't really want to set up loops for the
various objects looking for names that match. And I would prefer not to
define a bunch of objects, set each obj = ctrl.Parent, and concede that the
best I could do was execute code until either I guessed correctly or Access
just got tired of bothering with me. Anything of the form TypeOf
(ctrl.Parent) or ctrl.Parent.Type would be absolutely wonderful.
 
J

JMS

Wayne
I have to leave town in a bit for a few days. When I return on Tuesday, I'll check out the links you provided in detail. It does seem strange to me that the naming conventions will suggest that I (or the users who I hope to have using my application) will always have to know in advance if a control will be placed in a group or a frame before naming it, or to what level (used to be 3, now is 7?) that a form might be nested as a subform before naming that (sub)form, etc

----- Wayne Morgan wrote: ----

Probably the easiest way to solve your problem is to use naming conventions
Have all your forms start with "frm" as in frmFormName. Now, when you ge
the name, you'll know what you have. You can find a list of recommende
naming conventions a

http://www.mvps.org/access/general/gen0012.ht
http://www.mvps.org/access/tencommandments.ht

--
Wayne Morga
Microsoft Access MV


JMS said:
TC
My original question is at the top of this thread. Rephrased, my proble
is as follows: I would like to generically chase the lineage of a contro
back to the desktop. Best I can tell, the only thing that Access ca
reliably deliver is ctrl.Parent.Name. But the name of what? Is it anothe
control? subform? form? report? I don't really want to set up loops for th
various objects looking for names that match. And I would prefer not t
define a bunch of objects, set each obj = ctrl.Parent, and concede that th
best I could do was execute code until either I guessed correctly or Acces
just got tired of bothering with me. Anything of the form TypeO
(ctrl.Parent) or ctrl.Parent.Type would be absolutely wonderful
 
T

Tim Ferguson

I would like to generically chase the lineage of a control back to
the desktop.

This started out sounding like a sensible question, but now I am not so
sure... still:
Best I can tell, the only thing that Access can
reliably deliver is ctrl.Parent.Name. But the name of what? Is it
another control? subform? form? report?

I suggested way upthread the use of TypeName, and you simply responded that
you could not get it to work but not what was wrong. For the record, this
works as predicted for me:

Private Sub Detail_Click()
Dim strPT As String
Dim obj As Object

Set obj = Me.ActiveControl.Parent
strPT = TypeName(obj)

MsgBox strPT

End Sub

This returns a string like "Form_frmNewTemp" for a control on the detail
portion of the form, or "Page" on a page on a tab control -- it's as far as
I have tested but there are very limited use for a such a function beyond
that.

Best wishes


Tim F
 
T

TC

Tim Ferguson said:
This started out sounding like a sensible question, but now I am not so
sure... still:


I suggested way upthread the use of TypeName, and you simply responded that
you could not get it to work but not what was wrong. For the record, this
works as predicted for me:

Private Sub Detail_Click()
Dim strPT As String
Dim obj As Object

Set obj = Me.ActiveControl.Parent
strPT = TypeName(obj)

MsgBox strPT

End Sub

This returns a string like "Form_frmNewTemp" for a control on the detail
portion of the form, or "Page" on a page on a tab control -- it's as far as
I have tested but there are very limited use for a such a function beyond
that.

But he wants to trace the control back to its parent, then that parent's
parent & so on, until he reaches a top-level object (ie. an object with no
parent). IMO there is no way to do this in all situations.

Cheers,
TC
 
T

Tim Ferguson

TC said:
But he wants to trace the control back to its parent, then that
parent's parent & so on, until he reaches a top-level object (ie. an
object with no parent). IMO there is no way to do this in all
situations.

You can create a chain quite happily like this:

Textbox ->
Page ->
TabControl ->
Form_frmNewTemp

but accessing the form's parent property causes a trappable error. I am not
quite sure what anybody would expect the parent of a form to be, and that
is what I don't think is sensible -- "tracking it back to the desktop"??

Perhaps the OP would like to comment further...

All the best


Tim F
 
T

TC

Tim Ferguson said:
You can create a chain quite happily like this:

Textbox ->
Page ->
TabControl ->
Form_frmNewTemp

Not when a subform wants to get know what subform control it came from in
the parent form!

Try this experiement. Create a main form frmA. Add three subform controls
sf1, sf2, sf3. Put the same subform frmB into each of those controls. Now
write code in frmB's code module, to determine display >which< subform
control it is running from, in the main form.

Oops! No can do.

This >is< a valid scenario. For example, the subform might be showing daily
bookings. sf1 might be filtered on January, sf2 on February & so on.

Cheers,
TC
 
S

Sandra Daigle

PMFJI but I believe that you *can* find the parent control of a subform even
when the same form is used as the SourceObject for multiple subform
controls. The trick is to compare hWnd property of the current subform with
the hWnd properties of the main form's subforms. It's not pretty but
consider the following code:

Public Function GetSfrmParentCtlName(pFrmIn As Form) As String
Dim ctl As Control
Dim inti As Integer
Dim fOk As Boolean
Dim strX As String
fOk = False
If IsSubForm(pFrmIn) Then
Do Until inti > pFrmIn.Parent.Controls.Count Or fOk
If pFrmIn.Parent.Controls(inti).ControlType = acSubform Then
If pFrmIn.Parent.Controls(inti).SourceObject = pFrmIn.Name
Then
If pFrmIn.Parent.Controls(inti).Form.hWnd = pFrmIn.hWnd
Then
strX = pFrmIn.Parent.Controls(inti).Name
fOk = True
End If
End If
End If
inti = inti + 1
Loop
'GetSfrmParentCtlName = pFrmIn.Parent.ActiveControl.Name
End If
If fOk Then
GetSfrmParentCtlName = strX
Else
GetSfrmParentCtlName = vbNullString
End If
End Function

Public Function IsSubForm(pFrm As Form) As Boolean
Dim strName As String
On Error Resume Next
strName = pFrm.Parent.Name
IsSubForm = (Err = 0)
End Function
 
T

Tim Ferguson

TC said:
Not when a subform wants to get know what subform control it came from in
the parent form!
...

Oops! No can do.

This >is< a valid scenario.

Fair cop. I'll just add this to the list of reasons I don't use subforms...

B Wishes


Tim F
 
T

TC

Ah! I would never have thought of that approach. I'll try it in the next day
or two & report back on whether it worked for me.

Cheers,
TC


Sandra Daigle said:
PMFJI but I believe that you *can* find the parent control of a subform even
when the same form is used as the SourceObject for multiple subform
controls. The trick is to compare hWnd property of the current subform with
the hWnd properties of the main form's subforms. It's not pretty but
consider the following code:

Public Function GetSfrmParentCtlName(pFrmIn As Form) As String
Dim ctl As Control
Dim inti As Integer
Dim fOk As Boolean
Dim strX As String
fOk = False
If IsSubForm(pFrmIn) Then
Do Until inti > pFrmIn.Parent.Controls.Count Or fOk
If pFrmIn.Parent.Controls(inti).ControlType = acSubform Then
If pFrmIn.Parent.Controls(inti).SourceObject = pFrmIn.Name
Then
If pFrmIn.Parent.Controls(inti).Form.hWnd = pFrmIn.hWnd
Then
strX = pFrmIn.Parent.Controls(inti).Name
fOk = True
End If
End If
End If
inti = inti + 1
Loop
'GetSfrmParentCtlName = pFrmIn.Parent.ActiveControl.Name
End If
If fOk Then
GetSfrmParentCtlName = strX
Else
GetSfrmParentCtlName = vbNullString
End If
End Function

Public Function IsSubForm(pFrm As Form) As Boolean
Dim strName As String
On Error Resume Next
strName = pFrm.Parent.Name
IsSubForm = (Err = 0)
End Function
--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Not when a subform wants to get know what subform control it came
from in the parent form!

Try this experiement. Create a main form frmA. Add three subform
controls sf1, sf2, sf3. Put the same subform frmB into each of those
controls. Now write code in frmB's code module, to determine display

Oops! No can do.

This >is< a valid scenario. For example, the subform might be showing
daily bookings. sf1 might be filtered on January, sf2 on February &
so on.

Cheers,
TC
 
T

TC

Hi Sandra

Works like a dream! I would never have though of using hWind for this,
because I know that an Access control only has an hWnd when it has the
focus. But that is actually irrelevant here.

This is my simplified code. When run from a subform's code module, this will
reliably identify the subform control in which this particular instance of
the subform is executing, in the main form. It works fine, even when the
same subform (with the same recordsource) is referenced in several different
subform controls in the main form.

dim ctl as control
for each ctl in parent.controls
if ctl.controltype = acsubform then
if ctl.form.hwnd = me.hwnd then msgbox ctl.name
endif
next

Great tip!
TC


Sandra Daigle said:
PMFJI but I believe that you *can* find the parent control of a subform even
when the same form is used as the SourceObject for multiple subform
controls. The trick is to compare hWnd property of the current subform with
the hWnd properties of the main form's subforms. It's not pretty but
consider the following code:

Public Function GetSfrmParentCtlName(pFrmIn As Form) As String
Dim ctl As Control
Dim inti As Integer
Dim fOk As Boolean
Dim strX As String
fOk = False
If IsSubForm(pFrmIn) Then
Do Until inti > pFrmIn.Parent.Controls.Count Or fOk
If pFrmIn.Parent.Controls(inti).ControlType = acSubform Then
If pFrmIn.Parent.Controls(inti).SourceObject = pFrmIn.Name
Then
If pFrmIn.Parent.Controls(inti).Form.hWnd = pFrmIn.hWnd
Then
strX = pFrmIn.Parent.Controls(inti).Name
fOk = True
End If
End If
End If
inti = inti + 1
Loop
'GetSfrmParentCtlName = pFrmIn.Parent.ActiveControl.Name
End If
If fOk Then
GetSfrmParentCtlName = strX
Else
GetSfrmParentCtlName = vbNullString
End If
End Function

Public Function IsSubForm(pFrm As Form) As Boolean
Dim strName As String
On Error Resume Next
strName = pFrm.Parent.Name
IsSubForm = (Err = 0)
End Function
--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Not when a subform wants to get know what subform control it came
from in the parent form!

Try this experiement. Create a main form frmA. Add three subform
controls sf1, sf2, sf3. Put the same subform frmB into each of those
controls. Now write code in frmB's code module, to determine display

Oops! No can do.

This >is< a valid scenario. For example, the subform might be showing
daily bookings. sf1 might be filtered on January, sf2 on February &
so on.

Cheers,
TC
 
S

Stephen Lebans

TC the Form object and several controls do have permanent hWnd's for the
current session.
Just my $.02 worth.

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


TC said:
Hi Sandra

Works like a dream! I would never have though of using hWind for this,
because I know that an Access control only has an hWnd when it has the
focus. But that is actually irrelevant here.

This is my simplified code. When run from a subform's code module, this will
reliably identify the subform control in which this particular instance of
the subform is executing, in the main form. It works fine, even when the
same subform (with the same recordsource) is referenced in several different
subform controls in the main form.

dim ctl as control
for each ctl in parent.controls
if ctl.controltype = acsubform then
if ctl.form.hwnd = me.hwnd then msgbox ctl.name
endif
next

Great tip!
TC


Sandra Daigle said:
PMFJI but I believe that you *can* find the parent control of a
subform
even
when the same form is used as the SourceObject for multiple subform
controls. The trick is to compare hWnd property of the current
subform
with
the hWnd properties of the main form's subforms. It's not pretty but
consider the following code:

Public Function GetSfrmParentCtlName(pFrmIn As Form) As String
Dim ctl As Control
Dim inti As Integer
Dim fOk As Boolean
Dim strX As String
fOk = False
If IsSubForm(pFrmIn) Then
Do Until inti > pFrmIn.Parent.Controls.Count Or fOk
If pFrmIn.Parent.Controls(inti).ControlType = acSubform Then
If pFrmIn.Parent.Controls(inti).SourceObject = pFrmIn.Name
Then
If pFrmIn.Parent.Controls(inti).Form.hWnd = pFrmIn.hWnd
Then
strX = pFrmIn.Parent.Controls(inti).Name
fOk = True
End If
End If
End If
inti = inti + 1
Loop
'GetSfrmParentCtlName = pFrmIn.Parent.ActiveControl.Name
End If
If fOk Then
GetSfrmParentCtlName = strX
Else
GetSfrmParentCtlName = vbNullString
End If
End Function

Public Function IsSubForm(pFrm As Form) As Boolean
Dim strName As String
On Error Resume Next
strName = pFrm.Parent.Name
IsSubForm = (Err = 0)
End Function
--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

But he wants to trace the control back to its parent, then that
parent's parent & so on, until he reaches a top-level object (ie. an
object with no parent). IMO there is no way to do this in all
situations.


You can create a chain quite happily like this:

Textbox ->
Page ->
TabControl ->
Form_frmNewTemp

Not when a subform wants to get know what subform control it came
from in the parent form!

Try this experiement. Create a main form frmA. Add three subform
controls sf1, sf2, sf3. Put the same subform frmB into each of those
controls. Now write code in frmB's code module, to determine display
which< subform control it is running from, in the main form.

Oops! No can do.

This >is< a valid scenario. For example, the subform might be showing
daily bookings. sf1 might be filtered on January, sf2 on February &
so on.

Cheers,
TC


but accessing the form's parent property causes a trappable error. I
am not quite sure what anybody would expect the parent of a form to
be, and that is what I don't think is sensible -- "tracking it back
to the desktop"??

Perhaps the OP would like to comment further...

All the best


Tim F
 

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