Results 1 to 3 of 3

Thread: Question using On Error...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Question Question using On Error...

    I want to know if I can do this because it does not seem to work right.

    I start out with using an On Error Goto ER1 and then after that part I want to do an On Error Resume Next in the same sub. But the resume next part does not seem to take affect. Like this:

    On Error GoTo ER1

    <some code goes here...>

    ER1:

    <more code here>

    On Error Resume Next

    <and more code here...>


    But the On Error Resume Next does not work it seems. I can't change the On Error in the same sub?

    Thanks!

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    You need to turn off error trapping before you can change it.

    Insert a

    VB Code:
    1. On Error Goto 0

    on the line before the On Error Resume Next to reset the on error routine
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by WarrenW
    I want to know if I can do this because it does not seem to work right.

    I start out with using an On Error Goto ER1 and then after that part I want to do an On Error Resume Next in the same sub. But the resume next part does not seem to take affect. Like this:

    On Error GoTo ER1

    <some code goes here...>

    ER1:

    <more code here>

    On Error Resume Next

    <and more code here...>


    But the On Error Resume Next does not work it seems. I can't change the On Error in the same sub?

    Thanks!
    Just remove the On Error words from the statement and simply use "Resume Next"

    For e.g.

    VB Code:
    1. Private Sub DisplayNumbers()
    2. On Error Goto ER1
    3.     'Some code1
    4.     'Some code2
    5.     'Some code3
    6.     'Error Occured
    7.     'Some code4
    8.     'Some code5
    9.  
    10. ER1:
    11.     If Err.Number <> 0 Then
    12.         MsgBox "An error occured"
    13.         Resume Next
    14.     End If
    15. End Sub

    In this case, after the error occurs, the error handler will show a message box showing "An error occured", and then will resume execution from Some Code4.

    A few other things to note: I have found it better to put the error handler at the top of the procedure instead of at the bottom. This works great with add-ins that have a facility of inserting procedure headers. You can simply create a header with the error handler code, and then tell the add-in to insert proc headers in all procs, which gives you error handlers in all procedures!

    The second, and more significant advantage is that if you have an error handler at the bottom, you will need an Exit Function/Exit Sub as the last statement before the error handler code. Alternatively you will have to check if the Err.Number is non-zero. In the absence of any of these steps, the code execution will continue right through your procedure code into the error handler. The result is the error handling code will execute always. Since for normal execution the error number is assumed to be zero, you will end up getting the "An error occured" messagebox all the time. Therefore you should either use Exit Function/Exit Sub before the error handler code, or check the error number inside the error handler code.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

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



Click Here to Expand Forum to Full Width