You can use the SetTempVar action 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.
The SetTempVar action has the following arguments.
||Enter the name of the temporary variable.
||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.
- 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 (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.), a Visual Basic for Applications (VBA) (Visual Basic for Applications (VBA): A macro-language version of Microsoft Visual Basic that is used to program Microsoft Windows-based applications and is included with several Microsoft programs.) 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:
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 action in a VBA module, use the Add method of the TempVars object.
The following macro demonstrates how to create a temporary variable by using the SetTempVar action, then using the temporary variable in a condition and a message box, and then removing the temporary variable.
Expression: InputBox("Enter a non-zero number.")
Message: ="You entered " & [TempVars]![MyVar] & "."