Access 2007 Variable declarations - Variable available to any sub that calls for it

AAlpha1
AAlpha1 used Ask the Experts™
on
A variable is calculated in a Sub, Let's call it VarForAll
Another Sub needs the value previously calculated for VarForAll. I don't want to pass it as an argument.
Where and How do I declare VarForAll so any sub can use it.
Should the sub asking for VarForAll be private or Public?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Quality Lead Engineer
Top Expert 2011
Commented:
Hello AAlpha1,

In a normal code module insert a line after the option statement(s) as for instance:

public VarforALl as integer

Using whatever type is appropriate.  Everyone including forms should now be able to see the variable and whatever it was most recently populated with ... within access code modules at any rate!

Regards,
Chris
Chris Bottomley has given you the correct answer, but I would just like to add that while global variables can be convenient (because you don't have to bother passing them as parameters) they are also a problem precisely because they are global. Which means that code anywhere can alter them and it may be difficult to debug as a result.
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
Commented:
Global variables can also lose their value if you encounter an unhandled error. If you must use global variables, make sure to implement full error handling.

Author

Commented:
Thank you Chris. And, I appreciate the wise warnings from
LambertHeenan: and LSMConsulting:
Albert

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial