Parent Property

Applies To

CheckBox control, ComboBox control, CommandButton control, Image control, Label control, ListBox control, MultiPage control, OptionButton control, ScrollBar control, SpinButton control, TabStrip control, TextBox control, ToggleButton control.

Description

Returns the name of the form, object, or collection that contains a specific control, object, or collection.

Syntax

object.Parent

The Parent property syntax has these parts:

Part

Description

object

Required. A valid object.


Remarks

Parent is read-only.

Use the Parent property to access the properties, methods, or controls of an object's parent.

This property is useful in an application in which you pass objects as arguments. For example, you could pass a control variable to a general procedure in a module, and use Parent to access its parent form.

Example

The following example uses the Parent property to refer to the control or form that contains a specific control.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • Two Label controls named Label1 and Label2.
  • A CommandButton named CommandButton1.
  • One or more additional controls of your choice.
    Dim MyControl As Object
    Dim MyParent As Object
    Dim ControlsIndex As Integer
    
    Private Sub UserForm_Initialize()
        ControlsIndex = 0
        CommandButton1.Caption = "Get Control and Parent"
        CommandButton1.AutoSize = True
        CommandButton1.WordWrap = True
    End Sub
    
    Private Sub CommandButton1_Click()
        'Process Controls collection for UserForm
        Set MyControl = Controls.Item(ControlsIndex)
        Set MyParent = MyControl.Parent
        Label1.Caption = MyControl.Name
        Label2.Caption = MyParent.Name
        
        'Prepare index for next control on Userform
        ControlsIndex = ControlsIndex + 1
        If ControlsIndex >= Controls.Count Then
            ControlsIndex = 0
        End If
    End Sub