Maximize the use of TempVars in Access 2007 and 2010
The TempVars collection has the ability to save developers 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. Their 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
Add Method: Use it to add variables to the collection, all variables are of the variant type.
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:
TempVars!strAppName
TempVars.Item(“strAppName”)
TempVars.Item(0)
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.
Queries
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.
Conclusion
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.




6 Comments
[...] you’re using custom functions, TempVars or other Access specific techniques. Let’s face it, if you’re reading this blog you may [...]
[...] need multi-session If your looking for a reliable global variable than look no further: use TempVars in your code. But they don’t last between sessions so I came up with a hearty replacement I [...]
I haven’t used TempVars yet but after your post I think they have use in queries. Ugly name though: it suggest volatile storage, while they are global. The “stable environment” argument does not go very far for me: globals only reset while debugging, not in Production.
Thanks Man, you are great. I was searching for this from last week.
Hi Jaun, love your tempvars technique ! – hit a little glitch when executing a query (with tempvar criteria) thru the application object.
CurrentDb.Execute psQryNme – the query returns “3061-Too few parameters. Expected 2″.( I’m setting two tempvars specified in criteria ) — Executes when run manually.. Also tried setting tempvars as parameters.. Any idears ? Thx, Byron
Hi Byron,
Glad to hear you love the technique! Make sure the tempvars are populated before opening the query.
Kind Regards,
Juan