> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# A Safer Way to Use TempVars
- URL: https://nolongerset.com/a-safer-way-to-use-tempvars/
- Published: 2022-09-21T02:59:23.000Z
- Updated: 2026-05-08T12:54:01.000Z
- Description: The TempVars object is an intriguing alternative to traditional global variables, but it has some shortcomings. Here's one way to work around them.
- Author: Mike Wolfe
- Tags: TempVars, VBA, #Import 2026-05-20 02:59

Microsoft Access has a [TempVars](https://learn.microsoft.com/en-us/office/vba/api/access.tempvars) collection that holds up to 255 [TempVar](https://learn.microsoft.com/en-us/office/vba/api/access.tempvar) objects.

### The Problems with TempVars

Unlike traditional global constants, TempVar values survive a code reset. There are a few problems with TempVars, though. 

- TempVars are not "visible" (as opposed to, say, a [Global form](https://nolongerset.com/the-global-form/))
- TempVars provide no type safety (they are of type `Variant`)
- TempVars do not appear in IntelliSense
- TempVars are prone to typos (no benefit from `[Option Explicit](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/option-explicit-statement)`)
- TempVars cannot be directly called from queries

### The Solution to (Most of) These Problems

One way to mitigate most of these shortcomings without sacrificing the TempVar benefits is to wrap them inside read/write properties of a VBA class module. Here's the basic idea:

1. Create a class named TV
2. Set its [PredeclaredID attribute](https://nolongerset.com/creating-class-instances/#how-to-setting-the-predeclaredid-attribute) \= True
3. Add a property Getter/Letter for each TempVar
4. Set TempVar values in code like so: `TV.MyVar = 42`
5. Retrieve TempVar values in code like so: `?TV.MyVar`

### Benefits of the TV Class Module Approach

This approach adds...

- Visibility
- Type safety
- IntelliSense
- Compile-time checks for typos

...to the TempVars object. 

### Further Reading

For more information, read these articles that cover related TempVars concepts:

- A [sample TV class module](https://nolongerset.com/tv-class/) to demonstrate this concept in action.
- A [function](https://nolongerset.com/safely-using-tempvars-in-queries/) you can use to incorporate TempVar values into queries.
- [Code to populate a list box](https://nolongerset.com/populatelistboxwithtempvars/) with the contents of the TempVars collection.
- [Code to populate a local table](https://nolongerset.com/tempvars-table/) with the contents of the TempVars collection.
- A design-time routine to [generate a TV class module](https://nolongerset.com/generatetvclass/) from a local table.

---

### Referenced articles

[The Global FormStoring global variables on a hidden form has some distinct advantages over storing them in VBA.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/01/rustic-3441673_1920.jpg)](https://nolongerset.com/the-global-form/)

*Image by [Jason Dexter](https://pixabay.com/users/jdexter-260033/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1142721) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1142721)*

***UPDATE*** *\[2022-10-22\]: Change the heading and text in the final paragraph to reflect that all the articles are now complete.*