In VB6/VBA, you can declare module-level variables outside of a specific Sub
or Function
method. I've used Private
and Public
before inside modules and understand them like so:
Public
- visible to all code inside the module and all code outside the module, essentially making it global.Private
- visible only to code inside the module.
I've noticed that you can use Dim
and Global
as modifiers for modular variables. Are Dim
and Global
different from Private
and Public
, respectively, when used as access modifiers on modular fields? If so, how are they different?
Dim
andPrivate
work the same, though the common convention is to usePrivate
at the module level, andDim
at the Sub/Function level.Public
andGlobal
are nearly identical in their function, howeverGlobal
can only be used in standard modules, whereasPublic
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 byPublic
.