dcsimg
Results 1 to 3 of 3

Thread: Error handling

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Question Error handling

    Hi,
    I'm using an error handler in my code
    Code:
     
    Private Sub Sampcode()
    On error goto procerror
    other code
    other code
    other code
    procerror:
    MsgBox ("error message sample")
    Now when I enter correct values, the error handler runs again! I want it to check again if my values are correct.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,795

    Re: Error handling

    this is typically how it goes:

    Code:
    Private Sub SoSomething()
    On Error GoTo errHandler
    
    'Do some stuff that may cause an error
    
    Exit Sub 'Exit the sub before falling into the error handler
    
    errHandler:
    'Handle the error here
    End Sub
    Alternatively....
    Code:
    Private Sub SoSomething()
    On Error GoTo errHandler
    
    'Do some stuff that may cause an error
    
    
    DoCleanup:
    ' Do stuff to clean up before exiting
    Exit Sub 'Exit the sub before falling into the error handler
    
    errHandler:
    'Handle the error here
    Resume DoCleanup 'resume at the cleanup before exiting
    End Sub
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Error handling

    also very important... turn off the error handling with On Error GoTo 0
    Code:
    Private Sub SoSomething()
    
        On Error GoTo errHandler
    
        'Do some stuff that may cause an error
    
        On Error GoTo 0
    
        'Do some stuff that you don't want to test for an error
    
        Exit Sub 'Exit the sub before falling into the error handler
    
    errHandler:
    'Handle the error here
    
    End Sub
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width