Discussion Locked

Trapping error messages in Access 97

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have a form that is based on a table and I would like to trap the error that is raised when a user inputs a record with a primary key value that already exists in the table. What I would like to do is then give my own error message rather than the Access error message. Any help would be greatly appreciated.

5 replies

by donq

There are many ways to do what you want. This code is pasted to a Primary Key field's After Update event to automate some of my application functions, and it traps the normal Access error when a duplicate key value insertion is attempted. I have modified the line you need to change for clarity. Good Luck!

Private Sub omfPK_AfterUpdate()
On Error GoTo Err_omfPK_AfterUpdate

'AUTOMATICALLY SET THE omtLuid FIELD TO THE CURRENT USER AND
'SET THE AUTOMATED UPDATE VALUE TO THE CURRENT CLOCKTIME
'EVERY TIME THIS FIELD VALUE IS CHANGED.
omfLuid = CurrentUser()
omfUpd = Now()

Exit_omfPK_AfterUpdate:
Exit Sub

Err_omfPK_AfterUpdate:
MsgBox "Enter whatever text you want displayed in quotes here"
Resume Exit_omfPK_AfterUpdate

End Sub

by cooger

Thanks for your help, but I think the method described in the next answer is much easier and much cleaner. Thanks

by whyong

try to put this code in your form's on error event...

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conDuplicateKey = 3022
Dim strMsg As String

If DataErr = conDuplicateKey Then
Response = acDataErrContinue
strMsg = "Each employee record must have a unique " _
& "employee ID number. Please recheck your data."
MsgBox strMsg
End If
End Sub

happy coding

by cooger

Thanks for your help. I tried it out and it worked perfectly.....it's also quite simple and clean too!!

This discussion is closed to new replies.