referring to global variable

T

TMGreen

I have defined a global variable in a module, populated it from some called
code in that module. How do I reference that global variable to be used
throughout the application?
 
D

Douglas J. Steele

TMGreen said:
I have defined a global variable in a module, populated it from some called
code in that module. How do I reference that global variable to be used
throughout the application?

Simply by referring to it by name.

Make sure, though, that the global variable is declared in a module, not a
class module, or a module associated with a form.

Also, be aware that there can be problems relying on global variables:
certain errors can cause their values to get reset.
 
T

TMGreen

I had tried just referring by name but it comes up null though I know when I
leave the previous module (which is public in case that matters) it is there

I saw someone suggested instead placing the value you are needing back on
the forms as hidden fields. Would that be a recommended alternative to
global variables? I'm trying to understand why they are touted as unreliable
(i've never used them before)
 
B

Bob Bonta

Where are you trying to access the global variable? By nature, a global
variable is available to both public and private modules - hence the global.

I have found, however, the global variable is not available in a query (SQL
statement).

Once declared as global (not in a class or form module as DSteele stated) if
can be populated from anywhere prior to its reference for use and should be
available in any VBA module, form, or class module.
 
T

TMGreen

I am referencing it in a public module and ideally would like to be able to
reference it in a private module as well. I know it is not getting "reset" -
it's just "lost" in laa laa land. I've tried just referring to it in various
places, but it's always null.
 
R

Rick Brandt

TMGreen said:
I am referencing it in a public module and ideally would like to be
able to reference it in a private module as well. I know it is not
getting "reset" - it's just "lost" in laa laa land. I've tried just
referring to it in various places, but it's always null.

Where did you declare the variable? At the top of a module (correct) or
inside of a sub or function (incorrect).
 
T

TMGreen

it's declared at the top of a module under option explicit

Option Compare Database
Option Explicit
Global projnbrbeg as string

is that correct?
 
S

Sreedhar

Hi TMGreen,
"Douglas J. Steele" wrote:

As Doug says, certain errors just reset the value of a global variable and
hence, if you are looking forward to using its value for an entire session
with your App, it is unreliable to the extent of how thorough you are with
error handling.

Usually, I prefer to use the global variables to store values for immediate
consumption and for those which obviously cannot be declared at
Procedure-Level or at Module-Level.

For longer sessions, such as storing the User's Name (other than Network
login name), which needs to be stored for a full session with the db, I just
hide the sign-on form after validating the signon, and get the UserName value
from the hidden form.
 
D

Douglas J. Steele

Is that the only place it's declared? Have you perhaps also declared it in
whatever routine it is where you're setting it, or where you're trying to
use it? If so, the local declaration overrides the global one.
 
T

TMGreen

thanks to everyone. Based on various post suggestions here I reworked the
code and though I didn't get the global variable to share the value, I
approached it differently and solved the problem. Everyone helped a ton!
thanks! I always get fabulous answers here!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top