Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare a public variable

Status
Not open for further replies.

MrBaRRon

Programmer
Aug 8, 2003
39
IT
Hi everybody,

I have to declare a Globale variable in VBA so that I can reuse it in another function (sub).

What is the syntax please ?
Can I declare it everywhere in my project ?

Thank U
 
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.
 
Ahhh

Thank U very much. It's clearer now.

Thanks ; )
 
I need more details because when I call the variable from the report code by example (with msgbox) , the value is blank and not the one I set.

 
I don't know what else to say..

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.
 
faq705-5319 might provide you some insight.

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.
 
There is no need to use the module name unless you have a local variable of the same name.
 
I think it will help me.

Thank U very much ag ain for your help ; )

 
MrBaRRon

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.

Richard
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top