Microsoft Corporation
July 1996
The release of Microsoft® Access version 7.0 afforded new functionality to application development in the form of reusable database code repositories. These databases are similar to other Microsoft Access application databases, except that they contain referenced objects.
A new model for accessing external objects exists by using the References option on the Tools menu. When you design a code database, you specifically expose the functions through the use of the Public keyword in all routines you want to access externally.
Take, for example, a function that you use in all of your database applications. Here's one that returns the file-name portion of an absolute path:
Public Function ParseFileName(strFName As String) As String
On Error GoTo ParseFileName_Err
Dim strChar As String
Dim strTemp As String
Dim i As Integer
' Get the file name portion of the passed string
For i = Len(strFName) To 1 Step -1
strChar = Mid(strFName, i, 1)
If strChar = Chr$(92) Then
ParseFileName = strTemp
GoTo ParseFileName_End
End If
strTemp = strChar & strTemp
Next i
ParseFileName_End:
Exit Function
ParseFileName_Err:
MsgBox Err.Description, vbCritical, "modMyReusableCode.ParseFileName
Resume ParseFileName_End
End Function
Now you have created the needed components to reuse your functions. Let's use the ParseFileName() function from another database.
Private Sub Command0_Click()
On Error GoTo Command0_Click_Err
MsgBox CurrentDb.Name
MsgBox ParseFileName(CurrentDb.Name)
Command0_Click_End:
Exit Sub
Command0_Click_Err:
MsgBox Err.Description, vbInformation
Resume Command0_Click_End
End Sub
c:\MSOffice\Access\Samples\Northwind.mdb
Northwind.mdb
The first message box reflects the full path, and the second shows the database name only after passing through the use of the ParseFileName() function.
The other objects in a referenced database, such as the forms and reports, may be accessed from the calling database, but the context may not always be applicable to the current database application. One useful aspect of the repository database concept is to create objects that you use over and over and bring them into the current database for customization. The next example takes a basic form design, in this case a company splash dialog, and imports the object into the current database.
Public Function GetMySplashForm(strDbName As String, _
Optional strNewName As Variant) _
As Boolean
On Error GoTo GetMySplashForm_Err
' Export back the splash form to caller database.
DoCmd.CopyObject strDbName, _
IIf(IsMissing(strNewName), "frmMySplashForm", strNewName), _
acForm, _
"frmMySplashForm"
GetMySplashForm = True
GetMySplashForm_End:
Exit Function
GetMySplashForm_Err:
MsgBox Err.Description, vbCritical, "modMyReusableCode.GetMySplashForm"
Resume GetMySplashForm_End
End Function
? GetMySplashForm(CurrentDb.Name)
When you run the code, the frmMySplashForm form will be imported into the sample database Northwind.mdb.
If you set a reference to a type library from Microsoft Access and then move the file that contains that type library to a different directory, Microsoft Access will attempt to locate the file and re-establish the reference. If the RefLibPaths key exists in the registry, Microsoft Access will first search there. If there is no matching entry, Microsoft Access will search for the file first in the current directory, and then in all the directories on the drive. You can create the RefLibPaths key using the registry editor in Microsoft Windows® 95 or Windows NT®, under the registry key \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Access\7.0. For more information about using the registry editor, see the Windows 95 or Windows NT documentation.
To reference a specific repository database:
Let's assume that our database MyCode.mdb is located at c:\MSOffice\Access\Samples\MyCode.mdb
The techniques described in this article provide you with examples of how powerful a development concept using repository databases can be. There are a few important considerations to keep in mind when you implement referenced databases.
This is a limitation of Microsoft Access version 7.0. The reference must be set using the user interface of the full retail product and cannot be set with the run-time version of Microsoft Access.
Ensure that any functions that you expose for use in your repository databases have unique names and do not conflict with the current database or other repository databases.
Ensure that you have effective error handling in the repository databases and that the databases are compacted and placed in a compiled state before you use them.