The following example has 2 command buttons (Command1,Command2) a frame (Frame1) that contains an option button (Option1) and another option button (Option2) directly on the form (Cut and paste the code in a demo project).
[tt]Option Explicit
Private Sub Command1_Click()
MsgBox Option1.Container & " " & Option1.Parent.Name End Sub
Private Sub Command2_Click() On Error GoTo ErrEh
MsgBox Option2.Container Exit Sub
ErrEh:
MsgBox Option2.Parent.Name End Sub
Try this if you want to know if it is contained at any level e.g. control inside a frame inside a frame.
Public Function IsContainedIn(objContained As Object, _
objContainer As Object) As Boolean
'*****
' objContained is to be tested for being contained in
' objContainer.
'*****
Dim objW As Object
'*****
'* climb up Container tree until a container
'* matches the one being tested or we reach the top container.
'*****
Set objW = objContained
Dim objWContainer As Object
Dim lngErr As Long
Do
If TypeOf objW Is Form Then ' at the top of the container chain
IsContainedIn = False
Exit Do
End If
' If our container is one being checked then we are contained
On Error Resume Next
' e.g. DataControl has no Container Property
Set objWContainer = objW.Container
lngErr = Err
On Error GoTo 0
If lngErr Then Exit Do ' No container property
If objWContainer Is objContainer Then
IsContainedIn = True
Exit Do
End If
' Climb up to the next container
Set objW = objWContainer
Loop
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.