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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Error GoTo statement 1

Status
Not open for further replies.

nphani

Technical User
Feb 10, 2002
104
US
Hi Friends,

I am actually coding in VBA and could not find a better forum that could answer my question...

I have 3 functions, one calling other and each call being handled by an error handler:

Function A:
_________

Top_A:
On Error Goto ErrHndl_A

B() ' call to function B()

ErrHndl_A:
GoTo Top_A


Function B:
_________

Top_B:
On Error Goto ErrHndl_B

C()

ErrHndl_B:
GoTo Top_B


Function C:
_________
Dim Cntr
Cntr := 0

Top_C:
On Error Goto ErrHndl_C

ErrHndl_C:
If(Cntr <> 100) Then
Cntr := Cntr + 1
GoTo Top_C
End If
MsgBox("Error in C")


The program starts with a call for function A(). I get an error in C() and I was expecting that C() would loop 100 times and finally give the MsgBox text. But, fortunately thou, the flow is a bit different:

1) A() -> B() -> C()
2) Error occurs in C() and ErrHndl_C handles it and control passes to Top_C.
3) Error occurs again in C(). Now strangely ErrHndl_B handles this error and control passes to Top_B
4) This results in another call to C().
5) Error occurs as usual in C() and ErrHndl_C handles it and control passes to Top_C.
6) Error occurs again in C(). Now ErrHndl_A handles this error and control passes to Top_A
7) Control at Top_A otherwise means re-execution of the whole program and no error occurs in C() this time and program runs to completion. The re-execution leading to completion of the program is as expected.

The program is running well by clearing the error, but I don't understand why C()'s error handler is not looping for 100 times and giving the message box text. Instead the above sequence of steps is occuring.

Can somebody explain why this is happening or am I missing something...

Thanks,
Phani
 
In ErrHndl_C: replace this:
GoTo Top_C
By this:
Resume Top_C
in order to leave the error handler.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top