Classes built directly in a library database are not visible outside of the library in VBA. However, it is possible to get the classes exposed so that they can be seen and used outside of the library.
The easiest way to do this is to:
Export the class to a text file
Open the class module with a text editor
Look for two attributes at the top of the module. Change Createable and Exposed to True.
Save the class and close the text file
In the library, click Insert / Class module
In the Editor window that opens, click Insert / File. Navigate to and select the text file that you saved the class in. This will cause Access to pull in the contents of whatever file you selected and insert it in your class. In this case it is the contents of the modified class.
At the top of the class you will see something like
Option Compare Database
Option Explicit
Version 1.0 class
Begin
MultiUse = -1 'True
End
Option Compare Database
Option Explicit
Select and delete everything from the first Option down through the End
Compile and save the class
The class will now be visible from any database the references the library.
Example:
You need to set the instancing property of the classes to 2-PublicNotCreateable (as explained above). Here's a simple class.
Option Compare Database
Option Explicit
Dim mstrIsWorking As String
Function FlipSwitch()
If (mstrIsWorking = "T") Then mstrIsWorking = "F" Else mstrIsWorking = "T"
End Function
Public Property Get IsWorking() As String
IsWorking = mstrIsWorking
End Property
Public Property Let IsWorking(strTF As String)
mstrIsWorking = strTF
End Property
Then create a wrapper function in the library database to instantiate the classes for you, a sort of "CreateObject()" method for your classes:
'Standard Module in Library database
Private mclsLibraryTest As clsLibraryTest
Public Property Get GetLibraryClass(ByVal strClassName As String) As Object
Select Case strClassName
Case "clsLibraryTest"
Set GetLibraryClass = New clsLibraryTest
Case Else
Set GetLibraryClass = Nothing
End Select
End Property
In another database that references the Library database, you can instantiate a class like this:
Sub TestLibClass()
Dim lt1 As clsLibraryTest
Dim lt2 As clsLibraryTest
Dim lt3 As clsLibraryTest
Set lt1 = GetLibraryClass("clsLibraryTest")
Set lt2 = GetLibraryClass("clsLibraryTest")
Set lt3 = GetLibraryClass("BogusClassName")
Debug.Print "1st instance: " & lt1.IsWorking
lt1.FlipSwitch
Debug.Print "1st instance: " & lt1.IsWorking
Debug.Print "2nd instance: " & lt2.IsWorking
Debug.Print "1st instance type: " & TypeName(lt1)
Debug.Print "2nd instance type: " & TypeName(lt2)
Debug.Print "3rd instance type: " & TypeName(lt3)
End Sub
Here's the output:
Immediate Window
1st instance: False
1st instance: True
2nd instance: False
1st instance type: clsLibraryTest
2nd instance type: clsLibraryTest
3rd instance type: Nothing