Load Statement |
Load object
object
Use: Required
Data Type: A Form or Control object
An expression that evaluates to a form or control.
Loads a form or control into memory.
When a control or form is first loaded using the Load statement, it's resident in memory, but it isn't visible on the screen. To make a form visible, use the form's Show method. To make a control visible, set its Visible property to True.
Sidebar 5. Creating a Dynamic Control ArrayTo 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 Next 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. |
You can explicitly create a control only with the Load statement as part of a control array. See the sidebar Sidebar "Creating a Dynamic Control Array"
Use the Unload statement to remove from memory the form or control you've loaded with the Load statement.
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.
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.