Reputation: 11
I'm sort of new to MS-Access & VBA programming. I have developed a multi-user application in MS-Access 2016 and VBA and converted to accdb.
I need to get the value I stored in the Tag property of each form I create. The forms are not loaded, but I need to get the value from a module where a superuser of the application can grant/remove user's access to any particular form. Right now I'm using the form's name instead, but form's name doesn't give the same level of detail that I a can store in the Tag property.
Is there a way I can access/get the Tag property value of an unloaded form?
Upvotes: 1
Views: 1258
Reputation: 71217
Disclaimer: I'm not familiar with MS-Access at all.
A form is essentially a class module, and a class module is nothing but a "blueprint" for an object - it's a type.
When you interact with a form, you're actually interacting with an instance of that form - an object. This is true even when you don't create the instance yourself (using the New
keyword), because forms have a special attribute (which you can see if you export the module and view it in your favorite text editor), VB_PredeclaredId
, which is set to True
- this makes VBA create a global/default instance for you, an object that's named after the type.
So when you assign MyAwesomeForm.Tag
, you're working with the default instance - an object, not a type. This is potentially confusing, because VBA names the object exactly like the type.
Because the instance is global, and managed by VBA, you can do this:
MyAwesomeForm.Tag = "ABC"
Unload MyAwesomeForm
Debug.Print MyAwesomeForm.Tag 'should print nothing, but won't crash either
Merely accessing the Tag
property of an unloaded global instance will load the object!
And this takes us to your question:
Is there a way I can access/get the Tag property value of an unloaded form?
Absolutely not. Because you'll load an instance of the form simply by accessing the Tag
property. And it will be empty, because whatever value you set before was set on another object of the same type.
Tag
is an instance property: it belongs to an instance, not to the form type. That means you can have this:
MyAwesomeForm.Tag = "123"
Dim form1 As MyAwesomeForm
Set form1 = New MyAwesomeForm
form1.Tag = "ABC"
Dim form2 As MyAwesomeForm
Set form2 = New MyAwesomeForm
form2.Tag = "XYZ"
Debug.Print MyAwesomeForm.Tag 'prints 123
Debug.Print form1.Tag 'prints ABC
Debug.Print form2.Tag 'prints XYZ
An object is "loaded" when it's "in memory". An object that isn't in memory, doesn't even exist; so no, you can't read or write a property value to an "unloaded" form.
A form can very well be loaded but not displayed though. If that's what you meant, then the answer is "yes, absolutely!".
Upvotes: 2