When a form is referenced in code, it automatically
loads the form into memory. An advantage of
the Forms Collection is that it contains only
forms that have been loaded into memory. This
enables the programmer to test for a specific
form by looping through the collection and testing
the Name property of every item in the collection.
An example of this code is found in Listing
4.17.
LISTING 4.17
VERIFYING WHETHER A PARTICULAR FORM IS LOADED
Sub Command1_Click()
Dim frmCurr as Form
For each frmCurr in Forms
If frmCurr.Name = "Form3" Then
MsgBox "Form3 has been loaded"
End If
Next frmCurr
End Sub
This code declares a form variable for looping
and then sets up the For...Each statement. Inside
the For...Each statement is an If test. With
this code you are looking through each item
in the collection
and testing the property frmCurr.Name. This
allows frmCurr to represent every form that
is loaded in the collection. If the Name property
is equal to Form3, a message box displays informing
us that Form3 has been loaded into memory.
This code sample requires a project with at least three forms. The default
form names are expected, and the Form_Load event is responsible for loading all
forms into memory. Remember that if the forms are not loaded at the moment the
code runs, they will not be included in the collection.
Using the forms collection topics
See Also