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!
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:
All three methods above would return “Test Application”.
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.