TempVars

mjdemaris

Working on it...
Local time
Yesterday, 16:43
Joined
Jul 9, 2015
Messages
426
Quick question:

I saw someone post that most developers do not use TempVars. Is this true?

What is the value of tv over globals, enums or constants?

thanks
 
its on your own discretion if you want to use them.
the advantage, you don't loose the value of tempvars when error occurs, unlike public variables.
you can also use them in query.
 
you don't loose the value of tempvars when error occurs, unlike public variables.

That is a very common myth. VBA variables are not lost when an error occurs. They are lost only when the user chooses to reset the project. There are other ways to deal with a break instead of the reset.

BTW I have always found the name amusing. They are the least temporary of all variables.
 
how do you say it's a myth. can you quote which book, journal, etc.
 
The late ChrisO would always don't take things you read for granted. Test them and prove them yourself.

The following code will error with runtime error 9 (I have assigned myGbl as public elsewhere):

Code:
    Dim a(1) As Integer
    myGbl = 30
    MsgBox "value of global before error = " & myGbl
    a(1) = 1
    MsgBox "value of global after error =  " & myGbl

So when the debugger opens, debug and change a(3) to a(1). The code will continue and the msgbox will show that the value of myGbl is still 30 i.e. it has been retained despite the error.

hth
 

Users who are viewing this thread

Back
Top Bottom