Power Tip: Maximize the use of TempVars in Access 2007 and 2010

Today’s tip is provided by Juan Soto, Senior Access Developer at AccessExperts.net. His blog is at AccessExperts.net/blog and you can subscribe to it by clicking here. Juan is a frequent speaker at Access user groups nationwide and was a participant in the last Access developers kitchen for Access 2010. He can be reached through his website or his blog.

The TempVars collection has the ability to save you time, improve the reliability of your programming and provide you with new ways to share information across your application.

TempVars were introduced starting in Access 2007 and represented a major addition to the arsenal of tools for professional developers. With it, developers can now store their global variables in a stable environment that will retain values regardless of program crashes.

Before TempVars, developers had limited choices when using application wide variables: module level variables; hidden forms or tables. There are drawbacks to each approach, with module variables being the worst since they initialize if your program encounters an error, losing their assigned values in the process.

Use TempVars Everywhere
TempVars have no such limitation. They retain their values when your code encounters an error and can be used in Forms, Queries, Reports and code. You initialize them when your application starts and they disappear when your application terminates. In short, their a great way to share information across your program!

TempVars Collection
The Add method is how you add new variables to the collection.

TempVars.Add Name, Value

Where Name is the name of your variable and Value is of course the value you wish to assign it. Some examples:

TempVars.Add "strAppName", "Test Application"
TempVars.Add "lngClientID", 123456789
TempVars.Add "bolLoginAgain", True

Once assigned you can now use the value in your queries and code using the following methods:


All three methods above would return “Test Application”.

Remove Method
Use this method to remove the variable or use RemoveAll to get rid of all of them.

One particular advantage of using TempVars is their availability in queries, which you can’t do with global variables without using a function. I created an entire multi-franchise database system using TempVars to differentiate between franchisee data. It made my life easier and improved the reliability of my application.

I encourage you to start using TempVars in your code and your queries in place of module variables or classes, providing your users with a better experience and making your code more powerful.

Juan Soto

Send your Power Tips to accpower@microsoft.com.