B.2. Adding References to Your Projects

As noted above, adding references to libraries from your Access project can allow you to quickly enhance the functionality of your application with less coding effort. The types of type or object libraries that Access can reference include:

  • Type libraries: OLB, TLB, and DLL

  • ActiveX Controls: OCX

You have seen that you can automate Office applications by adding a reference to one of the libraries that comes with Microsoft Office, for example, Microsoft Office 11.0 Object Library. Here are the steps necessary to add a reference, as well as what it means when you do.

Open the Visual Basic Editor and select Tools | References. This will display the References dialog box as shown in Figure B-1.

The items listed in the Available References list include items that have been registered to the Windows System Registry. To add one of these libraries to references for your project, check the box to the left of the library. When you close and reopen the References dialog box, any libraries you have checked will move above all unselected libraries.

Figure B.1. Figure B-1

NOTE

The order of the libraries you have selected is important. See section Order of References Is Important later in this appendix.

Many application installation packages will handle registering libraries for you. If the application you install does register the library you want, there are two ways to use the library in your references.

One method to get to an unregistered library is to use the Browse... button. Click the Browse... button to open a file selection dialog box. Select the type of library you want from the drop-down list for Files of type. Browse to the folder that contains that library and select the library.

Another method to get to an unregistered library is to register the library yourself. If the library is a 32-bit library (most are), use REGSVR32 to register it. To run REGSVR32 select Run... from the Windows Start menu. In the Open box of the Run dialog box enter REGSVR32 followed by the full file specification of the library you want to register. For example:

REGSVR32 "C:Program FilesCommon FilesMicrosoftOffice11MSOCFU.DLL"

After you register a library, you will need to close and reopen the References dialog box to get the library to display in the Available References list.

B.2.1. Reference Order Is Important

One of the reasons for adding references to your project is to make additional classes available so you can declare variables in your code and manipulate objects of those classes. But you should be aware that the name of a class in one library does not have to be unique from the names of classes in other libraries.

A classic example of this occurs when you include references to both DAO and ADO. Both libraries have a Recordset class. (You can use the Object Browser to see when a class occurs in more than one library. The Object Browser is discussed below.)

In situations where there is a duplication of class names, Access determines which class to use by searching sequentially down the list of libraries listed in the Available Libraries list. Unfortunately, the compiler won't always tell you that you have the wrong reference. If you refer to a property or method that is not available for the class you have used in your variable declaration, the compiler will report the problem. Otherwise, you'll discover the problem only when you test your code.

If you have libraries that have classes with the same names, you can get Access to choose the class you want by changing the Priority of the library in the list. The up arrow and down arrow buttons, above and below the Priority on the References dialog box (refer to Figure B-1), will move the library selected in the Available Libraries list up or down in the list. Move the library containing the class you want higher than libraries containing the same class name.

You can also avoid problems with duplicate class names by making a specific reference to the library that contains the class you want to use. For example, if you reference both the ADO and DAO libraries, and you want to declare a variable for the Recordset class of ADO, you can declare your variable using the following syntax.

Dim rsADO as ADODB.Recordset

If you want to declare a variable for the DAO Recordset class, you can use the following syntax.

Dim rsDAO as DAO.Recordset

Using this syntax does not eliminate the need to have the reference to the library in your Reference list. But it will prevent any confusion about which library you are referring to in your variable declaration.

B.2.2. The Object Browser

After you have added a reference to a library, the classes contained in that library are available for viewing in the Object Browser. To see the Object Browser in the Visual Basic Editor, press F2 or select View | Object Browser from the menu. The Visual Basic Editor will display the Object Browser as shown in Figure B-2.

Notice that when you select an item in the Classes list, the properties and methods of that class display in the right-hand list. Select an item in the right-hand list to display more specific information about the item. For example, Figure B-2 shows that vbTextCompare is a constant with a value of 1 and is a member of VBA.VBCompareMethod.

Figure B.2. Figure B-2

You can specify which of the referenced library you want to browse by choosing it from the library selection drop-down list. Notice the list starting with <All Libraries> in Figure B-3.

If you are looking for a particular class, property, method, or declared constant, you can specify a portion of the string to search for and click the Search button (the binoculars) to find it. Figure B-4 shows the results of searching for recordset. Notice that since <All Libraries> is selected the class recordset is listed twice.

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

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