Microsoft Office Tutorials and References
In Depth Information
Understanding Objects and Collections
Listing 4-6: Looping Through the Controls
DoCmd.OpenForm “Products Form”
Dim myCtl as Control
For Each myCtl In Forms![Products Form]
‘Code to be performed on every control goes below.
Debug.Print myCtl.Name
Next
A control is also a collection: A collection of properties defines the control’s
name, contents, appearance, type, and behavior. If you want to set up a loop
that accesses each property that a control supports, first ensure that the
form is open. With that task accomplished, define an object variable of the
Property type, and use the specific control’s name as the collection name
in the For Each...Next loop, as Listing 4-7 shows.
Listing 4-7: Looping Through the Properties
DoCmd.OpenForm “Products Form”
Dim myProp as Property
For Each myProp In Forms![Products Form].[Product Name]
‘Code to be performed on every control goes below.
Debug.Print myProp.Name & “ = “ & myProp.Value
Next
The first line opens a form named Products Form. The next line defines
an object variable named myProp as the Property type. Then the For
Each...Next loop displays the name and value of every property for the
Product Name field.
Using With...End With
If you need to change a whole bunch of properties associated with an object,
you can save a little typing by using a With...End With block. The syntax
of the block is
Book VIII
Chapter 4
With objectName
. property = value
End With
objectName is the name of an open object or the object variable name
that points to the object, .property is a valid property for that object, and
value is the value you want to assign to that object. Assuming that myCtl
refers to a control on an open form, as in Listing 4-8, you can use a With
myCtl...End With block to change several properties of that control.
 
Search JabSto ::




Custom Search