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 described here:

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

1 Method: An action that can be performed on 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 name or collection name with a period and the property name:

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 VBA Editor, it immediately displays a list of properties and methods for that object. For example, if you type into the Code or Immediate window, Access immediately displays a menu of properties supported by the Forms collection, as shown at the top of Figure 5-4.

Forms.

l^f Count

Item l®!1 Parent

DoCtnd. I

Figure 5-4:

Find menus of proper-

.«lAddMenu

ApplyFUter Ä Beep ^■CancelEvent A Glose

CopyDataPaseFile g CopyOPjeci ties and methods.

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

DoCmd.

m 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, known as IntelliSense, 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 name or 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 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 can use the Search tool in the Object Browser to find a specific word. The Members Of pane on the right changes to show only properties, methods, and events for the item you selected in the Classes pane on the left.

3. Optionally, if you want Help for a name in the right column, click the name for 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 see the Help page for the DoCmd object, as in the right half of Figure 5-5.

When you're viewing the Help information 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 in the Object Browser confuse you. This isn't the feudal system, so think of a class as anything that can act as a container (an object or a collection). Think of the members of as things (properties, methods, other collections) within the collection.

Figure 5-5:

Find help in the Object Browser.

Click for help with methods.

Click for help with methods.

Figure 5-5:

Find help in the Object Browser.

Methods of the selected object (DoCmd)
0 0

Post a comment