Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing classes in an mda library 1

Status
Not open for further replies.

GFullington

Programmer
Oct 7, 2003
18
US
I have created several modules in an MDA library that work great. I am in the process of replacing the modules with class modules. In the calling app, the newly created class does not show up in the objects browser, only the original modules. I tried readding the reference to the library.
Here is what I am trying to do in the main app

Dim objSUBS As clsMdaSubs - it doesn't recognize the class that I have in the MDA.

How do I get the main app to recognize the class?
 
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
 
Thanks FancyPrairie!!
By following the steps outlined, I was able to instantiate the object. Now I can use "pseudo-OO" development! It is interesting that the createable and exposed attributes can only be seen/changed when you export the class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top