Distinguishing between objects and collections

You're no doubt wondering how (or why) a thing could be both an object and a collection at the same time. Start with a simple, real world example: a can of peas. The can of peas itself is an object — a unit — that you can buy at most any store and easily carry in your hand. The can, however, is also a collection — a repository — of individual peas. Thus, it's both an object and a collection. The same could be said for a carton of eggs: The carton itself is an object, but it can also be a collection because it holds the eggs inside.

Referring to Figure 5-1, take a look at what each box refers to. The Forms collection is a collection of all the currently open forms in a database. When your VBA code is running, it can access any form within that collection of open forms. See how the word Form is indented under the word Forms. This illustrates that each object in the Forms collection is a Form. Seems reasonable.

In the object model hierarchy, a Form is color-coded as an object and a collection. How can a form (an object) be a collection? If you look at just about any form, you'll see that it contains controls. In fact, a form is a collection of controls. From a programming standpoint, a form is an object that you can manipulate (open, close, print, and so forth) as a unit (an object). However, it's also a Controls collection, which contains smaller individual objects (each called a control) that you can manipulate with VBA.

But wait a minute. According to the object model, a control is both an object and a collection. What collection of things does a control contain? Each control has its own collection of properties that uniquely define its name and many other properties. You can see those properties in form design when you select a single control and view its Properties sheet. For example, in Figure 5-2, the combo box control named Contact Type is currently selected in Forms design. The Properties sheet in that same figure is showing the properties for that one control.

Figure 5-2:

Forms hold controls; controls hold properties.

Address Book Form : Form

Address Book Form : Form

Form Header



j j 1 í^úickF&Tid| : jUnbound

¥ Detail



act Type v ■: i

Bjcor ;::

First fclánie[

• •





[Address^ if











v ; Stated

Every control has its own collection of properties.

• Every form has its own collection of controls.

jjj* Combo Box: Contact Type

Contact Type

Format Data Event Other

Name Contact Type


Value List

Control Source Contact Type


Input Mask

Row Source Type .

Row Source

'Column Count . . , Column Heads . . . Column Widths . . , Bound Column . . . List Rows

Lust orner; Vendor

When you're in Access's forms Design view, there are several ways to open the Properties sheet. Use whatever method seems most convenient. Your options are i Double-click the control whose properties you want to view.

i Right-click a control and choose Properties.

i Press the F4 key.

i Choose ViewOProperties from the Access toolbar.

As you work with VBA in Access, you'll often see little chunks of object modules (like the example shown in Figure 5-1) and smaller. The complete Access object model is too big to even fit onscreen and would be a tight squeeze on a printed page in this book. However, you can always take a look at an entire object model by following these steps:

1. If you're currently in the Access program window, press Alt+F11 to switch to the VBA editor.

2. In the VBA editor's Type a Question for Help box, type object model.

3. Press Enter.

4. In the Help pane, click the name of an object model.

In this case, click Microsoft Access Object Model to see the highest-level objects and collections in Access object model.

The selected object model appears in a separate Help window, as in the example shown in Figure 5-3. For help with any object or collection that appears in the model, just click its name in the Help window to see more specific help on that particular object or collection.

0 0

Post a comment