Error Messages

G

Guest

I have a data entry Form and after the user inputs the data I have a Save
button that the user will push to save and close the form. If the user
inputs duplicate data in the Primary Key Field Microsoft Access Will produce
it's own error message. I know the Error message number it is Run-time error
'3022', I was wondering if there is some kind of code I can put in so that if
this specific run-time error occurs I can put in my own Message Box. THis is
what I tried but it doesn't work....

Private Sub cmdSave_Click()

On Error GoTo Err_cmdSave_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close acForm, "frm_DrawingControls_Add", acSaveNo

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:

If DataErr = 3022 Then
MsgBox("This record has a duplicate Id.", , "Message")
Resume Exit_cmdSave_Click
Else
MsgBox Err.Description
Resume Exit_cmdSave_Click
End If

End Sub

This obviously is wrong but is there code somewhat like this that will work?
 
A

Allen Browne

Try using the Error event of the form. It provides an argument called
DataError

This kind of thing:

Function FormError(frm As Form, DataErr As Integer, Response As Integer) As
Integer
'Purpose: Generic Form_Error handler.
'Return: Response.
'Usage: In a form's Error event procedure:
' Call FormError(Me, DataErr, Response)
Dim sMsg As String
Const conDupeIndex As Integer = 3022
Const conFileMissing As Integer = 3024
Const conRelatedRecordRequired As Integer = 3201

Select Case DataErr
Case conDupeIndex
sMsg = "The record cannot be saved, as it would create a duplicate."
Response = acDataErrContinue

Case conRelatedRecordRequired
Response = acDataErrDisplay

Case conFileMissing
sMsg = "Data file is currently unavailable."
Response = acDataErrDisplay

Case Else
Response = acDataErrDisplay
End Select

If Len(sMsg) > 0 Then
MsgBox sMsg, vbExclamation, "Problem"
End If
FormError = Response
End Function
 

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

Similar Threads


Top