Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form error trapping

Status
Not open for further replies.

neilmcmor

Technical User
Aug 9, 2007
30
GB
I am struggling to get my head around error trapping. I stuck this bit of code in the Forms VBA to get error codes but get nothing returned. The form is called Prisoner_Details. Can anyone help

Public Sub Form_Error(DataErr As Integer, Resonse As Integer)
MsgBox DataErr

End Sub
 
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description


________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Error handling is done at the subroutine/function level. The basic outline is:

Code:
Private Sub MySub()
  On Error GoTo MyErrHandler

  '......Your code here......

  Exit Sub   'You need this to keep it from "falling through"
             'to the error handler if there are no errors

MyErrHandler:
  'Handle the error as you see approriate, use the Err.Number
  'and Err.Description properties to get information
  'about the error
End Sub

The Form_Error traps certain Access-specific errors. You commonly use it to replace Access error messages with your own user-friendly messages. Sometimes the errors here are more of the "warning" kind, and you can use this subroutine to suppress the Access error message.

But it's not an overall error trapper, any mundane error (like trying to open a file on a path that doesn't exist) will only be trapped in the subroutine level error handler such as the one in my example.

 
Here is my code
Code:
On Error Goto ErrHanler

'Code in here, if error happens the ErrHandler will take over.


ExSub: 
Exit Sub
ErrHandler:
msgbox "Error Number: " & err.number & " - " & err.discription, vbInformation, "Opps: ERROR!"
Goto ExSub
End Sub
 
I also like to add the subroutine's name to the message which helps a lot in debugging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top