Using a public or a global variable

odrap

Registered User.
Local time
Today, 12:38
Joined
Dec 16, 2008
Messages
156
In a form with subform , i have to use a Boolean that can be seen as well in the parent form as well as in the subform.
I like to declare this Boolean such a way that it can only be seen by this two forms? Must it be declared as Global or as Public and where do i have to declare it? In the declaration section of the module of the parent form doesn't seems to solve the problem. Is the only way the use of a global variable?
 
you could store it in a (hidden) control in the mainform

then in the subform to refer to the control its just

parent!myboolean
 
Thank you very much.
To be sure i understand your advice quiet well, am i wright if i put a hidden textbox on the form, call it e.g Nieuw and set the default value of it to "False" as initial value and use it as a replacement of the boolean that otherwise must be declared by a dim or public ...
 
In a new module in the declarations section enter

Public bFlag As Boolean

That's it
 
to dcrake

yeah that works dc, but he said he wanted not to have the variable with project scope

to odrap

yeah, have it available on the main form, but the subform needs to reead it by referring to the form control, so parent!nieuw

you may find you need to intiallise it in the mainforms open event, rather than relying on a default value, as it will be unbound - in which case

niuew = false
 
Gemma
Does it really matter if it is declared at application level or form level if only the forms concerned reference it.

BTW this post is a continuation of an earlier post he made and should have continued there.
 
no, doesnt matter to me, but it is easier to keep track of variables if you limit their scope - especially if they get destroyed when a form or whatever closes
 
Just to point out that declaring a public variable in a form (actually a class module) is not same thing as in a standard module.

In standard module, the public variable is available anywhere and anytime. (Gee, sounds nasty. :p) But in class module, the public variable cannot be called upon directly; it acts like a property instead and thus an object (e.g. an instance of that class) must be referenced first before we can access that variable.

To illustrate:

In a standard module:
Code:
Public foo As Boolean

In a class module. (can be a form module, report module as well) The module is called "MyClass":
Code:
Public bar As Boolean

In another module (doesn't matter whether it's standard or class here):
Code:
foo = True 'will work
bar = True 'will trigger an error

Dim baz As New MyClass
baz.foo = True 'will trigger an error
baz.bar = True 'will work

Dim iter As New MyClass
Debug.Print iter.bar And baz.bar 'Will return false

The debug.print line should illustrate the point that each public variable in a class module can only belong to the actual object and is not shared with other object, even if the object were derived from same class.

In that context, we actually don't need a hidden control to hold a boolean for a form. We can just simply declare one at form module and reference it in same manner as Gemma suggested as it's now a new property of that form.

HTH.
 
banana

so in a form module, what is the name of the class?

is it just the name of the form?
 
Just the name of form or same syntax you use to refer any other form's property.

Code:
Forms!MyForm!bar = True
Form_MyForm.bar = True

Both should work (aircode and untested, though).

(To clarify, I'm assuming we're calling the public variable "bar" from another form or different module. In the form where the variable is declared, it's referenced like any other variable within that module.)
 
but its destryoed when the form closes?

so its not available when the form's closed?

so you need some error checking if you use it other than in a subform say?
 
Yep. As I said, a public variable in a class module acts like a property and thus is dependent on the object being instantiated and alive.

Yep.

Possibly. One way to protect against this would be to ensure that you hold a reference to the form itself first.
 

Users who are viewing this thread

Back
Top Bottom