Results 1 to 3 of 3

Thread: [RESOLVED] Message box - to report a certain error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Resolved [RESOLVED] Message box - to report a certain error

    Hi
    What i'm trying to do is instead of my program crashing when I get a 'runtime error 3021' - no current record

    Is there a way I can say something like if this error appears in my 'run' sub then bring up a message box saying 'No such record' and then on pressing ok call my 'ClearForm' sub that clears all the form ready for data to be entered again?

    Thanks in advance for any advise

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,502

    Re: Message box - to report a certain error

    You need to learn the concept of error handlers, and not just for this - they should ideally be in almost every sub/function of all of your programs.

    At the moment any errors you get will completely crash the program, whereas almost all situations are either recoverable or will only stop one small section of the program working.

    The general code for an error handler looks like this:
    VB Code:
    1. Sub ....
    2.  
    3.   On Error GoTo ErrorHandler:  'note that "ErrorHandler" is my preference - you can use anything as long as it is the same below
    4.  
    5.  
    6.   '< your code here >
    7.  
    8.  
    9.   Exit Sub  'dont run the error handling code if no error has occured!
    10. ErrorHandler:
    11.   Select Case Err.Number    'I have used Select Case (rather than If) to easily allow for multiple specific errors
    12.   Case 3021   ' = no current record
    13.     MsgBox "No such record"
    14.     Call ClearForm
    15.  
    16.   Case Else   ' unexpected error, just say what it was
    17.     MsgBox "Error " & Err.Number & ": " & Err.Description
    18.   End Select
    19.  
    20.   'code here to clean up if required (eg: erase arrays/close recordset objects)
    21.  
    22. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Message box - to report a certain error

    Thanks

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