Full Version: Customizing Error Messages
UtterAccess Forums > Microsoft® Access > Access Forms
manchako
Hi,
I'm trying to customize an error message that my program throws. When you change a field in a form (primary key) to a value that already exists I get the error:
"The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."
Is there a way to change this error message on this particular form?
Thanks!
theDBguy
Hi,
Yes, I think so. You should be able to use the Form's OnError event to handle the error and throw your own error message.
Just my 2 cents...
manchako
Thanks it works to throw up the message but it still displays the old message after the new one (two dialogue boxes come up). Is there something I can punch in VBA to stop the second box from coming up after my message?
theDBguy
Hi,
You might be able to use the form's BeforeUpdate as well in conjunction with the OnError event. If I have time for some testing later on, I will try to give you more specific suggestions.
Good luck!
HairyBob
Hi,
etting the Response parameter of the Error event to acDataErrContinue should work - for example:
CODE
Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 3022 Then
        If MsgBox("Duplicate entry attempted." & vbCrLf & vbCrLf & "Do you wish to continue editing ? - If you click 'No' " & _
                                                "your changes will be undone.", vbExclamation + vbYesNo + vbDefaultButton1, _
                                                                                                "Duplicate Error") = vbNo Then
            Me.Undo
        End If
        
        Response = acDataErrContinue
    End If
End Sub
manchako
Perfect! Thanks!
theDBguy
Hi,
Glad to hear that!
Thanks for the assist, Bob. tyle="vertical-align:middle" emoid=":thumbup:" border="0" alt="thumbup.gif" />
fkegley
Another possibility would be to use a DLookup into the table to see if the key is already present in the table. This would of course need to be in the Before Update event of the form. Then, if the key is present, you can display your own message and cancel the update. If not, then let the update proceed as usual.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.