customize error message (1 Viewer)

mlopes1

Registered User.
Local time
Today, 18:09
Joined
Sep 4, 2002
Messages
76
Is there way to customize error messages? I would like to have my own text be displayed when a user tries to enter duplicate value for an indexed field. Its run time error 3022. I found previous posts on this topic BUT, my duplicate is on a composite key, two primary keys, so I am unsure how to apply it to 2 fields.

Thanks for your help as always.

Marco
 
Last edited:

ghudson

Registered User.
Local time
Today, 17:09
Joined
Jun 8, 2002
Messages
6,195
I depends on what event you are using your code with. You need to test for the error number
in each event that might trigger the error number 3022. My example below used the command
button bAddRecord OnClick event and I am trapping for your error 3022...

Code:
Private Sub bAddRecord_Click()
On Error GoTo Err_bAddRecord_Click
    
'your code here
    
Exit_bAddRecord_Click:
    Exit Sub
    
Err_bAddRecord_Click:
    If Err = 3022 Then '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.
        Beep
        MsgBox "You can not do that because you would be creating a duplicate index and that is not allowed.", vbCritical, "Shame on you for trying to create a duplicate index!"
        Exit Sub
    Else
        MsgBox Err.Number, Err.Description
        Resume Exit_bAddRecord_Click
    End If
    
End Sub
 

mlopes1

Registered User.
Local time
Today, 18:09
Joined
Sep 4, 2002
Messages
76
That worked perfect. Thank you again. Can this technique be used on any event in a form? It seems very powerful concept.
 

ghudson

Registered User.
Local time
Today, 17:09
Joined
Jun 8, 2002
Messages
6,195
:D
That worked perfect. Thank you again. Can this technique be used on any event in a form? It seems very powerful concept.
YES! You should have error trapping on ALL of your coded events (subs and functions)!
 

Users who are viewing this thread

Top Bottom