> ## 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.

# Safely Using TempVars in Queries
- URL: https://nolongerset.com/safely-using-tempvars-in-queries/
- Published: 2022-09-24T02:15:07.000Z
- Updated: 2026-05-08T12:54:34.000Z
- Description: Calling TempVars directly from a query can cause issues. Instead, use this simple wrapper function to save yourself some potential headaches.
- Author: Mike Wolfe
- Tags: TempVars, #Import 2026-05-20 02:59

*This article is part of the series, [A Safer Way to Use TempVars](https://nolongerset.com/a-safer-way-to-use-tempvars/)*.

---

TempVars are a handy alternative to global variables in Microsoft Access, but they have some quirks.

Among those quirks is that if you directly display a numeric TempVar as a field in a query, it actually treats the TempVar value as a character code and shows the corresponding character rather than the numeric value:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/09/image-59.png)

I wrote about the issue here: [Beware TempVars in Queries](https://nolongerset.com/beware-tempvars-in-queries/).

Luckily, the solution is very simple: **create a wrapper function** and call that instead of calling TempVars directly.

## The TempVars Wrapper Function

```vba
' ----------------------------------------------------------------
' Procedure : GetTempVar
' Date      : 9/21/2022
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/gettempvar/
' Purpose   : A public function to return the value of a TempVar object
'               that can be called from a query or form/report control
' Notes     - Returns Null if the TempVar does not exist (rather than raise an error).
'           - This function provides no type safety or compile-time checking.
'           - Use the TV class module to get these benefits when
'               interacting with TempVars from VBA; for more info,
'               see: https://nolongerset.com/tv-class/
' ----------------------------------------------------------------
Public Function GetTempVar(VarName As String) As Variant
    GetTempVar = TempVars(VarName).Value
End Function
```

To use this function, you pass the name of the TempVar as a string.

Note that the function will not throw an error if the TempVar does not exist; it will merely return `Null`.

## Usage

Here's how the function would be used in a query:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/09/image-60.png)

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/09/image-61.png)

It's worth noting that this function returns a Variant, rather than a more explicit value type. You can see this distinction in the screenshot above, as Variant values get left-aligned in Access query results while numeric values (Long, Integer, etc.) are right-aligned. 

Returning a Variant is appropriate insofar as all TempVar values are Variants, but it does mean that the function is less type-safe than my [TempVars TV class module approach](https://nolongerset.com/tv-class/). For that reason, and [several others](https://nolongerset.com/a-safer-way-to-use-tempvars/#benefits-of-the-tv-class-module-approach), I recommend you use the `TV` class module wherever possible.

I recommend you **only use this function in places where the class module is not accessible**:

- Queries
- Form/report controls
- Pretty much anything being [evaluated by the Jet/ACE expression service](https://nolongerset.com/expressions-vs-code/)

*Image by [MetsikGarden](https://pixabay.com/users/metsikgarden-4421146/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3207338) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3207338)*