Solved Global Object

Local time
Yesterday, 21:21
Joined
Aug 10, 2024
Messages
20
Hello,

I noticed that there's a Global (object). I couldn't find any documentation about it. What is the purpose of this Global object?

It appears it has members for the things in the global namespace but, for example, referencing them results in an error.

So, what is the deal with the Global object, what is it used for?
 
An example please?
To me a Global was just a variable that is Global in it's scope, hence the name.
Most variables are Local to their sub/function/class.
 
Perhaps you are talking about the TempVars object?
 
An example please?
To me a Global was just a variable that is Global in it's scope, hence the name.
Most variables are Local to their sub/function/class.
Yes, same here. It seems to be some kind of object. Why is it there? What is its purpose?

That is what I'm thinking when I look at Global. The MS ref does not mention Global so perhaps it's some legacy thing.
 
Yes, same here. It seems to be some kind of object. Why is it there? What is its purpose?

That is what I'm thinking when I look at Global. The MS ref does not mention Global so perhaps it's some legacy thing.

Public and Global are nearly identical in their function, however Global can only be used in standard modules, whereas Public can be used in all contexts (modules, classes, controls, forms etc.) Global comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public.
 
I noticed that there's a Global (object). I couldn't find any documentation about it. What is the purpose of this Global object?
The class Global in the VBA object library contains public ("global") methods to handle (MsForms) UserForms, which are the built in UI windows for VBA applications, other than Microsoft Access applications.
2024-08-16_11h23_41.png


There are similar Global classes in other object libraries, e.g. Excel and Graph. They also serve the purpose of making certain methods globally available inside the VBA project.


I think, you rather mean the <globals> section in the Object Browser. This is not a tangible object even though it is listed in the Classes list. It is just a virtual grouping section that contains all methods and constants that are directly in the global namespace, i.e. can be used without explicitly referencing any object instance.
2024-08-16_11h26_30.png
 
The class Global in the VBA object library contains public ("global") methods to handle (MsForms) UserForms, which are the built in UI windows for VBA applications, other than Microsoft Access applications.
View attachment 115635

There are similar Global classes in other object libraries, e.g. Excel and Graph. They also serve the purpose of making certain methods globally available inside the VBA project.
I see, there's indeed a Global class.
Public and Global are nearly identical in their function, however Global can only be used in standard modules, whereas Public can be used in all contexts (modules, classes, controls, forms etc.) Global comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public.
Ok, now I'm seeing what is going on. If I try to use Public like an object (like I did with Global), e.g., apply the dot operator, IntelliSense offers me completions. This seems to be a quirk of the VBA editor of some sorts. Of course, I do not expect to see this because Public is listed as a statement in the reference (more like a qualifier or keyword to me).

I encountered Global by accident and tried to find out more about it and used it like an object and IntelliSense happily gave me completions which made me believe it is some kind of Object, like Forms for example. All my attempts failed to use it as an object in any way.

So, Global is an artifact of earlier versions in VBA and played a similiar role as Public to declare that the thing lives in the global namespace. However, IntelliSense plays tricks on us if you try to use it like an object and offers completions.
 
I would perhaps re-interpret the "Intellisense playing tricks on us" as actually where someone played tricks on Intellisense.

Generically speaking, the way Intellisense works is you find some object. Objects have generic properties but then the specific type of object has further qualifiers. So Intellisense determines what kind of object you have and looks through a list of properties associated with that object. If it finds the object, it can give you a list with a "Find as you type" option to more rapidly fill in properties.

Where I'm going with this is that in order to actually provide that service, you need what is sometimes called a "symbol table" (a term more often used in compilers) to list the value-like properties and to provide links to related / applicable methods. BUT... if you HAVE a symbol table and it is linked with something identifiable, Intellisense isn't THAT intelligent. It says "Here is an object. It has a symbol table. I can work with that." Even if the object really shouldn't have a symbol table because it really wasn't that kind of object. Intellisense is not discriminatory. Give it a symbol table and it will be happy to use it for you. And if you can find it in Object Browser, it has a symbol table.
 
I would perhaps re-interpret the "Intellisense playing tricks on us" as actually where someone played tricks on Intellisense.

Generically speaking, the way Intellisense works is you find some object. Objects have generic properties but then the specific type of object has further qualifiers. So Intellisense determines what kind of object you have and looks through a list of properties associated with that object. If it finds the object, it can give you a list with a "Find as you type" option to more rapidly fill in properties.

Where I'm going with this is that in order to actually provide that service, you need what is sometimes called a "symbol table" (a term more often used in compilers) to list the value-like properties and to provide links to related / applicable methods. BUT... if you HAVE a symbol table and it is linked with something identifiable, Intellisense isn't THAT intelligent. It says "Here is an object. It has a symbol table. I can work with that." Even if the object really shouldn't have a symbol table because it really wasn't that kind of object. Intellisense is not discriminatory. Give it a symbol table and it will be happy to use it for you. And if you can find it in Object Browser, it has a symbol table.
But that would require the thing before the dot is indeed an object that links to a symbol table. That leads back to my original reasoning where I thought Global is an object because IntelliSense found something on it. IntelliSense does the same thing on Public and Private.

Perhaps it is an object and an implementation detail that leaks through IntelliSense, or IntelliSense is buggy, or there's something more about it (undocumented). Any way to interact with Global treated as an Object causes an error.

Anyways, I'm satisfied with the answers collected here. It is certainly not something of interest to the developer otherwise somebody would have come forward.

PS: I'm new to MS Access/VBA so I'm exploring the platform.
 
That leads back to my original reasoning where I thought Global is an object because IntelliSense found something on it. IntelliSense does the same thing on Public and Private.

As has been pointed out, it is an older term that is no longer recommended to be used as-is.

There is a thing called the VB_Global_NameSpace, but the VBA Language Spec, v20140424, pg 49, 4th bullet, says this:

A variable declaration that is part of a <global-variable-declaration> or <public-variable-declaration> declares a public variable.

I.e. at the indicated version of VBA, Global and Public exactly overlap for most declarations. Earlier, pg 48, we find this Backus-Naur definition of the use of the keyword Global, plus an explanation:

global-variable-declaration = "Global" variable-declaration-list
<global-variable-declaration> and the optional Shared keyword (section 3.3.5.1) provides syntactic compatibility with other dialects of the Basic language and/or historic versions of VBA.

So in current VBA since probably 2003 (just guessing), Global is treated exactly like Public. The VBA Language Spec says Global would work that way not only for variables but also for declared constants and for Enum definitions. Also for sub/function declarations.

Another section of that document says that when defining Class Objects and Modules, the VB_Global_Namespace attribute can be TRUE or FALSE to control the visible scope of variables and therefore involve something that is global by association with a global namespace.
 
Any way to interact with Global treated as an Object causes an error.
This impression was probably created due to the ambiguity between the scope keyword Global and any class named Global.
If you explicitly reference the class, this ambiguity is resolved and it works as you probably expected:

This works:
Code:
MsgBox VBA.Global.UserForms.Count

And this also works:
Code:
    Dim gbl As VBA.Global
    Set gbl = VBA.Global
    MsgBox gbl.UserForms.Count
 
So in current VBA since probably 2003 (just guessing), Global is treated exactly like Public.
It's since 1995 (VB4/Office95) because those versions introduced custom class modules which require the Public keyword instead of Global.
 
It's since 1995 (VB4/Office95) because those versions introduced custom class modules which require the Public keyword instead of Global.

That old? Well, for a while there I wasn't updating my old copy of Access 2.0 so I guessed the conversion was when the internal format changed. But back then I was out of touch for a while. 29 years ago? I didn't actually use Access again until I needed it for work around 2006 or 2007.
 

Users who are viewing this thread

Back
Top Bottom