Previous Page

CHAPTER  12

Using Library Databases and Dynamic-Link Libraries

A library database is a collection of procedures and database objects that you can call from any application. You can use libraries to store routines that you use often, so you don't have to write the same routine for each application you create. You can also use libraries to distribute new features to your users.

A dynamic-link library (DLL) is a file containing procedures that your application can link to and use at run time. This chapter shows you how to create and use your own Microsoft Access library databases, and how to call routines in dynamic-link libraries.

Using Library Databases

When you create an application, the objects and Visual Basic code in the application database work only within that application. For example, you can call functions in the UtilityFunctions module in the Orders sample application only from the objects in the Orders databasethey aren't available from other databases. That works fine for objects and code that you use in only one application. However, you may begin accumulating a set of general-purpose functions that you'd like to be able to call from any application, without re-creating the functions in each separate application database.

You can create a library database to store such general-purpose functions. For example, the IsLoaded function in the Orders database's UtilityFunctions module is a good candidate for a library function. This function doesn't contain any hard-coded names from the Northwind database, such as form names or field names. Instead, you pass it a form name as an argument to determine whether the form is loaded. If you store this function in a library, you can use it with any open database.

You can also create new features for Microsoft Access and use library databases to distribute these features to users. The following sections show you how to create and use library databases of your own.

Creating Library Databases

Anytime you want to make a procedure or feature available to multiple applications, add it to a library database. Here are the basic steps for creating a library in Microsoft Access:

Step One: Write the functions and create the objects   In a new database, write and debug the Visual Basic functions that you want to be available in a library. Design and create the forms and other objects that make up the interface for any generic feature. Because the forms that make up the feature's interface and the Visual Basic functions that make it work are all stored in the library database, they're available in any database from which you establish a reference.

Unlike Microsoft Access versions 2.x and earlier, duplicate function names in different libraries don't result in a conflict. Given two functions with duplicate names in two referenced libraries, Microsoft Access uses the function in the library with the highest priority specified in the References dialog box (Tools menu). If you need to call a function that is in a library lower in the priority list, qualify the function call with the name of the module that it resides in.

Step Two: Load the database as a library   You may want to rename the database so its file name extension is .mda instead of .mdb. (By convention, library databases have a .mda file name extension.) Then add a reference to the library by way of the References dialog box so that Microsoft Access loads the database as a library when you make a call to that library from your code.

When you are selecting databases to reference, click Databases in the Files Of Type box to see a list of files with .mdb and .mda extensions.

Important   You need to establish this reference to every database that will use the library.

After you load the database as a library, you can call the functions in the library database from your application, and you can use code in the library database to open forms or other objects stored in the library database and display them in the current application.

Referencing Library Databases

Microsoft Access doesn't actually load a library database into memory until code in an active application calls that library. If you want to use a library from your application, you must first establish a link, called a reference, from your application to that library database. You can add a reference to one or more library databases in your application by using the References dialog box.

To get to the References dialog box, click References on the Tools menu when a Visual Basic module is open. The following figure shows how you can select libraries to reference.

You can also use the References dialog box to view currently existing references in any open database.

Another way to add a reference to a library database is to use the Reference object and the References collection in your Visual Basic code. To establish a reference in Visual Basic, create a new Reference object with either the AddFromFile or AddFromGUID method of the References collection. Use the AddFromFile method if you know the full path and file name of the file that contains the library you want to add a reference to. Use the AddFromGUID method if you know the library's 16-bit unique identifier. You can retrieve this value from the GUID property of a Reference object after a reference has been established, and use it to re-create the reference if necessary.

See Also   For more information on establishing references by using Visual Basic, search the Help index for "referencing, other databases."

Note   When you establish a reference, the full path to that library database is stored with the reference. If you move the library database to a different folder, or if you change the name of the folder, you need to reestablish the reference. When you are designing an application that will be distributed to other users, you'll want to implement error-handling routines which control what happens when a library database cannot be loaded.

Once you have established your references, you can call functions in the library database from code in your application. Then you can use functions in the library database to open its forms and other objects and display them in the application. Even though you can use the code and the database objects in the library database, they don't appear in the Database window of the open database.

Note   While Microsoft Access supports references to multiple libraries, it doesn't support circular references between libraries. A single library A can establish a reference and call functions in another library B, but as long as that reference exists, library B cannot establish a reference to library A.

Debugging Library Databases

While you're creating objects and writing code in a library database, you open the library as a database and work in it as you would any other database. It's a good idea to get the objects in the library database working the way you want them to and to debug all Visual Basic code before you use the database as a library.

See Also   For information on debugging, see Chapter 7, "Debugging Visual Basic Code."

Sometimes objects and code that work perfectly in the context of the open library database can have trouble running in the context of your application, where new bugs and behavior can emerge. Therefore, after loading the database as a library, you may still need to make some adjustments to your objects and code. If you have problems with your application that appear to be related to the behavior of a library you have referenced, close the current database you are working in and open the library database for testing and debugging.

Running Macros and Actions in Library Databases

You can attach macros to forms and reports in a library database and run them from the library database. This includes menu bar macros that you attach to forms in the library database.

When a library database calls a macro, Microsoft Access looks first for a macro of that name in the library database and, if it finds one, runs that macro. If the library database that called the macro doesn't contain a macro of that name, Microsoft Access looks for the macro in the current database.

Note   It's a good idea to avoid using macros in library databases when possible. Macros don't provide the error-handling capabilities of Visual Basic, so you can't trap run-time errors that may occur when you run a library database macro. In addition, it's much easier to debug a library database that uses Visual Basic rather than macros. You carry out macro actions in Visual Basic by using the DoCmd object. For more information, search the Help index for "DoCmd object."

Certain actions, such as OpenForm and OpenReport, have arguments that specify the object on which the action performs its operation. When you carry out these actions from a library databasewhether in a macro or by using the DoCmd object in Visual BasicMicrosoft Access looks first for an object of that name in the library database that called the action and, if it finds one, performs the operation on that object. If the library database that called the action doesn't contain an object of that name, Microsoft Access looks for the object in the current database.

See Also   For information on how Microsoft Access handles objects referred to by a specific action, search the Help index for the name of the action.

Storing Custom Toolbars in Library Databases

If you want to store custom toolbars for your application in a library database and show or hide them by carrying out the ShowToolbar action from the library, then the database must be loaded as an add-in.

See Also   For more information on installing add-ins, see Chapter 17, "Creating Wizards, Builders, and Menu Add-ins."

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender