# Solved: Declaring Global Variables in Access 2007



## SimonB (Sep 21, 2004)

Access 2007 is supposed to handle global variables much better than previous versions, but I can't seem to find any instruction on how and where to declare them. I am working in multiple form modules, and I want a variable that will stay active, with a set value that can be changed from time to time as needed, throughout the Access session, from one form to another. Can someone help me with this?
Thanks, SimonB


----------



## OBP (Mar 8, 2005)

Simon, isn't it the same as previous versions, i.i. in a module?


----------



## Rollin_Again (Sep 4, 2003)

Declare the variable in a stand alone module and use the following syntax.


```
Public myGlobalVar As String
```
Regards,
Rollin


----------



## SimonB (Sep 21, 2004)

In my digging over the last hour or so there seems to be a new collection (my symantics may be wrong here) of "Temporary Variables" that remain in memory until you close the database. It's a collection of variables that can be used application-wide and survive code resets, unlike previous Access versions. Example: In AutoExec macro, SetTempVar named LicensedTo to "Acme Corporation", and add this text box to each report header: =[TempVars]![LicensedTo]. I've been able to declare the TempVar, but can't find how to change its value.

Here's what I have found. It's interesting.
http://blogs.msdn.com/thirdoffive/archive/2006/04/13/570539.aspx 
http://www.allenbrowne.com/Access2007.html (search for "TempVar")


----------



## SimonB (Sep 21, 2004)

OK, looks like I may have got it. This is the syntax:
This declares the global(TempValue) variable:
TempVars("tempvariablename").Value = initialvalue (the ".Value" is optional)
(you have to give it a value, and do not declare a type)
This is the syntax for changing that value anywhere you need to:
[TempVar]![tempvariablename].Value = new-value 
(it seems you need to specify the parameter ".Value" here)
And this is how to use the TempVariable elsewhere:
variablename = [TempVar]![tempvariablename].Value
Thanks, anyway, gang.
Best, SimonB


----------



## SimonB (Sep 21, 2004)

I thought I had it, but it seems now I don't. Here's what I am doing:
I declare the TempVars this way:
TempVars("LoginAttemptCounter").Value = Val("0")
TempVars("MaxAllowedLoginAttempts").Value = Val("3")
Then I loop through a process of validating login credentials, with this check in it:
If [TempVar]![LoginAttemptCounter] < [TempVar]![MaxAllowedLoginAttempts] Then...
The compiler doesn'y like this statement. My syntax is bad, somehow. Does anyone see the problem?
SimonB


----------



## OBP (Mar 8, 2005)

Simon, wouldn't it be better to assign the TempVar to a declared Variable in you current procedure and use that for comparisons.


----------



## SimonB (Sep 21, 2004)

OBP,
Someone sent me the solution. Instead of this to refer to the TempVar:
[TempVar]![tempvariablename].Value
...use this:
TempVars("tempvariablename").Value
It works.
Thanks for your help.


----------

