PDA

View Full Version : Solved: Problem with "On Error" Not Trapping



Cyberdude
06-11-2005, 12:01 PM
I just had a problem with error handling. The sequence is essentially like the following:

On Error GoTo BadName
MaxNames = Range(?MaxNamesX?)
On Error GoTo 0
. . .
BadName:
On Error GoTo CantFindName
MaxNames = Range(?MaxNameCntX?) ?<-caused runtime error not trapped
On Error GoTo 0
. . .
CantFindName:

. . .

I intentionally set an incorrect defined name in the first ?On Error? sequence in order to trigger the error handling. It worked by transferring control down to label BadName. Since there is the possibility the second name might be bad, I put in the second ?On Error? to handle that case. Unfortunately the second ?On Error? didn?t trap the second bad name. Instead I get an execution error that essentially tells me it couldn?t find the defined name MaxNameCntX. My question is, why didn?t the second ?On Error? trap the second error? While I didn?t expect it to help, I tried putting an ?On Error GoTo 0? in front of the second ?On Error? condition, but it didn?t help. :banghead:

Jacob Hilderbrand
06-11-2005, 12:17 PM
You should use On Error Resume Next instead and check if Err <> 0 to see if there was an error at each step.

Richie(UK)
06-12-2005, 12:13 AM
Hi CD,

You haven't posted the code for your error-handler but I suspect that you have missed using a Resume statement of some kind.

For more detail have a look here (http://www.vbaexpress.com/forum/showthread.php?t=3526&highlight=error+resume).

HTH

rbrhodes
06-12-2005, 01:06 AM
Hi,

It can be misleading. On Error goto 0 sounds like a reset. Here's your example with a resume statement with a label to resume at. Resume resets the Error and picks up where you tell it to ie: Next (next line after the error) or at a label (in this case 'pickup:')



Sub e()
On Error GoTo BadName
MaxNames = Range(?MaxNamesX?)

BadName:
MsgBox ("First error: " & Err.Number & " " & Err.Description)
'This line...
Resume pickup
pickup:
On Error GoTo CantFindName
MaxNames = Range(?MaxNameCntX?)
CantFindName:

MsgBox ("Second error: " & Err.Number & " " & Err.Description)

End Sub


Cheers,

dr

Cyberdude
06-13-2005, 10:24 AM
First let me apologize for not responding to the guys who offered suggestions. My DSL has been down for 4 days, and my dialup was up and down. Then when I got through with dialup, I had a problem I've had before on this forum: the input editor wouldn't turn on ... the normally brown color (on my screen) will turn green when it's ready for input, but it remained brown, so I couldn't enter anything. Very frustrating.

You should use On Error Resume Next instead and check if Err <> 0 to see if there was an error at each step.
I tried that, and on the second label, BadName, it didn't "resume next" ... I got the same error message (no number) instead. Strange. It seems like no matter what the error was, it should have done a "resume next", but it didn't.

For more detail have a look here (showthread.php?t=3526&highlight=error+resume).
Thanx, Richie. Terrific reference, and probably contains an explanation of the problem I'm having. I am printing it out for further study.
rbrhodes, thanx for the code. I'm printing it out too for further study.
I guess I've never used a double "On Error" before, so I never had to deal with the problem. I also will examine the VBE Help. I thought I understood it, but apparently not. You guys have been a big help. :bow:

Cyberdude
06-13-2005, 01:55 PM
Sub e()
...
On Error GoTo BadName
MaxNames = Range(?MaxNamesX?)
BadName:
MsgBox ("First error: " & Err.Number & " " & Err.Description)
'This line...
Resume pickup
pickup:

I have always had the inclination to want to write a statement to disable my previous "On Error" condition as the first statement following the label I went to. In your example following the BadName label, you show a Msgbox statement. While that is probably an oversight, what statement should you have written at that point to clear the previous "On Error"?

johnske
06-13-2005, 02:33 PM
Hi Cyber,

As stated above, the 'Resume' clears the error. You should also be able use Err.Clear to specifically clear the error in the example you've given above E.G.On Error Goto BadName
MaxNames = Range(?MaxNamesX?)
On Error Goto 0
. . .
BadName:
Err.Clear
On Error Goto CantFindName
MaxNames = Range(?MaxNameCntX?) ?<-caused runtime error Not trapped
On Error Goto 0
. . .
CantFindName:

rbrhodes
06-13-2005, 05:06 PM
In your example following the BadName label, you show a Msgbox statement. While that is probably an oversight, what statement should you have written at that point to clear the previous "On Error"?

The message box is simply to tell you what happened in the first error. It's not necessary if you don't care that there was an error or what the error was!.

The statement Resume pickup clears the error and goes to the label 'pickup:'

Resume Next goes to the line after the line that caused the error. In this case that would have been Badname: and a loop on the Msgbox...

Clear as mud. <g>

Any ways, you need the "Resume (go_somewhere)" statement to clear the 1st error and continue on to the next one!

Cheers,

dr

Cyberdude
06-14-2005, 03:23 PM
Not at all. Thanx to you johnske and rbrhodes for filling in the holes. That's what I was looking for. rbr, I see now what you mean by the Msgbox in your example. It just triggered something in my brain, because I like the FIRST statement to clean up the On Error in effect, 'cause you never know when another statement could blow up. Thanx aagain to both of you. :beerchug: