Add Method

Applies To

Controls collection, Pages collection, Tabs collection.

Description

Adds or inserts a Tab or Page in a TabStrip or MultiPage, or adds a control by its programmatic identifier (ProgID) to a page or form.

Syntax

For MultiPage, TabStrip

Set Object = object.Add( [ Name [, Caption [, index]]])

For other controls

Set Control = object.Add( ProgID [, Name [, Visible]])

The Add method syntax has these parts:

Part

Description

object

Required. A valid object name.

Name

Optional. Specifies the name of the object being added. If a name is not specified, the system generates a default name based on the rules of the application where the form is used.

Caption

Optional. Specifies the caption to appear on a tab or a control. If a caption is not specified, the system generates a default caption based on the rules of the application where the form is used.

index

Optional. Identifies the position of a page or tab within a Pages or Tabs collection. If an index is not specified, the system appends the page or tab to the end of the Pages or Tabs collection and assigns the appropriate index value.

ProgID

Required. Programmatic identifier. A text string with no spaces that identifies an object class. The standard syntax for a ProgID is <Vendor>.<Component>.<Version>. A ProgID is mapped to a class identifier (CLSID).

Visible

Optional. True if the object is visible (default). False if the object is hidden.


Settings

ProgID values for individual controls are:

CheckBox

Forms.CheckBox.1

ComboBox

Forms.ComboBox.1

CommandButton

Forms.CommandButton.1

Frame

Forms.Frame.1

Image

Forms.Image.1

Label

Forms.Label.1

ListBox

Forms.ListBox.1

MultiPage

Forms.MultiPage.1

OptionButton

Forms.OptionButton.1

ScrollBar

Forms.ScrollBar.1

SpinButton

Forms.SpinButton.1

TabStrip

Forms.TabStrip.1

TextBox

Forms.TextBox.1

ToggleButton

Forms.ToggleButton.1


Remarks

For a MultiPage control, the Add method returns a Page object. For a TabStrip, it returns a Tab object. The index value for the first Page or Tab of a collection is 0, the value for the second Page or Tab is 1, and so on.

For the Controls collection of an object, the Add method returns a control corresponding to the specified ProgID. The AddControl event occurs after the control is added.

The following syntax will return the Text property of a control added at design time:

userform1.thebox.text
If you add a control at run time, you must use the exclamation syntax to reference properties of that control. For example, to return the Text property of a control added at run time, use the following syntax:

userform1!thebox.text
Note You can change a control's Name property at run time only if you added that control at run time with the Add method.

See Also

AddControl event, Clear method, Page object, Remove method, Tab object.

Example

The following example uses the Add method to add a control to a form at run time and uses the AddControl event as confirmation that the control was added.

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

  • A CommandButton named CommandButton1.
  • A Label named Label1.
    Dim Mycmd As Control
    Private Sub CommandButton1_Click()
    
        Set Mycmd = Controls.Add("Forms.CommandButton.1") ', CommandButton2, Visible)
        Mycmd.Left = 18
        Mycmd.Top = 150
        Mycmd.Width = 175
        Mycmd.Height = 20
        Mycmd.Caption = "This is fun." & Mycmd.Name
        
    End Sub
    
    Private Sub UserForm_AddControl(ByVal Control As MSForms.Control)
        Label1.Caption = "Control was Added."
    End Sub
Example

The following example uses the Add, Clear, and Remove methods to add and remove a control to a Page of a MultiPage at run time.

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

  • A MultiPage named MultiPage1.
  • Three CommandButton controls named CommandButton1 through CommandButton3.
    Dim MyTextBox As Control
    
    Private Sub CommandButton1_Click()
    Set MyTextBox = MultiPage1.Pages(0).Controls.Add("Forms.TextBox.1", _
        "MyTextBox", Visible)
    End Sub
    
    Private Sub CommandButton2_Click()
        MultiPage1.Pages(0).Controls.Clear
    End Sub
    
    Private Sub CommandButton3_Click()
        If MultiPage1.Pages(0).Controls.Count > 0  Then
            MultiPage1.Pages(0).Controls.Remove "MyTextBox"
        End If
    End Sub
    
    Private Sub UserForm_Initialize()
        CommandButton1.Caption = "Add control"
        CommandButton2.Caption = "Clear controls"
        CommandButton3.Caption = "Remove control"
    End Sub
Example

See the Cut method example.