Avatar of BD
Flag for United States of America asked on

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?
Microsoft AccessVBA

Avatar of undefined
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dale Fye

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.

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.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

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 :-)

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

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 - Former Microsoft Access MVP)

@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 ?