How to Set Temporary Value (Set TempVars)

How to Set Temporary Value

While we are using the Access program sometime the same information was used in different place/form even in report. The same information on the new form is related or linked to another form. We can use the method of setting TempVars ( for Temporary variables) on the original form instead of parsing or referencing the value from the original form to a new form.

TempVars is a Variant store that only exists while a given solution/database is open. We can set or store the Temp value to a variable by using VBA and Macro and can be used in Forms, Queries, Reports and code.

Method #1 Set Temporary Value using VBA

We can set the temp value to a variable by using the vb code:
Declare a variable with: Dim tempVariableName (any name) as TempVars
Assign a value to Tempvariable: TempVars!tempVariableName = “David Smith”

1. Set text value (string) or number (integer) to temp variable. The input/value from a text box “txtUserName” is assigned to a TempLoginID on a Login Form. After a correct UserLoginID and Password is verified then close a login form and open a Navigation Form. The LoginID on the Navigation form will display after form is loaded.


You can use the declare method like Dim TempLoginID as TempVars
or you can use the Add method like: Tempvars.Add “TempLoginID”, me.txtUserName.Value

nav login

For the example below, a user logged in with “jsmith” then the LoginID is showing “jsmith” on the Navigation form as a TempVars!TempLoginID is assigned to a txtLogin textbox.

login textnav show login

2. Set SQL statement to temp variable. The statement from SQL can be viewed by using a Msgbox as shown below:

tempvartemp message

3. Remove TempVars VBA. If we don’t want to use this Temp variable any more then we can remove it.
(a) We can use the “Remove” method to remove a single variable:
TempVars.Remove “TempSearch”

(b) or use ‘.RemoveAll’ to clear all temp variables:

Method #2 Set Temporary Value using Macro

Macro is widely used in the Access data recently and can also be used under the web Access form environment. The VBA environment is not working under the web Access form. Also the TempVar is more frequently used under the Embedded Macro.

1. Set TempVars for Macro in the Embedded Macro

temp macroset temp macro

2. Remove TempVars Macro. We can remove a specific TempVar by using RemoveTempVar follow with a TempVar Name (currentUserID in this example). Or we can remove all Temp variables by using a RemoveAllTempVars shown below.

remove temp macro

Related posts