Detect whether a Control has a Parent Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to find a way of determining programatically whether a control is
on the page of a tab control or not. I have set a reference to the object,
objControl and started off by trying.

If objControl.Parent.ControlType = acPage Then
...
Else
...
End If

However this property only exists if the control is on a tab otherwise you
get an error. Is there a way of detecting if this property exists or do you
do this by generating and trapping the error?
 
Trapping the error sounds fair enough.

You could try:
If TypeOf objControl.Parent Is Page Then

BTW, you probably realize that there are some controls that could be in the
tab page but have a different parent, e.g.:
- the parent of an option button in a group is the group;
- the parent of an attached label is the control.
 
Hi Allen

Thanks for your swift response. The code is only called from text boxes - I
have a universal calendar form for entering dates - it then runs the
BeforeUpdate or AfterUpdate event for the control that opened it, e.g.

CallByName objControl.Parent, objControl.Name & "_AfterUpdate", VbMethod

If the control is on a Tab you have to do

CallByName objControl.Parent.Parent.Parent, objControl.Name & "_AfterUpdate"

It may not look pretty, but it does mean the form can run unchanged in all
my applications as none of the forms or controls are hard-coded into it and
any validation code on the control runs (Just have to make it a Public Sub).

Kind regards
--
Peter Schmidt
Ross-on-Wye, UK


Allen Browne said:
Trapping the error sounds fair enough.

You could try:
If TypeOf objControl.Parent Is Page Then

BTW, you probably realize that there are some controls that could be in the
tab page but have a different parent, e.g.:
- the parent of an option button in a group is the group;
- the parent of an attached label is the control.
 
Pete said:
I am trying to find a way of determining programatically whether a
control is on the page of a tab control or not. I have set a
reference to the object, objControl and started off by trying.

If objControl.Parent.ControlType = acPage Then
...
Else
...
End If

However this property only exists if the control is on a tab
otherwise you get an error. Is there a way of detecting if this
property exists or do you do this by generating and trapping the
error?

There's not really anything wrong with trapping the error, but the
approach you've taken won't necessarily work for all controls on the
page. For example, an attached label's Parent is the control it's
attached to, and the controls in an option group have the option group
frame as their parent.

I believe this function is more reliable, and may work for you. You
pass it references to an unspecified control and a tab page, and it
returns True if that control is among the control on that tab page.

'----- start of code -----
Function IsControlOnPage(pControl As Access.Control, pPage As
Access.Page) As Boolean

Dim ctl As Access.Control

For Each ctl In pPage.Controls
If ctl Is pControl Then
IsControlOnPage = True
Exit For
End If
Next

End Function

'----- end of code -----

So, to tell if control "Text1" is on tab page "Page2", you can write:

If IsControlOnPage(Me!Text1, Me!Page2) Then
' it's on the page.
Else
' it isn't.
End If
 
Hi Dirk

Allen's suggestion of:

If TypeOf objControl.Parent Is Page Then

works for me in my circumstances (only ever used for text controls-see my
other posting) - the calendar form does not 'know' if the form that called it
has a tab control on it or not.

I don't know why, but I don't like the concept of deliberately generating
errors and then trapping them as a way of detecting things other than
'genuine' errors - it just goes against the grain and strikes me as being
'sloppy' ;-)
 
Pete said:
Hi Dirk

Allen's suggestion of:

If TypeOf objControl.Parent Is Page Then

works for me in my circumstances (only ever used for text
controls-see my other posting) - the calendar form does not 'know' if
the form that called it has a tab control on it or not.

Ah, I see.
I don't know why, but I don't like the concept of deliberately
generating errors and then trapping them as a way of detecting things
other than 'genuine' errors - it just goes against the grain and
strikes me as being 'sloppy' ;-)

Suit yourself. I believe in doing things the most efficient way, and
sometimes there is no more efficient way exposed to the programmer.
It's true that you have to beware of *assuming* that the error you trap
is the one you were expecting.
 

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

Back
Top