Load Statement

Syntax

Load object

object

Use: Required

Data Type: A Form or Control object

An expression that evaluates to a form or control.

Description

Loads a form or control into memory.

Rules at a Glance

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

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.