Another way to use the Forms Collection is with various looping techniques.
By using For...Next, For...Each, or Do...While statements, the programmer can
loop through the collection to affect all the forms, search for specific forms,
and search for specific properties or even values. This will assist in searching
for information without having to program every form name individually.
A simple example would be to retrieve each
form's Caption, as shown in Listing 4.15.
LOOPING THROUGH THE FORMS COLLECTION WITH FOR...NEXT
Dim iLoop as Integer
For iLoop = 0 to Forms.Count - 1
This code declares an integer variable for
looping and then sets up the For...Next statement.
Notice iLoop = 0. You must start at 0 because
collections are 0-based. Because you will not
always know the amount of forms in the collection,
you can take the Forms.Count and subtract 1
from the total. You must remove 1 because the
Count starts at 1 and the Collection starts
at 0. When this sample code is
run, a message box displays with the Caption
property of every form in the collection.
An alternative to the For... Next loop would
be the For...Each loop, which does not depend
on an integer counter (see Listing 4.16).
LOOPING THROUGH THE FORMS COLLECTION WITH FOR...EACH
Dim frmCurr as Form
For each frmCurr in Forms
This is probably preferable to the For...Next loop because you don't have
to keep track of position in the collection.
Using the forms collection topics