What Paul gave you will stop the update, but I believe you'd have to remove the 'cannot be duplicated' at the Table level, or the system error message will still pop up.
Alternatively, you could trap the error at the Form level, with code in the Form_Error event:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3202 Then 'Duplicate Data is Not Allowed
If Screen.ActiveControl.Name = "DateWorked" Then
Response = MsgBox("This is a Duplicate Entry for the DateWorked Field!", vbExclamation, "Duplicate Date Worked Are Not Allowed!!!")
Response = acDataErrContinue
End If
If Screen.ActiveControl.Name = "StaffName" Then
Response = MsgBox("This is a Duplicate Entry for the Staff Name!", vbExclamation, "Duplicate Staff Names Are Not Allowed!!!")
Response = acDataErrContinue
End If
End If
End Sub
But a question that I think needs to be addressed, because it does make a difference, here, is what your goal actually is. Do you
Want to not allow a duplicate Staff Name to be entered?
And/Or
Not allow a duplicate Date Worked to be entered?
Or is your goal to not allow a Record where both the Staff Name and the Date Worked are duplicated? It makes a difference in how you approach the problem!
If the latter is the situation, the code needs to be in the Form_BeforeUpdate event, and the Where Clause of DCount() needs to include both Fields. That would be something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "Your Actual Table Name", "StaffName='" & Me.StaffName & "' And [DateWorked] = #" & Me.DateWorked & "#") > 0 Then
MsgBox "This Staff Member/Date Worked Combination Has Already Been Entered! Try Again!"
Cancel = True
End If
End Sub
Linq ;0)>
The problem with making anything foolproof...is that fools are so darn ingenious!
All posts/responses based on Access 2003/2007