See Also Example
Assigns an object reference to a variable.
Syntax
Set objectvar = { objectexpr | New formtype | Nothing}
Remarks
The Set statement has the following parts:
Part Description
objectvar Name of the variable to which the object reference is being assigned.
objectexpr Expression consisting of the name of an object, another declared variable of the same object type , or a function or method that returns an object.
New Reserved word used to create a new instance of a specific object type (created as design time), such as Form1. The New reserved word cannot be used to create new variables of any of the fundamental data types, nor can it be used to create a variable of any generic object type (MDIForm, Form or Control.) or any specific control type (CommandButton, TextBox, and so on).
formtype A specific form type, for example Form1.
Nothing Reserved word used to discontinue association of objectvar with any specific object. Assigning objectvar to Nothing releases all the resources associated with the previously referenced object when no other variable refers to it.
To be valid, objectvar must be an object type consistent with the object being assigned to it. And, it must either have been explicitly declared (using a Dim, Global, ReDim, or Static statement) or implicitly declared for you by Visual Basic, as occurs, for example, with form types.
The following example illustrates how Dim is used to formally declare the variable LedgerForm as Form1. The Dim statement only declares a variable of type Form1. No instance of Form1 actually exists. Set then assigns a reference to a new instance of Form1 to the LedgerForm variable.
Dim LedgerForm As Form1
Set LedgerForm = New Form1
In the following example for a control, the variable X is declared (using Dim) as a variable that refers to a list box. The Set statement assigns the object reference for X to an actual instance of a list box on Form1, in this case List1.
Dim X As ListBox
Set X = Form1!List1
Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because these variables are references to, rather than copies of the object, any change in the object is reflected in all variables that refer to it.
However, when you use the New reserved word in the Set statement, you are actually creating a new instance of the referenced form type. The new instance is not loaded until you either explicitly load it using the Load statement or implicitly load it by referring to one of its properties, methods, or members in code.
Dim Statement
Global Statement
ReDim Statement
Static Statement
The example uses the Set statement to declare a reference to a new instance of Form1. To try this example, which also illustrates the action of the Arrange method, paste the code into the Declarations section of an MDI Form named MDIForm1 that has an MDI child form (named Form1), and a picture box (named Picture1). Then press F5 and click anywhere in the picture box..
Const FORMCOUNT = 5
Dim F(1 To FORMCOUNT) As Form1 ' Create object type variable.
Sub MDIForm_Load ()
Dim I ' Declare local variable.
Load Form1 ' Load original Form1.
For I = 1 To FORMCOUNT
Set F(I) = New Form1 ' Refer to new instance of Form1.
F(I).Caption = "Form" & I + 1 ' Change caption on copies.
F(I).Show ' Load 5 copies of Form1.
Next I
End Sub
Sub Picture1_Click ()
Static ClickCount ' Declare variables.
Dim I, PrevWidth, Start
ClickCount = ClickCount + 1 ' Increment click counter.
Select Case ClickCount
Case 1
MDIForm1.Arrange 1 ' Tile horizontally.
Case 2
MDIForm1.Arrange 2 ' Tile vertically.
Case 3 ' Minimize each form.
PrevWidth = MDIForm1.Width ' Get MDIForm width.
MDIForm1.Width = PrevWidth / 2 ' Divide it in half.
Form1.WindowState = 1 ' Minimize the original.
For I = 1 To FORMCOUNT ' Look at each instance of F.
If TypeOf F(I) Is Form1 Then
F(I).WindowState = 1 ' Minimize each copy.
End If
Next I
Start = Timer ' Get timer start point.
Do
Loop Until Timer >= Start + 5
MDIForm1.Width = PrevWidth ' Resize to original size.
MDIForm1.Arrange 3 ' Arrange icons.
End Select
End Sub