Re: Global Variables - Untrapped Errors
- From: "Allen Browne" <AllenBrowne@xxxxxxxxxxxxxx>
- Date: Thu, 14 Feb 2008 22:25:36 +0900
The answer could depend on what you mean by an untrapped error.
From the code window, choose Tools | Options | General.Set Error Trapping to "Break On Unhandled Errors".
Now if an error occurs in code that has an error hander, control drops to the error handler where you can take appropriate action, and the values in your variables are not lost. Conversely, if the error happens in a procedure that has no error handling (it was not called from a higher level proc that did have error handling), you get a dialog asking if you want to End or Debug. Choosing End resets the variables, but choosing Debug lets you work with the variables until you Reset (Run menu) or do something that triggers a reset.
The example you give of a class object (created with the New keyword) will be lost when the project is reset. Microsoft does not publish the internal layout of how Access works, so you would be outside the bounds of your EULA if you tried to find the memory and reinstantiate it. Worse, this is the kind of stuff that is not necessary consistent across different versions, or even different service packs. Worse still, there's no guarantee that the contents of memory are still valid after it has been freed up (and the details would depend on other factors such as o/s version.) So, in short: no that's not practical.
In the very early days of Access (version 1 and 2), when computers were much slower and had 4MB of RAM, I did flirt with reusing objects globally but found it less than desirable - partly for the reason you identified. It's easy enough to add another public boolean to indicate whether the initialization code has been run, but it's still messy. You test this every time you need to use a global variable, but there's still no guarantee that your object is still valid, so you add error handlers as well that reinitialize it when needed. Worse, globals always run the risk of interfering with each other. In the end, it's not worth it. IMHO, you're better off creating the objects when you need them, keeping them independent of each other, passing them between routines where appropriate, and not having to worry about other developers on the team messing them up.
These days, there are very, very few public variables in databases that I develop. Even something with tens of thousands of lines of VBA code would have perhaps 2 or 3 public variables reserved for very specific circumstances, and the lifetime I rely on them for would typically be a millisecond or a few seconds at most.
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Vincent" <animedreamer@xxxxxxxxxxx> wrote in message
news:e8d43df2-6bae-4d79-a538-5955007715a7@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
What exactly happens when Access encounters an untrapped error? It
seems like it is resetting all global variables when this occurs. Is
this indeed what is happening? If you have a global variable assigned
to an object, is it set to nothing when an untrapped error occurs?
This seems to be the case.... If so, is there anyway to recover the
lost reference? Is this theoretically possible? For instance,
suppose I instantiate an object: set myObject = new TheObject and
then save the pointer to this object on a form: forms!vars!
theReference = VarPtr(myObject). Is it possible to recover the
reference by invoking CopyMemory like such: CopyMemory myObject,
byval clng(forms!vars!theReference), 4 Now, obviously, I do not want
to be doing such crazy things, but I am curious if this would
theroetically work. And, why does Access reset all globals on an
untrapped error!!
Vincent
.
- References:
- Global Variables - Untrapped Errors
- From: Vincent
- Global Variables - Untrapped Errors
- Prev by Date: Re: Fast DELETE, slow DELETE
- Next by Date: Re: Fast DELETE, slow DELETE
- Previous by thread: Global Variables - Untrapped Errors
- Next by thread: Re: Global Variables - Untrapped Errors
- Index(es):
Relevant Pages
|