You can use the SetTempVar macro action in Access desktop databases to create a temporary variable and set it to a specific value. The variable can then be used as a condition or argument in subsequent actions, or you can use the variable in another macro, in an event procedure, or on a form or report.
Setting
The SetTempVar macro action has the following arguments.
Action argument |
Description |
Name |
Enter the name of the temporary variable. |
Expression |
Enter an expression that will be used to set the value for this temporary variable. Do not precede the expression with the equal (=) sign. You can click the Build button to use the Expression Builder to set this argument. |
Remarks
-
You can have up to 255 temporary variables defined at one time. If you do not remove a temporary variable, it will remain in memory until you close the database. It is a good practice to remove temporary variables when you are finished using them. To remove a single temporary variable, use the RemoveTempVar action and set its argument to the name of the temporary variable that you want to remove. If you have more than one temporary variable and you want to remove them all at once, use the RemoveAllTempVars action.
-
Temporary variables are global. Once a temporary variable has been created, you can refer to it in an event procedure, a Visual Basic for Applications (VBA) module, a query, or an expression. For example, if you created a temporary variable named MyVar, you could use the variable as the control source for a text box by using the following syntax:
=[TempVars]![MyVar]
Note: In macros, queries and event procedures, you do not need to precede the expression with an equal sign.
You can also refer to temporary variables in any add-ins or referenced databases.
-
To run the SetTempVar macro action in a VBA module, use the Add method of the TempVars object.
Example
The following macro demonstrates how to create a temporary variable by using the SetTempVar macro action, then using the temporary variable in a condition and a message box, and then removing the temporary variable.
Condition |
Action |
Arguments |
SetTempVar |
Name: MyVar Expression: InputBox("Enter a non-zero number.") |
|
[TempVars]![MyVar]<>0 |
MsgBox |
Message: ="You entered " & [TempVars]![MyVar] & "." Beep: Yes Type: Information |
RemoveTempVar |
Name: MyVar |