Error Handling

  • Thread starter Jesse via AccessMonster.com
  • Start date
J

Jesse via AccessMonster.com

Is it possible to trap an error 3022 so instead of the user getting the
cryptic message about the primary key to have it tell them something like the
account already exists or something along those lines. If so where would
that go in the form code exactly.

Any help would be appreciated.
 
D

Douglas J Steele

On Error GoTo ErrHandling

' existing code

EndIt:
Exit Sub ' or Exit Function

ErrHandling:
Select Case Err.Number
Case 3022
MsgBox "The account already exists"
Case Else
MsgBox Err.Description
End Select
Resume EndIt
 
G

Guest

Look in VBA Help for On Error. It will give you a basic tutorial on how to
do error handling. As to trapping an error while you are in an error handler:

If errr.number = 3022 then
msgbox "some message the user will understand"
End If
 
J

Jesse via AccessMonster.com

The only question I have is where to put this in what event on the form. I
tried before update event and it still gives me the access runtime error 3022
instead of the custom one i did.
 
G

Guest

You need to put it in whatever procedure your are getting the error. Each
Sub and Function should have it's own error handler.
 
J

John Welch

Jesse, I agree with George. If the error has to do with the Jet database
engine itself (like a duplicate primary key), rather than with code in a
specific procedure, then you will get a database error rather than an error
in that procedure. The way to handle that is with the Form_Error event, as
George mentions. Here's a little example:

Sub Form_Error(DataErr As Integer, Response As Integer)

Case 3022 ' Duplicate value in index,
MsgBox "Whatever you want to say"
Case Else
Response = acDataErrDisplay
Exit Sub
End Select
Response = acDataErrContinue
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top