Variables Public and Global

Dick7Access

Dick S
Local time
Today, 16:28
Joined
Jun 9, 2009
Messages
4,291
What is the different between a Public and Global Variable?
Also I am trying to Change the label caption of a report depending on the if statement from a combo box. Am I looking in the wrong direction?
 
Public versus Global

1. Public is spelt differently to Global.
2. Global can’t be used in a Class module. (Only tested in Access 2003)
3. In Access 2003 if we put the cursor on Global, and press F1, we get Public.
4. Microsoft no longer seems to like the word Global.

The information in that link does not compare the two, is incorrect, ambiguous or irrelevant.

Chris.
 
chris may have identified slight nuances, but i think the idea is that global and public are synonymous. These variables have scope through the whole of your application.

I use
public somevar as long

can you use global instead of public?

----
re your report question

in a report try the format event (it may be the print event)

select case comboboxvalue
case 1: somelabel.caption = "whatever1"
case 2: somelabel.caption = "whatever2"
case 3: somelabel.caption = "whatever3"
end select

maybe simpler like this

somelabel.caption = comboboxvalue.column(1)

just depends how it is set to work.
 
My real point is that there are a lot of off topic answers on the www which do not address the specific question.

It does not matter who the people are, they can still go off topic with their answers. It becomes more of a problem when that person has some reputation because that person is perceived as being more believable.

What can then happen is that people can go off building needles workarounds without actually knowing the real problem.

It does surprise me that Jeanette and Armen posted answers that are ambiguous, incorrect or irrelevant.

Chris.
 
For starters, you have to understand the way Access tries to present variables from VBA, which is not quite the same as the way VB6 treats it.

In Access, a variable declared as "PUBLIC" in the declaration area of a class or general module is visible if and only if the entity that declares it is active at the time. You can thus see the public variables on a form when the form is actually open / instantiated. If it is NOT instantiated, its content is not visible (because it doesn't exist.)

An unmanaged trap in Access can often cause PUBLIC variables to lose their values because of the last-chance handler getting involved. While it might not be exactly technically correct, this is how I understand it in overview: The last-chance handler is the trap handler in the code inside of Access itself at the same level as the Access component that activated your database code. Just as returning from a subroutine destroys the context inside the subroutine, so returning to the last-chance handler's context destroys the context called from the same level as the last-chance handler.

The concept of GLOBAL is more of a VB6 issue than a VBA issue in practical terms. Access tries to treat public variables as PROPERTIES of the thing to which they are attached, and those properties are retrievable using the syntax for properties. In fact, if there is ANY ambiguity in the name of the variable, you MUST use the object name holding the correct variable in order to disambiguate the variable. The discussion about declaring property routines to be self-reloading is related to this concept.

In the classic sense of old-time programmers (like white-haired, balding me!) the variables declared in the declaration area of a module ARE global no matter what you call them, because every routine in that module can see them whether they are declared with DIM, PUBLIC, or PRIVATE. The difference is in what else can see them from the outside. Which is why I tend to avoid use of the word GLOBAL in Access context. Maybe there is a newer interpretation, but I'm an old dog, reluctant to learn too many more new tricks before I retire.
 
The original question was:-
>>What is the different between a Public and Global Variable?<<

Public can do this but Global can’t…
Global can do this but Public can’t…

Chris.
 

Users who are viewing this thread

Back
Top Bottom