Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables get deleted after Execution error - remedy?

Status
Not open for further replies.

spitzmuller

Programmer
May 7, 2004
98
CH
Hi folks:

I have a module in which I defined several Public Variables that can be accessed by any other Procedure in the whole project.

Now, whenever my code stops (e.g. due to a programming error) and i get the debug-window (with the Debug- and End-Buttons), the global variables get cleared and I have to relauch.

Is there a way around this?

Or, to put it in ohter words: is there a recommeded way to implement variables that all procedures can access??

Help is appreciated.

Simon
 
Simon,
You can protect the values in your Public variables with an "On Error" clause:

Private sub mySub()
Dim ....

On Error GoTo ErrRoutine

blah, blah, blah (your code)

Exit Sub

ErrRoutine:
msgbox "Error: " & err.Number & " " & err.Description, vbOkOnly

End Sub

This will just get you out of your current sub without destroying the contents of your Public variables.

You can also check the error number in ErrRoutine and do conditional processing based on the type of error, or resume processing at some other place in the sub if you want.

Tranman
 
Have you tried setting a breakpoint at the beginning of the subroutine in question? That way you cvan run the sub, and when execution stops on the breakpoint (soon as you enter the sub) you can use F8 to execute the code line by line. This'll let you get to the line causing problems while constantly monitoring the contents of your public variables, hopefully tracking down the problem.
 
There is an option in the code editor that will enable warnings before state loss. On the "General" tab of the options dialog, There is a checkbox under "Edit and Continue" for "Notify Before State Loss." Once you turn that on you'll be warned just before losing state.

I don't leave it on under normal circumstances because it gets annoying, but it will help you pinpoint where the state loss is occurring.

You can create a sub procedure called "InitGlobalVariables()" that you can call to re-initialize your globals. Then you can type the call in the immediate window when you lose state and you won't have to relaunch.

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Hi guys

Thanks for your answers. Especially liked the "Notify before state loss".

Will try immediately... Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top