AddControl event, Add method 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("MSForms.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

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.