Make a global variable in Visual Basic for Applications - vba

In VBA for Access, how do I make a global variable that I can access and change anywhere?

You can declare a variable at Global scope by declaring it in a Standard Module or a predeclared class module such as a Worksheet, UserForm or PreDeclared class, using the Public keyword:
Public myVar As String
You can also declare a Global variable using the now deprecated, but still valid Global syntax, which is functionally the same as Public
Global myVar As String
But note that declaring a variable with Public or Global will make the variable accessible across your entire project AND to any project that refers to that project, and even if your project is protected, a user could still query the variable from the Immediate window.
If you must have a variable that is available across your project, but only that project, then you should declare the variable in a standard module, and include an Option Private Module statement to make the module private to the project, but its variables as Public to the project only.
Option Private Module
Public myVar As String

Related

Vba global variable declaration

In vba how to declare a global variable, which will be assigned from userform, but then used inside various sub procedures, functions in different modules?
Declaration of var (dim) above in module does not let from other modules to call the value of variable
You need to declare the variable on top of the first module you are using (outside subs and functions) and add the Option Explicit line above, like this :
Option Explicit
Public (Variable Name) As (Variable Type)
EDIT : Since I can't comment under your answer I'll try my best here.
If I understand right you have a "main" module where you do all your procedures and one userform where you gather data. Then initialize a public variable on top of the "main" module and use it in your userform.

How to create a global function and pass variables to it in VBA Access?

In my program I am trying to cut down on repetitive code by putting it into global functions.
In my current setup I am calling the function like:
Call [Global variable].Close
(global variable is the name of the class module).
This function is looks like;
Public Function Close()
DoCmd.Close
Cancel = True
End Function
Unfortunately this wont compile properly. What am I doing wrong?? (As an aside I also want to pass variables to and from this function and want to know how this would be done).
Thanks for your help.
If you're using a class module, you need an instance of that module before you can call its members. That requires either creating the class module as pre-declared (which involves exporting the code, editing in Notepad, and re-importing), or creating an instance with the New keyword:
Dim myClassInstance As MyClass
Set myClassInstance = New MyClass
myClassInstance.Close
But, depending on your use-case, you might better off with the function being available in a standard module - then you can just call it from anywhere, including from within a query.

Access Global not working

I have a Global Variable called EmployeeID. I set this by calling a function:
Global EmployeeID As Integer
Public Sub init_Globals(emp As Integer)
EmployeeID = emp
End Sub
On the main form, I used:
MsgBox EmployeeID
from a button and it works just fine; but the funny thing is when I use this from a subform it returns a 0:
I might add that this is a Continuous Form as well.
Private Sub cboWhichDate_AfterUpdate()
Me.txtEmployee = EmployeeID
End Sub
A "global" variable can only be "global" if its scope is global. Sticking a Global access modifier to a field declared in a class module doesn't make it global.
In fact, the Global keyword is deprecated, and is completely replaced/superseded by the Public access modifier.
What matters is the variable's scope. If that declaration is located in a class module (a form's code-behind is essentially a class module), then all you've done is declare a public instance field; you can only access the value via the object instance that owns it.
Dim MyObject As New MyForm
MyObject.EmployeeID = 42
MsgBox MyObject.EmployeeID
To make a global variable, add a standard module (.bas) to your project, and declare it there.
Public EmployeeID As Integer
Now everyone everywhere can access EmployeeID, because it's scoped to a module (i.e. not an instance of an object), and its accessibility is public.
That said, global variables are evil and can easily lead to unmaintainable spaghetti code; Consider passing values as parameters instead.

VBA - Global Variable Scope?

I'm struggling here a bit.
I had created a few Public variables on a form (MainForm). I now realize I need to expand the scope of some of these variable to additional forms.
So, I moved the Public declarations to a new Module called "Globals".
When I attempt to run the new form, I am getting a complaint about an ambiguous variable as I attempt to reset its value to "". No complaint from the "MainForm" using the same variables.
Module "Globals"
Public myIP As String
Public myStatus As String
New Form:
myStatus = "" << throws an err
Am I correct to move the Public variable declarations to a Module?
Any thoughts about the "ambiguous variable" err from that new Form??
Thanks!
You may be trying to declare the same variable in the same scope. See here for more details, also a SO thread related to this here.
As a side note, I notice with Excel VBA you must appropriately qualify a module-level variable if a procedure-level variable is declared with the same name.

What is a public object module in VBA?

I'm trying to get as close to function pointers / abstract classes as I can in VBA.
I have a class called VerificationManager and verifies a bunch of cells in a couple of spreadsheets match up. This will be done in different ways depending on the information and spreadsheets being used with it.
I'd like to be able to make the code reusable by specifying a method to be called in a string using the Application.Run function. So I can rewrite the function that changes.
Now if I was using Java or C# I would be able to extend an abstract class and rewrite the internals to the function. If I was using JavaScript I could store a function in a variable and pass the variable to the class and call it from there.
Inside my class I have a public property called "verificationModule" which I set to the name of the function I want it to call.
Sub VerifyWorkLocations(empLoc As EmployerLocation)
...
For i = 0 To empLoc.numOfEmp
Application.Run verificationModule, empLoc.taxdescmatch, empLoc.employees(i)
Next i
...
End Sub
However, when I try to call Application.Run I receive the following error:
Compile Error:
"Only user-defined types defined in public object modules can be
coerced to or from a variant or passed to late-bound functions"
I already tried placing my User Defined Types in a Class Module but it basically said that a class module was the wrong place for a type.
The error comes from full-fledged VB, where you can create an ActiveX dll project, create a public class there and put a UDT into that class.
In VBA, you use classes instead of UDTs when you need to coerce to or from a variant.
So just declare a class with all the fields you have in your UDT, and delete the UDT.
Alternatively, create a DLL in VB6 that would only contain the declaration of the type, and reference that dll from VBA. Or, if you're comfortable with IDL, just create a TLB file directly.
To add a module to a VBA application, try the following steps (assuming you've already entered the VBA IDE):
From the Project Explorer, right-click on your project.
Click on "Insert..."
Select "Module."
If no modules exist, a new folder within the VBA/Excel project will be created, named "Modules," and a module with a default name of "Module1" will be created.
Double-click the module to open it in the source editor.
This is where you have to place UDT's. I believe this is because of the difference in the way such types are stored internally by VBA relative to the COM-style structure of elements/objects declared/manipulated as Classes...