Creating a Dynamic Control Array

To create a dynamic control array—that is, an array of controls you can add to at runtime—you must first place a control of the required type on the form and set its index property to 0. You can then use the Load statement to create new controls based on the control whose Index is 0. The new controls inherit all the properties of the original control, including its size and position. This means you must set the Left and Top properties for the new controls; otherwise, all your controls will sit on top of each other! These newly loaded controls are also hidden, so you must also set their Visible property to True once you have sized and positioned them. The following example creates a control array containing five command buttons that appear horizontally across a form:

Private Sub Form_Load()

Dim intCtrlCtr As Integer Dim varCtrl As Variant

For intCtrlCtr = 1 To 4

Load cmdArray(intCtrlCtr)

cmdArray(intCtrlCtr).Caption = "Button #" _

& intCtrlCtr + 1 cmdArray(intCtrlCtr).Top = cmdArray(0).Top cmdArray(intCtrlCtr).Left = _

cmdArray(intCtrlCtr - 1).Left + _ cmdArray(intCtrlCtr - 1).Width + 75


For Each varCtrl In cmdArray varCtrl.Visible = True Next End Sub

Controls belonging to a single control array share the same event handlers. However, their Index property is passed as a parameter to the event handler, allowing you to determine which control fired the event.

Trying to load a control that's already loaded causes an error, so it's a good idea to maintain an instance counter to determine the next index number to use when creating a control.

Note that, unlike previous versions of VB, VB6 supports dynamic control creation. Also note that hosted versions of VBA don't support dynamic control creation.

• Referencing any property or method of a form—or of a control on a form— that isn't already loaded forces the form to be loaded into memory.

• In Visual Basic, the Form_Initialize event, followed by the Form_Load event (for VBA UserForms, the UserForm_Initialize event only) is fired when the Load statement is called, or when the form is loaded into memory.

Load Statement 407

• In these days of VB rapidly becoming a more object-oriented language, a new syntax is emerging for loading and unloading controls and forms. The idea is to create an instance of a form and assign that instance to a local object variable as you would for any another object. The following example shows a comparison of the new and old styles for loading a form:

'New Syntax

Dim frmNewForm As Form2 Set frmNewForm = New Form2 frmNewForm.Show vbModal Set frmNewForm = Nothing

'Old Syntax Load Form2 Form2.Show vbModal Unload Form2

• Loading a form causes the form's visual interface to be loaded. Whether or not the form is loaded, however, its code remains in memory and continues to be accessible.

See Also

Set Statement, Unload Statement

Was this article helpful?

+1 0


  • brad
    How to create control array VBA?
    8 years ago
  • pearl
    Can you create an array of controls is vba?
    1 year ago
  • asmara hagos
    When would you use an array in visual basic?
    5 months ago
  • jean vasala
    How to create an array of controls in vba access?
    5 months ago

Post a comment