Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2007

Tip: Looking for answers? Try searching our database.

referring to global variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TMGreen - 29 Jan 2007 20:31 GMT
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?
Douglas J. Steele - 29 Jan 2007 20:53 GMT
>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.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

TMGreen - 29 Jan 2007 21:10 GMT
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)

> >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
[quoted text clipped - 7 lines]
> Also, be aware that there can be problems relying on global variables:
> certain errors can cause their values to get reset.
Bob Bonta - 29 Jan 2007 21:34 GMT
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.

Signature

~ Bob Bonta ~

> 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
TMGreen - 29 Jan 2007 22:05 GMT
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 are you trying to access the global variable?  By nature, a global
> variable is available to both public and private modules - hence the global.  
[quoted text clipped - 8 lines]
> > 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
Rick Brandt - 29 Jan 2007 22:15 GMT
> 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).

Signature

Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt   at   Hunter   dot   com

TMGreen - 29 Jan 2007 22:31 GMT
it's declared at the top of a module under option explicit

Option Compare Database
Option Explicit
Global projnbrbeg as string

is that correct?

> > 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
[quoted text clipped - 3 lines]
> Where did you declare the variable?  At the top of a module (correct) or
> inside of a sub or function (incorrect).
Rick Brandt - 29 Jan 2007 23:46 GMT
> it's declared at the top of a module under option explicit
>
[quoted text clipped - 3 lines]
>
> is that correct?

Yes.

Signature

Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt   at   Hunter   dot   com

Douglas J. Steele - 30 Jan 2007 12:44 GMT
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.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> it's declared at the top of a module under option explicit
>
[quoted text clipped - 11 lines]
>> Where did you declare the variable?  At the top of a module (correct) or
>> inside of a sub or function (incorrect).
Sreedhar - 30 Jan 2007 06:26 GMT
Hi TMGreen,

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

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.
Signature

Sreedhar

> 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
[quoted text clipped - 3 lines]
> global variables?  I'm trying to understand why they are touted as unreliable
> (i've never used them before)
TMGreen - 30 Jan 2007 20:36 GMT
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!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.