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

BD
BD used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
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.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

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

or

Tempvars!VarName = varValue

Either method works just fine.
BD

Author

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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

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

TempVars
Distinguished Expert 2017

Commented:
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 / Application Developer
Top Expert 2007

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial