-
Scope and Lifetime of Class object
I have a main form in an Access 2000 application that acts as a menu. I would like to declare a class object and a collection as a global variable so that I can access the collection from numerous Form class objects. I have tried to declare the object as public on the main menu and instantiate them on the Load event, but I have not been able to access the object collection from another form. Any assistance would be greatly appreciated.
-
Go to Modules in MS Access, create a new module, then publically declare the thing that you want to manipulate from there.
Let's say you create Module1, and in Module1 you declare your collection. You can then set routines up in Module1 to create and destroy your collection as needed (Class modules acting as "wrappers" for your collection with class_terminate and class_initialize subroutines in them are even better, but one step at a time).
So, if MyCollection is declared public as type COllection in Module1, you can then specify the collection from any form as:
Module1.MyCollection
You'll have to set the collection to instantiate it, probably from your main module:
Set Module1.MyCollection = new collection
Any form after that can access MyCollection.
-
Thank you for the enlightened Response
I will keep your suggestion in mind for future applications. I had a class object that consisted of some user profile specifications, that I instanciated on load from the main menu. The object is declared as a global from the main form "fmnuMain" as:
Public objProfile as clsProfile
Private Sub fmnuMain_Load()
Set objProfile = New clsProfile
'Load objProfile with user data
End Sub
Since the form "fmnuMain" remains open throughout the applications session, I can access the class object from any other form. My initial problem was that I was unable to address the class object where I have since found the resolution through the precedance. To access:
Forms!fmnuMain.objProfile.User_Name
The application is only open to one user at a time so a collection is not required.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|