Check if global variable has been erased

Kronix

Registered User.
Local time
Today, 20:39
Joined
Nov 2, 2017
Messages
102
I have a global variable of type Collection defined in a module. I know if I get an uncaught error the global variable will no longer be defined. But I am trying to check if it is undefined and if it is then call a function that sets it new and assigns its values. But I can't find a way to check it. I tried the checks "isNull(globvar)" and "globvar=nothing" but they are never true and the program continues running and gives me the 424 Object Required error.
 
I think that it has been destriyed so there is no way checking ir resetting it.
Use Tempvars instead. They are global and dont get destroyed on error.
 
I know if I get an uncaught error the global variable will no longer be defined.

That is a common myth. The variables are only cleared when you Reset from the error.

I tried the checks "isNull(globvar)" and "globvar=nothing" but they are never true and the program continues running and gives me the 424 Object Required error.

Almost there

Code:
If globvar Is Nothing Then
 
Wow it worked. Thanks! So "Is Nothing" can be used with any pie in the sky variable name that hasn't been defined anywhere?

I wonder if a Tempvars variable can be a Collection? Right now I have a tight schedule and don't want to bother testing it.
 
Wow it worked. Thanks! So "Is Nothing" can be used with any pie in the sky variable name that hasn't been defined anywhere?

Is Nothing only works with Objects. It will throw an error if the variable is a simple type. Consequently it is essential to Dim the variable as some kind of object before applying Is Nothing.

I wonder if a Tempvars variable can be a Collection? Right now I have a tight schedule and don't want to bother testing it.
TempVars can only hold simple types so they aren't any use for objects such as a Collection. Given that limitation, the advantage of not being cleared at a Reset doesn't amount to much. (As I said before it is a pervasive myth that VBA variables are lost on unhandled errors.)

Some developers also like them because they can be referred to in queries. Even then their utility is limited because they are not available from CurrentDb so no use for opening recordsets. A wrapper function does a better job of passing VBA variables to a query since Public functions do work from CurrentDb. I prefer to provide a parameter or concatenate the value into dynamic query.

Developers who like TempVars seem to overuse them in the same way that some excessively use Global variables for passing values between procedures that would be better handled by passing a parameter with the call. Unfortunately a TempVars enthusiast at Microsoft advised developers to maximise their use and some accept their word as gospel.

I always thought TempVars an odd name too as they are the least temporary of any variable in VBA.
 

Users who are viewing this thread

Back
Top Bottom