duplicate number when writing to table from form VBA

nabiki

Registered User.
Local time
Today, 06:43
Joined
Dec 10, 2012
Messages
10
Hi I have a form that writes to a table using the code below, the cardnumber field is "indexed no duplicates" but as the users will be completely non-savvy i would like to override the standard error and give them an error msg that says "This number already in database please try again" with an OK button that resets the field with a NULL value ready for new input...

Code:
Private Sub cardnumbernew_AfterUpdate()
Dim rst As New ADODB.Recordset
rst.Open "Giftcards", CurrentProject.Connection, adOpenKeyset, adLockPessimistic
 With rst
        .AddNew
        !Code = Me.codecard
        !cardnumber = Me.cardnumbernew
        !Date = Me.Date2
        .Update
        .Close
       
     
   
    End With
Set rst = Nothing
DoCmd.RunMacro "submitnewcard"
Me!cardnumbernew.SetFocus
End Sub

The "submitnewcard" macro just requerys the table as it displays below the input and resets all the fierlds as null.
 
It would be more usual to validate user input in the Before Update event rather that trying to do it in the After Update event :confused: and letting the system do it for you as you will have far more control over the process.
 
Last edited:
You would trap the "duplicate" error in the form's on Error event.
 

Users who are viewing this thread

Back
Top Bottom