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.

Refer to Figure 5-1, and take a look at what each box refers to. The Forms collection is a collection of all the open forms in a database. When your VBA code is running, it can access any form within that collection of open forms. Notice 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, Forms 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 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.

Wait a minute. According to the object model, Controls 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 property sheet. For example, in Figure 5-2, the combo box control named Company is selected in the form's Design view. The property sheet in the figure is showing the properties for only that control.

Every form has its own collection of controls.

Figure 5-2:

Forms hold controls; controls hold properties.

Every form has its own collection of controls.

Figure 5-2:

Forms hold controls; controls hold properties.

Every control has its own collection of properties.

When you're in the Access forms Design view, you have several ways to open the property sheet. Use whatever method seems most convenient. Your options are

^ Double-click the control whose properties you want to view.

^ Right-click a control and choose Properties.

^ On the (Form Design Tools) Design tab, click the Property Sheet command in the Tools group.

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

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

2. Press F1 or choose HelpOMicrosoft Visual Basic Help to show the main Access Developer help screen.

3. In the Help window, click Access Object Model Reference.

The Help window, shown in Figure 5-3, shows the subcategories of the Access object model. Click one of the subcategories to drill down into each object's specific properties, methods, and collections. (You see a lot of them, so warm up your clicking finger.)

Figure 5-3:

View an object model from Help.

Figure 5-3:

View an object model from Help.

0 0

Post a comment