Creating a Reference
A reference is Access's way of locating a library database so that it can use the code in it. You can establish references in four ways:
- Create a library reference
- Create a runtime reference
- Create an explicit reference
- Use VBA code
TIPMuch of the text that follows refers to the Windows registry. You can access the Windows registry using the RegEdit utility. To use RegEdit, select the Run option from the Start menu, and then type RegEdit .
Creating a Library Reference
You create a library reference by adding the library to the Menu Add-ins section of the Windows registry, as shown in Chapter 25 covers add-ins in more detail.
Figure 24.1. You create a library reference by adding the library to the Menu Add-ins section of the Windows registry.

Creating a Runtime Reference
Creating a runtime reference involves establishing a reference to the library at runtime using the Run method of the Application object. This method of creating a reference actually opens the library database and executes the specified function. It uses OLE automation to accomplish this task.The major advantage of this technique is that Access does not load the library code into memory until it is ready to use it. Furthermore, this technique does not require that Access load additional modules in the call stack into memory unless you explicitly call them. Creating a runtime reference does have a few disadvantages, however:
- The library database must have an MDA extension.
- The library database must be located in the path specified in the AddInPath key in the Windows registry. The AddInPath key is located in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Access\Wizards subdirectory of the Windows registry, as shown in Figure 24.2.
Figure 24.2. The AddInPath key is located in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Access\Wizards subdirectory of the Windows registry.
Calling a Function from a Library at Runtime
![]() | The code in Listing 24.1 illustrates how to call a function in a library. Notice that the example calls the IsLoaded function from the library. You can find this code in the CHAP24EX.MDB database on the sample code CD-ROM. |
Listing 24.1 Calling a Function in a Library
Sub AppRun()
If Application.Run("Chap24Lib.IsLoaded", "frmCustomers") Then
MsgBox "Customers Form is Loaded"
Else
MsgBox "Customers Form is NOT Loaded!!"
End If
End Sub
Listing 24.1 uses the Run method of the Application object to call a function called IsLoaded, which is located in the CHAP24LIB.MDA library. You must reference this file with an explicit reference (see "Creating an Explicit Reference," later in this chapter) or you must place the library in the directory you specified in the AddInPath key of the Windows registry. Notice the explicit reference to the name of the library in which the function is located. When using this method of loading a library (without an explicit reference), you must specify the library name.
Using the LoadOnStartup Key
You can add a LoadOnStartup key to the Windows registry. This key provides a means for Access to load a type library when the user loads the database. A type library is not an actual module, but more of a blueprint of what the module looks like. It displays the functions and constants for a specific module. This is helpful because Access can look up functions without having to actually load the module in which you placed the function. Access does not automatically create this key for you. To create the LoadOnStartup key and add an entry to it, follow these steps:
Figure 24.3 shows an example of a completed entry that references the library in the C:\Libraries folder: CHAP24LIB.MDA.
Figure 24.3. This completed entry references the library in the C:\Libraries folder: CHAP24LIB.MDA.

When the user launches Access, Access loads the module and procedure lists of library databases listed under the LoadOnStartup key. When you use the Run method (discussed earlier in the "Creating a Library Reference" section), Access searches for the specified procedure in libraries that you have loaded or referenced. If it does not find the procedure, Access searches any databases listed in the LoadOnStartup key and then locates and loads the required library.As you can see, the LoadOnStartup key can reap the benefits of Application.Run by using the type library. Access can check the functions without loading the actual module until you reference it explicitly through code.NOTEThe LoadOnStartup key is not a panacea. Loading the type library when you load Access does slow down the initial load time for your application. Furthermore, Access uses the memory occupied by the type information regardless of whether you ever actually access the library functions. You must decide whether either of these facts is an issue.
Creating an Explicit Reference
The most common type of reference by far is an explicit reference. You can create this type of reference from any code module in the database referencing the library. To create an explicit reference, follow these steps:
Figure 24.4. The References dialog box is where you create an explicit reference.

When you add a library database to the References dialog box, Access loads the database as a library when you make a call to the library from your code. You can call a library routine just as you would call any subroutine or function. You then can use code in the library database to open forms and other objects stored in the library. Access does not actually load the library database into memory until code in the active application database calls a function or subroutine that is located in the library.Explicit library references impose a few limitations:
- The references you add in a database are available only to that database. Therefore, you must add the library reference to each application database that needs to use the library.
- Access stores the explicit path to the reference. This means that, if you move the library, Access will not be able to resolve the reference. Exceptions to this rule are covered later in this section.
When you call a function that is in a library that Access cannot locate, the message shown in Figure 24.5 appears. The References dialog box shows the library is missing, as shown in the sixth line of the Available References list box in Figure 24.6.
Figure 24.5. A warning message indicates that the library database cannot be located.

Figure 24.6. The References dialog box shows a library flagged as missing.

Although Access might not be able to find a library database that you have moved, it does its best to resolve library references. By default, Access looks in these places to attempt to resolve a library reference:
- The absolute path of the library
- The relative path to the library
- The current folder
- The directory where you installed Access
- The Windows path (Windows and Windows\System folders)
- The PATH environment variable
- The path located in the RefLibPaths key of the Windows registry
A couple of these locations require further explanation. If the library is not located in exactly the same location on the user's machine as it is on your machine, Access searches the relative path to the library next. This means that, if you placed the library in the same directory as the database that references it, or in the same relative location, Access will locate the library database. Suppose you have placed your application in C:\AccessApps\Sales. You have placed the library database in C:\AccessApps\Sales\Libraries. The user installs the application in C:\SalesApp with the library installed in C:\SalesApp\Libraries. In this case, Access can resolve the reference to the library.Another trick when dealing with library databases is to use the RefLibPaths key of the Windows Registry. If you create a key called RefLibPaths in the Windows registry, Access also searches the paths specified under RefLibPaths in an attempt to resolve any references. To use this trick, follow these steps:
This is a good method to use if you will be distributing an application containing several library databases. You can select a location for the library databases and then reference that location in the Windows Registry. You even can create the registry entries programmatically by using Windows API calls or the VBA SaveSetting statement. Figure 24.7 shows the RefLibPaths key with an entry for the Chap24Lib.mda library.
Figure 24.7. Access searches the RefLibPaths key of the Windows Registry to resolve any references.

TIPYou can use the Packaging Wizard to create the RefLibPaths key in the Windows Registry. This is the easiest way to create the RefLibPaths entry, but it requires that you distribute your application using the Packaging Wizard.
Creating a Reference Using VBA Code
With Access 2000 came the capability to create library references using VBA code. You use the AddFromFile method to accomplish this task. You apply the AddFromFile method to the References collection, which is similar to other collections used within Access, and provides a hook to the references associated with a database. The AddFromFile method of the References collection accepts a string as a parameter. The string contains the name of the library reference you are adding. Listing 24.2 shows the code to pass in a library name and then add a reference to it.
Listing 24.2 Locating and Referencing Libraries in Code
Function CreateLibRef(strLibName as String)
Dim ref As Reference
On Error GoTo CreateLibRef_Err
'Create new reference
Set ref = References.AddFromFile(strLibName)
CreateLibRef = True
Exit_CreateLibRef:
Exit Function
CreateLibRef_Err:
Dim intAnswer As Integer
Dim strLocation As String
intAnswer = MsgBox("Library Not Found, Attempt to Locate?", _
vbYesNo, "Error")
If intAnswer = vbYes Then
strLocation = InputBox("Please Enter the Location of the Library")
Resume
Else
CreateLibRef = False
GoTo Exit_CreateLibRef
End If
End Function
The routine begins by invoking an error handler. A reference object is then set to the result of executing the AddFromFile method on the References collection. If the AddFromFile method executes successfully, Access creates the reference, and the function returns a True condition. Otherwise, the code prompts the user whether he wants to locate the library database. If he responds affirmatively, the code prompts him for the location of the library database and the code attempts once again to establish the reference. If he opts not to supply a location, the routine terminates, returning a False condition.