Part IV: Professional Database Development
1066
FIGURE 30.12
mcrHelloWorldEnhanced uses the SetTempVar action to get a value from the user and display it in a
message box.
Setting a temporary variable
Removing the temporary variable
Using the variable
Cross-Reference
For more information on string concatenation using the ampersand (&), see Chapter 5.
The RemoveTempVar action removes a single temporary variable from memory — in this example,
MyName. You can only have 255 temporary variables defined at one time. These variables stay in memory
until you close the database, unless you remove them with RemoveTempVar or RemoveAllTempVars.
It’s a good practice to remove temporary variables when you’re done using them.
Caution
Using the RemoveAllTempVars action removes all temporary variables created with the SetTempVar
action. Unless you’re sure you want to do this, use the RemoveTempVar action instead.
Temporary variables are global. Once you create a temporary variable, you can use it in VBA pro-
cedures, queries, macros, or object properties. For example, if you remove the RemoveTempVar
action from mcrHelloWorldEnhanced, you can create a text box on a form and set its
Control Source property as follows to display the name the user entered:
=[TempVars]![MyName]
Using temporary variables to simplify macros
Using temporary variables, you can sometimes eliminate steps from a macro. You can get the form
or report name from another control on a form. With a temporary variable, you eliminate the need
for creating a structure of multiple OpenForm or OpenReport actions. You can also use more
than one variable in a macro.