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

# GenerateTVClass(): Auto-Create a TempVars Class Module
- URL: https://nolongerset.com/generatetvclass/
- Published: 2022-10-22T03:59:00.000Z
- Updated: 2026-05-08T12:54:03.000Z
- Description: The culmination of my TempVars series, this article shows you how to generate a TempVars class module from a local table.
- Author: Mike Wolfe
- Tags: TempVars, Code Library, #CodeGeneration, #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/)*.

---

A [dedicated TempVars class module](https://nolongerset.com/tv-class/) (with PredefinedId = True) has many benefits:

- Visibility
- Type safety
- IntelliSense
- Compile-time checks for typos
- No need to create an instance of the class

Writing such a class by hand isn't *difficult*, but it is *tedious*. Luckily, there are [easier](https://nolongerset.com/upsertstandardmodule/) [ways](https://nolongerset.com/upsertclassmodule/) to generate boilerplate code in Microsoft Access.

## Starting Point

Before you get started, you will need a table with at least three fields:

- `VarName`: name of the TempVar
- `VarTypeName`: name of the TempVar's type (e.g., *String*, *Integer*, etc.)
- `VarValue`: a default value for an unset TempVar; this field is OPTIONAL

To create a blank table, you can run the `CreateTempVarsTable` routine that I [wrote about here](https://nolongerset.com/tempvars-table/#the-code). Once you have the table, populate it with the TempVars you would like your class module to handle. For example:

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

## The Approach

The subroutine follows a four-step approach:

1. Build the header
2. Build the Let/Get properties from the source table
3. If needed, build a private function to support TempVars with default values
4. [Generate a class module](https://nolongerset.com/upsertclassmodule/) named TV from the string we just built

**BEWARE*: In step 4, any existing class or standard module named "TV" will be overwritten *without warning*... Consider this your warning.*

## The `GenerateTVClass` Subroutine

This procedure has the following dependencies:

- [**UpsertClassModule**](https://nolongerset.com/upsertclassmodule/)

```vba
' ----------------------------------------------------------------
' Procedure : GenerateTVClass
' Date      : 10/21/2022
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/generatetvclass/
' Purpose   : Creates a TempVars class module named "TV" based on
'               the contents of a local table.
' ----------------------------------------------------------------
Sub GenerateTVClass(Optional TblName As String = "tblTempVar")
    Dim s As String
    
    'Build the header
    s = s & "' ---===== DO NOT EDIT DIRECTLY =====---" & vbNewLine
    s = s & "' This class module is auto-generated; to recreate run:" & vbNewLine
    s = s & "'     GenerateTVClass " & TblName & vbNewLine & vbNewLine
    s = s & "' For more information see:" & vbNewLine
    s = s & "' - https://nolongerset.com/generatetvclass/" & vbNewLine
    s = s & "' - https://nolongerset.com/tv-class/" & vbNewLine
    s = s & "Option Compare Database" & vbNewLine
    s = s & "Option Explicit" & vbNewLine & vbNewLine
    
    'Build the properties
    With CurrentDb.OpenRecordset(" SELECT * FROM " & TblName & _
                                 " ORDER BY VarName", dbOpenForwardOnly)
        Dim NeedsGetTV As Boolean
        Do Until .EOF
            s = s & vbNewLine
            s = s & "Public Property Let " & !VarName & _
                    "(Value As " & !VarTypeName & "): TempVars(""" & _
                    !VarName & """) = Value: End Property" & vbNewLine
            If IsNull(!VarValue) Then
                s = s & "Public Property Get " & !VarName & _
                        "() As " & !VarTypeName & ": " & !VarName & _
                        " = TempVars(""" & !VarName & """): End Property" & vbNewLine
            Else
                NeedsGetTV = True
                s = s & "Public Property Get " & !VarName & _
                        "() As " & !VarTypeName & ": " & !VarName & _
                        " = GetTV(""" & !VarName & """, """ & !VarValue & _
                        """): End Property" & vbNewLine
            End If
            .MoveNext
        Loop
    End With
    
    'Build the GetTV() function if needed
    If NeedsGetTV Then
        s = s & vbNewLine & vbNewLine & vbNewLine
        s = s & "'By using Variants for the default value and return type, we give up some" & vbNewLine
        s = s & "'   type safety for the convenience of having a single function" & vbNewLine
        s = s & "Private Function GetTV(VarName As String, DefaultValue As Variant) As Variant" & vbNewLine
        s = s & "    Dim Val As Variant" & vbNewLine
        s = s & "    Val = TempVars(VarName)" & vbNewLine
        s = s & "    " & vbNewLine
        s = s & "    If IsNull(Val) Then" & vbNewLine
        s = s & "        GetTV = DefaultValue" & vbNewLine
        s = s & "    Else" & vbNewLine
        s = s & "        GetTV = Val" & vbNewLine
        s = s & "    End If" & vbNewLine
        s = s & "End Function" & vbNewLine
    End If
    
    'Create (or recreate) the class module
    UpsertClassModule "TV", s, True
End Sub
```

## Sample Usage

Once the local table is populated, just run the GenerateTVClass routine from the Immediate Window. 

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

If all goes well, this will create a class module named **TV** that looks something like this:

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

### Referenced articles

[A Safer Way to Use TempVarsThe TempVars object is an intriguing alternative to traditional global variables, but it has some shortcomings. Here’s one way to work around them.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/09/footprints-1142721_1920.jpg)](https://nolongerset.com/a-safer-way-to-use-tempvars/)

[TV: The TempVars Class ModuleA TempVars class module that provides visibility, type safety, IntelliSense, and compile-time typo checks.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/09/TV-Class.jpg)](https://nolongerset.com/tv-class/)

[Create a Class Module from a String in Microsoft AccessGenerate VBA class modules from strings during design time using this simple function in Microsoft Access.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/10/portrait-5544201_1920.jpg)](https://nolongerset.com/upsertclassmodule/)

[VBA Code Generation TrickUsing Notepad++ to transform existing code into a VBA string that generates itself.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2020/12/Generating-VBA-Strings-with-Notepad---Macros.jpg)](https://nolongerset.com/vba-code-generation-trick/)

*Image by [Sugiarto](https://pixabay.com/users/otrags-1458972/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=964898) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=964898)*