GETTING STARTED WITH VBA

VBA adds support for object-oriented features, a browser for viewing an object's functionality, and the capability to define custom public and private properties and methods. The rest of this chapter introduces you to these powerful new features.

Introducing Objects

VBA adds some basic object-oriented features. Your application consists of many objects. An object can be thought of as a single unit that contains code and data that serve a common purpose. Table 2.1 lists object examples.

Table 2.1. Example and Descriptions of Objects
Object Description
Text box All controls are objects.
Form Every form in your application is a separate object.
Table Each table in your application contains other objects, such as fields and indexes.
Excel Other applications that support Automation are also objects.

Objects have properties and methods, which are the exposed parts of the object. Exposed properties and methods are considered to be public. For example, the capability to reference a control's value is done through a public property, Value.

All functionality that's internal to the object is considered private. All the private members (both properties and methods) are accessible only to the object itself. For example, writing a private method on a form means that the method can be called only from within the form. No other form knows about nor can call that method.

Before Access 95, all code behind forms was private. Therefore, you had to resort to poor and complex programming techniques to expose your form functionality. Often, this was done through global and static variables and subroutines written in global modules. The disadvantage to this approach was that your functionality wasn't encapsulated. By exposing the internal guts of your forms through global variables and subroutines, you make it very easy for someone to accidentally change the state of your form. By being able to hide, or encapsulate, all the internal workings within the form, you avoid this problem. You now can selectively expose any parts of your form by defining those parts as public.

What's more, Access 2000 lets you create multiple instances of objects. This way, you can display and manipulate multiple copies of the form. Imagine displaying a customer form a few times, one for each customer. The manipulation of multiple instances is introduced in Chapter 4, “Looking at the Access Collections,” and discussed again in Chapter 16.

Using the Object Browser

Access 2000 supports a large number of different objects. Every control that you can insert on a form is also an object. What's more, through Automation, you can use VBA to program other Automation objects, such as Microsoft Word or Excel. You can even use C++ or Visual Basic versions 4 and later to develop your own Automation applications. (For more information on Automation, see Chapter 13, “Driving Office Applications with Automation and DDE.”)

Because you now can access a large number of objects, you need a mechanism to manage them. VBA provides this through the Object Browser. The Object Browser makes it easy to view and obtain information about all the public properties and methods of any object (see Figure 2.6).

Figure 2.6. The Object Browser lets you view and obtain information about all the objects your application references.


The Object Browser can

  • Show all the objects available to your applications, as well as the properties and methods associated with the object. You can also browse any other object available on your system.

  • Allow you to quickly locate and go to different procedures in your code.

  • Enable you to get help via the Help button on generally any property, method, or object.

  • Allow you to paste the prototype of an object's method or property into your code. This capability is helpful if you can't remember the list of parameters, and it also helps prevent typing errors.

Displaying the Object Browser

To display the Object Browser, you must be in the VBE. This is the only time the Object Browser is available. In the following steps, you open a code module so that you can use the Object Browser:

1.
Create a new code module by going to the Modules page on the database window and clicking New. You will now be placed in the VBE.

2.
From the View menu, choose Object Browser.

Tip

You also can display the Object Browser by pressing F2.


3.
Select the different objects from the Projects/Libraries drop-down list (the top drop-down list). Selecting different libraries displays all the objects within the library.

4.
Clicking a class (object) in the Classes list displays all the members (methods and properties) in the class. If a Help file is available for the object, clicking the ? button displays the associated Help topic.

By selecting a library and an object, you can view all the public methods and properties on the object. Selecting an individual method or property gives you the appropriate syntax. At this point, you can paste the syntax into your code, or click the ? button next to the syntax to bring up the associated Help topic.

Adding References to Other Objects

By adding references to any object on your system, you can use the referenced object in your application and the Object Browser to view the object. To add references, follow these steps:

1.
In the VBE, choose References from the Tools menu.

2.
In the References dialog, select the name of the application or object (see Figure 2.7).

Figure 2.7. By setting references ahead of time, you have access to all the objects included in that reference.


If the object or application isn't listed, you can browse for it. Object libraries have a .tlb or .olb file extension. Because executable files, DLLs, and OCXs also can contain object libraries, these extensions are also available.

Also, you can reference other databases you might have created. When you do this, you can open the forms and call the functions and procedures of the other database. This way, you can create a common procedure library that all your applications can share.

Caution

The order of the objects defines the order in which they're searched. This is important if you have two or more libraries that contain objects of the same name. When this occurs, the object listed higher in the list is used. This can cause problems with your code if two libraries contain objects of the same name.


After you make a reference to an object, you can call the methods and properties available on that object. You can view the available methods and properties of the object by using the Object Browser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.136