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. |
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 |
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:
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
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