Control Parent

T

TC

I'm aware of that, Stephen. That is why the method in question never
occurred to me. But as I say in my post, this is in fact irrelevant! The
subform has an hWnd whenever it is executing. You can walk the main form's
subform controls, then get to the running instance of the subform via the
subform control's Form property. Then you just see if the hWnd (if any) of
that instance, is the known hWnd of the running instance. So the code as
posted, works as described.

Cheers,
TC


Stephen Lebans said:
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.


TC wrote:

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
 
S

Sandra Daigle

Hi TC,

Glad it worked for you! I can't remember why I originally needed this but I
do remember that I had almost given up when I thought of the hWnd property
and lo and behold, it worked!

Thanks for posting the simplified code - I'll hang on to it for future use.
While my code may be unnecessarily complex, I can't remember the details of
how I arrived at it. I am *sure* there must be a perfectly good reason for
its complexity ;-)

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.
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
 
S

Stephen Lebans

The SubForm's hWnd is permanet for the current Access session or until
you switch back and forth from Form to Design view not just when the
SubForm control or any of its constituent controls have the focus.
:)
--

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


TC said:
I'm aware of that, Stephen. That is why the method in question never
occurred to me. But as I say in my post, this is in fact irrelevant! The
subform has an hWnd whenever it is executing. You can walk the main form's
subform controls, then get to the running instance of the subform via the
subform control's Form property. Then you just see if the hWnd (if any) of
that instance, is the known hWnd of the running instance. So the code as
posted, works as described.

Cheers,
TC


"Stephen Lebans"
wrote in message news:#[email protected]...
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


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.


TC wrote:

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