Public myvariable as long (or whatever).
You use public where you would otherwise use Dim.
You can declare public variables in any standard module; most developers would create a specific standard module (Often named 'Globals') to contain (just) all public variables so that the declarations are easy to locate.
In your globals module you declare the variable.
You then set it to a value or get its current value anywhere you like in code (extra work needed for saved queries).
If you are getting blanks it suggests that you are never running the code that sets it to a value.
Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886 As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
In case you haven't quite figured out your global variable yet, I went throught the same process a few weeks ago and here's how I handled it. I created a seperate module 'Globals' and declared 'Public FYFinder As Integer', then I set the variable in in one form's module 'FYFinder = Me.txtFY'(this in a Private Sub) and used it in another form's code module Private Sub as 'Select Case Globals.FYFinder'.
Notice that I used the module name dot variable to find it. Hope this helps.
Using global variables is fine, but there is a danger. If you set the variable in more than one place, then one control / section of code could over write the variable elsewhere.
Other options...
- Use one function to update a global variable to prevent accidental updates
- You can pass variables from sub routine to sub routine.
- You can use a "Static" datatype - especially useful from a central function or module.
Well, I've just been researching how to work with global variables, and found this thread. Concerning the June 30 reply by willir (Richard) above, I'd much appreciate if somebody would explain how this is done:
"- You can pass variables from sub routine to sub routine."
Now I do sometimes use functions from external modules that take variables in subroutines, but it sounds like he is talking about something much more powerful here.
Barring more on this, I'm ready to start trying out globals. Until now I've been using a technique of setting the value of an invisible text box, then taking its value in another form or report's subs. Thanks if anyone can shed more light here.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.