Chapter 24: Optimizing Access Applications
853
After a database is referenced, you can call the procedures in the referenced database as if they
exist in your application database. The referenced database always stays in a compiled state unless
it’s directly opened in Access and modified.
Creating a library reference for distributed applications
If you’re distributing your application, references stay intact only if the calling database and the
library database are in the same path. For example, if the main database is in C:\myapp on your
machine, and if the library database is in C:\myapp\library, the reference remains intact as
long as the library database is located in C:\myapp\library. If the path won’t remain consis-
tent upon distribution, your application’s users must manually adjust the reference or you must
create the reference with VBA code.
The following procedure creates a reference to the file whose name is passed as an argument. In
order for this function to work, the full filename with path must be passed:
bResult = CreateReference(“C:\My Documents\MyLib.accdb”).
The function is
Public Function CreateReference(strFileName As String) _
As Boolean
Dim ref As Reference
On Error GoTo HandleError
Set ref = References.AddFromFile(strFileName)
CreateReference = True
ExitHere:
Exit Function
HandleError:
MsgBox Err & “: “ & Err.Description
CreateReference = False
Resume ExitHere
End Function
Tip
You can verify that a reference is set by using the ReferenceFromFile function. To verify a reference, pass
the function, the full path, and the filename like this:
bResult = ReferenceFromFile(“C:\Windows\System32\mscal.ocx”)
Here’s the function, which returns True if the reference is valid and False if it isn’t:
Public Function ReferenceFromFile(strFileName As String) _
As Boolean
Dim ref As Reference
On Error GoTo HandleError
For Each ref In References
If StrComp(ref.FullPath, strFileName) = 0 Then
ReferenceFromFile = True
Exit For