Consulting

Results 1 to 9 of 9

Thread: Problem with "On Error" Not Trapping

  1. #1

    Problem with "On Error" Not Trapping

    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.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You should use On Error Resume Next instead and check if Err <> 0 to see if there was an error at each step.

  3. #3
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    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.

    HTH

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    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

  5. #5
    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.
    Quote Originally Posted by drj
    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.
    Quote Originally Posted by Richie
    For more detail have a look here.
    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.

  6. #6
    Quote Originally Posted by rbrhodes
    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"?

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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:
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Quote Originally Posted by Cyberdude
    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

  9. #9
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •