Defining new TempVars? Use ADD or DIM to establish TempVar?

Question about the better code to use for defining a new TempVar.  Use:

  1. TempVars.Add "NewTempVarsName", (set tempvars value)
  2. Dim NewTempVarsName As TempVar   ...then a few lines down...  TempVars!NewTempVarsName = (set tempvars value)

Is one method preferred over the other?  Advantages to either method?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Option 1 is what you would use if you were defining a constant.  You don't need a TempVar to define a constant.  If you later change the value of this TempVar some future developer is going to be cursing you and your offspring for all eternity.

Option 2 is what you would use if you were defining a variable that can be addressed from various forms and modules and whose value can change.

When I use TempVars, which is rarely, I define them in the first form that opens, which is normally a switchboard.  Then they get set where they are used.  I don't want to have to search every module and every form for TempVar definitions.  I'd been working with Access for more than a decade when TempVars were introduced so I already had developed a method to use a hidden form to store all global variables rather than defining variables as global.  Global variables are a problem when an error is raised.  Untrapped errors cause global variables to loose their contents.  The hidden form has another advantage and that is for testing, I can make the form visible and watch variables change or even change them manually.  I know you can do this in the Code window with watches, but the form is more flexible.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Dev-Soln LLCCommented:
I use them a lot, and prefer to initialize them in my startup or splash form, similar to the way that Pat does with her global variable form.  You can use syntax like:

Tempvars.Add strVarName, varValue


Tempvars!VarName = varValue

Either method works just fine.
BDAuthor Commented:
Question to Pat:

First, thanks for the explanation on setting TempVars, good info.  

Second, using your hidden "global values" form, I assume you have a main form that is displayed when the database opens.  Do you use Form_Load to place your code for opening the hidden form and entering your initial values?  Could you provide an example of your code to do this?  Also, how are you hiding the form?

I am very weak with VBA and trying to learn.  Thanks.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
TempVars are cool because they do not get hosed with a crash or when reset occurs, unlike Globals.
Also ... they can be called directly from Queries :-)

Joe, I agree that TempVars are useful.  My point was that I solved the problem years ago and TempVars don't add anything to my existing solution and I think my existing solution is sufficiently good that it is worth a mention.  TempVars were implemented because of Webforms and Data Macros so if you are using them, TempVars is your only option.

BD,  I usually make my "hidden" form the form that opens when the database opens.  In the Current event, I set any defaults that need setting for the form's controls or TempVars if I've used them in this app.  Then I hide the current form by setting its visible property to False and open the Menu/Switchboard.  There is no logic involved.  It is simply a list of TempVars definitions followed by set statements if they variables need initiation.

Here's a picture of a hidden form from one of my apps.  As you can see, all the controls are unbound.  This particular app has no code in this form except to hide itself and open the switchboard since no TempVars were used and nothing required an initial value.Variables.JPG
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
@Pat ... my post was unrelated to what you posted per se ... I simply commented and added my input on TempVars.
Not sure why ... you needed to address me in your last post ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.