MS-Access / Getting Started

Testing Reference Fix-Up

Before we can test this routine, we need to do some setup work; namely, we need to create a library database and reference it from the database created in the previous section.

Create the Library Database

To create the library database that will be used by our reference fix-up code, create a new database named LibraryTest.accdb and add a new standard module. Save the module with the name My. Add the following code to the module in the library database:

Public Property Get Name() As String
    Name = Environ("USERNAME")
End Property
Public Property Get Computer() As String
    Computer = Environ("COMPUTERNAME")
End Property
Public Property Get CurrentForm() As Form
    Set CurrentForm = Screen.ActiveForm
End Property
Public Property Get Project() As CurrentProject
    Set Project = CurrentProject
End Property
Public Property Get Library() As CodeProject
    Set Library = CodeProject
End Property

Call Code in the Library Database

After you save the code in the library database, re-open the database that contains the EnsureReferences function written earlier. Open the USysTblDependentFiles table and add the LibraryTest.accdb file in the Attachment field.

Insert a new standard module in the first database that will call code in the library database. Add a reference to the LibraryTest.accdb database, and then add the following code to the module:

Sub TestLibraryCode()
    ' calls code in the library
    Debug.Print My.Computer
    Debug.Print My.Name
    Debug.Print My.Library.FullName
    Debug.Print My.Project.FullName
End Sub

Run the code to verify that it works.

Break the Reference

To test that the EnsureReferences function works, close the database, and then rename the LibraryTest.accdb file you just created. Re-open the database and run the TestLibraryCode routine. The code should fail with a missing reference error. Now, run the EnsureReferences routine and re-run the TestLibraryCode routine. You should have a copy of the library database in the directory where the database resides and the routine should run successfully.

Any breakpoints in code will not work when you manipulate the VBA project programmatically, If you have a breakpoint set, you may see the message "Can't enter break mode at this time" when the reference is removed.

[Previous] [Contents] [Next]