Understanding properties and methods

Every object and every collection exposes at least one property and method to VBA. The difference between a property and a method is as follows:

1 Property: A characteristic of an object, such as size, color, or font

1 Method: An action that can be performed upon an object, such as open, close, or copy

The standard syntax for referring to a specific object (or collection) property is to follow the object/collection name with a period and the property name, as follows:

ObjectCollectionName.property where ObjectCollectionName is a name from the object model, and property is a valid property name for that object.

When you type a valid object or collection name followed by a period into the Code or Immediate window of the VB editor, it immediately displays a list of properties and/or methods for that object. For example, if you type


into the Code or Intermediate window, Access immediately displays a list of properties supported by the Forms collection in a menu, as in the top of Figure 5-4.

[fi1 iAppilcatlon

Count l^1 Item Hi1 Parent

Figure 5-4:

Find menus of properties and methods.

DoCltfd. I

®;AddMenu S ApplyFllter ■a Beep ® CancelEvent ^ Close

CopyDatatiaseFlle a CopyObject

Look at the bottom half of Figure 5-4. DoCmd is another object in the Access object model (which I haven't mentioned yet) that offers many methods. Type its name followed by a period


into the Code window or Immediate window, and you see a list of methods supported by the DoCmd object. After the menu is visible, you can just click any property or method name to add it to the command.

The drop-down lists of property and method names serve as useful reminders for experienced programmers, but beginners need more information than the little lists provide. You can get help with objects and collections by using the same basic techniques that you use for getting help with VBA statements. For example, you can select (double-click) an object or collection name in your code and then click Help (or press F1). Or, you can search the Object Browser for the object/collection name and get help from there. Here's how.

1. In the VBA editor, open the Object Browser by pressing F2 or by choosing ViewOObject Browser from the VBA editor's menu bar.

2. In the left column, scroll to and then click the name for which you want help.

For example, for help with the DoCmd object, scroll down through the left column and then click DoCmd. Alternatively, you could use the Search tool in the Object Browser to find a specific word. The pane on the right changes to show only properties, methods, and events for the item that you selected at the left.

3. Optionally, if you want Help for a name in the right-column, click the name with which you want help.

4. Press F1 or click the Help button in the Object Browser.

For example, if you click DoCmd in the left column and then press F1 or click the Object Browser's Help button, you'll get the Help page for the DoCmd object, as in the right half of Figure 5-5.

When you're viewing the help for an object or collection, be sure to look at the headings under See Also. Those offer help with the specific properties and methods exposed by the object.

Classes and members

Don't let the Classes and Members Of headings (an object or collection). Think of the members in the Object Browser confuse you. Think of a of as things within the collection. class as anything that can act as a container

DoCmd is selected.

Click for help with methods.

Figure 5-5:

Find help in the Object Browser.

DoCmd is selected.

Click for help with methods.

Figure 5-5:

Find help in the Object Browser.

Methods of the DoCmd object

Help for the DoCmd object

Methods of the DoCmd object

Help for the DoCmd object

Was this article helpful?

0 0

Post a comment