We help IT Professionals succeed at work.

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

BD
BD asked
on
2,364 Views
Last Modified: 2017-04-03
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

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
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.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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 ?