Results 1 to 5 of 5

Thread: On Error Resume Next?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Okay, I know this is probably a dumb question but can someone explain what 'On Error Resume Next' means?

    Does that just mean that the code will keep running even though there was an error or does it mean something else?

    Say I have the following code:

    Code:
    If Err Then
       '-- Exit function
       MsgBox "There was an error!"
       Exit Funtion
    End If
    If I had declared "On Error Resume Next", would that mean that it would just go to the next line (End If) and stop or would it continue with the remaining code in that function?

    Thanks,

    Dan


  2. #2
    Guest
    On Error Resume Next Makes it so, if your Sub has an error, it ignores it.

  3. #3
    Guest
    When you use On Error Resume Next, it'll just keep going as though nothing had happened, but you can tell when an error had occurred with Err.Number


    Code:
    On Error Resume Next
    Open "C:\autoexec.bat" For Input As #1
    If Err.Number = 53 Then
        MsgBox "File Not Found!"
        Err.Clear
    End If

    Hope it helps
    Sunny

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    I think that it is better to use it like that:

    Err.Clear
    On Error Resume Next
    Open "C:\autoexec34.bat" For Input As #1
    If Err.Number <> 0 Then
    MsgBox Err.Description
    Err.Clear
    End If

    this way, you will know of any error that happen and not just if a specific one accured.
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Yes just as the above posts has stated the On Error Resume Next statement just goes on to the next line if an error is raised. But this could make some unexpected results.
    Consider this code:
    Code:
    Public Sub AnExample()
        Dim sngDivider As Single
        On Error Resume Next
        sngDivider = Val(Text1.Text)
        If 10 / sngDivider = 2 Then
            MsgBox "The Result Is 2"
        Else
            MsgBox "The Result Is Not 2"
        End If
    End Sub
    This would of course popup a message that stated that the result is two if the user has typed in 5 in the Text1 textbox, right.
    Well consider that the user has typed in 0 (zero) or a non numeric text. Then the if statement would raise an error, you can't divide by zero.
    But since we got an On Error Resume Next statement it would just go on with the next line and popup the MsgBox that stated that the result is 2.

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