TempVars vs Type Variables (1 Viewer)

NauticalGent

CopyPaster of the First Order
Local time
Today, 01:44
Joined
Apr 27, 2015
Messages
3,413
Hello folks,

Seeking some advice/opinions. I have it in my head that TempVars are bad practice - not sure where it came from but the consensus here at AWF seems to be to avoid them.

My current job has be going over and documenting the existing App/Code. While doing so, I came accross this:

Code:
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
TempVars("Identifier") = rs!Identifier.Value
TempVars("BuyerCode") = rs!BuyerCode.Value
TempVars("OfficeSymbol") = rs!OfficeSymbol.Value
TempVars("Name") = rs!Name.Value

In the past I would achieve the same results with this:

Code:
Public Type UserInfo
  UName As String
  EMail As String
  JobTitle As String
  LogDate As Date
End Type
 
Public uInfo As UserInfo       'Declare a GLOBAL Instance of the User Defined type

And then enumerate them with:

Code:
Dim MyDB As DAO.Database
Dim rst As DAO.Recordset
 
Set MyDB = CurrentDb
Set rst = MyDB.OpenRecordset("SELECT * FROM Employees WHERE [EmployeeID]=1", dbOpenSnapshot)    '1 Record
 
'Fill the Enumeration/User Defined Type
With rst
  uInfo.UName = ![LastName]
  uInfo.EMail = ![email]
  uInfo.JobTitle = ![Title]
  uInfo.LogDate = ![HireDate]
End With
 
rst.Close
Set rst = Nothing

My questions are: Do TempVars get a bad rep or should they be avoided? Is one method better than the other?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 06:44
Joined
Sep 12, 2006
Messages
14,413
I've never got into the habit of using them, but I think it's a matter of taste. One benefit is that they retain values in the event of a program crash. Also they come with intellisense, I think.
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:44
Joined
Mar 14, 2017
Messages
4,314
I think they are too easy to use and discourage people from understanding what they are actually doing.
I still use global variables - they work fine.

Honestly, though - my personal preferences aside - they're not "bad" (like MVF's or something). I just think they obfuscate things and make it foggier to see through, which I always stay away from stuff like that myself.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 06:44
Joined
Sep 21, 2011
Messages
7,898
I have always thought of them as just another form of global variable?
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 01:44
Joined
Apr 27, 2015
Messages
3,413
I've never got into the habit of using them, but I think it's a matter of taste. One benefit is that they retain values in the event of a program crash. Also they come with intellisense, I think.
Thanks Dave, I read that bit about retention too. If everything else comes down to a matter of taste then TempVars would seem to be a better option. I have read somewhere that they can be used in queries too so there is that...
I think they are too easy to use and discourage people from understanding what they are actually doing.
I still use global variables - they work fine.

Honestly, though - my personal preferences aside - they're not "bad" (like MVF's or something). I just think they obfuscate things and make it foggier to see through, which I always stay away from stuff like that myself.
Isaac! It seems it IS possible for you and I to communicate outside of the WaterCooler....who woulda thunk it?!?! Thanks for the input.
I have always thought of them as just another form of global variable?
Paul, agreed. One of the frustrating things (to me at least) about VBA is that there seems to be about 10 different ways to do the same thing. To CopyPasters like me, it makes me wonder if one way is better than the other and why is there so many options??? Maybe I am over-thinking too much....

MarkK once provided a nifty Class Module that accomplished the same thing but it seemed like a bit of overkill. I guess the big question is that does one method carry a bigger "hit" on resources over the other...is there even a way to determine this?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:44
Joined
May 21, 2018
Messages
4,763
The big differences from Tempvars and all others you mentioned is that they do not go out of scope on an unhandled error and more importantly they are a collection. You can loop, count, add, remove, and call by index. That does provide a lot more flexibility over types and globals.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:44
Joined
Oct 29, 2018
Messages
14,544
As others already mentioned, TempVars is like a Global Variable with the main difference that you won't need a helper/wrapper function to access them outside of VBA and that they don't lose their content during a session reset. Also, I think custom Types are only accessible through VBA.

I use TempVars a lot more now than Global Variables, because I like using them in query parameters.

Just my 2 cents...
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 01:44
Joined
Apr 27, 2015
Messages
3,413
Good stuff guys, appreciate your time...
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:44
Joined
May 21, 2018
Messages
4,763
Actually I totally missed that benefit. But I think what @theDBguy mentions for use in query is probably the real reason they exist.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 06:44
Joined
Sep 12, 2006
Messages
14,413
Yes I forgot about the benefit with tempvars, of not needing a function to read a variable in a query. That's what comes from not using them!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:44
Joined
Feb 19, 2002
Messages
31,476
One of the frustrating things (to me at least) about VBA is that there seems to be about 10 different ways to do the same thing.
There's a book written by an old friend named Gerald Weinberg called "The Psychology of Computer Programming". It is more of a psychology book than a technical one and it talks about how programmers work. One of his points is that some computer languages are better than others and the worst ones are like VBA which give you way too many ways of doing the same thing.

I don't like tempvars for two reasons -
1. you have to remember to use their .value property in some cases
2. people define them all over the place. Use a little discipline and make a module specifically for defining tempvars and make it easier on the people who have to look at your code later.
 

Users who are viewing this thread

Top Bottom