Using TempVars Instead Of Module-Based "Public Globals"
TempVars were introduced in Access 2007 to provide a more stable environment for sharing variables globally. To read more about these variables, see this link:
Use TempVars EverywhereÂ
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!
Sample Code from Juan Soto's blog is as follows:
Program Code
' ****************************************************************************************** ' 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: ' ****************************************************************************************** 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. ' ' Juan Soto ' ******************************************************************************************