Given below is my code to prevent entry of duplicate record based on three different fields. This code is in the before update event
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Title <> "" And Me.StartDate <> "" And Me.EndDate <> "" Then
If DCount("*", "tblLeaveRequest", "[Title]= '" & [Title] & "' And [StartDate] = #" & [StartDate] & "# And [EndDate] = #" & [EndDate] & "# ") > 0 Then
MsgBox "Leave request already exists."
Cancel = True
Me.Undo
End If
End If
End Sub
This code successfully prevents saving of duplicate record. But the bad part is that this code is also preventing editing of any existing records. I do want to keep the ability to update existing records.
Could someone please tell me what is wrong with this code
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Title <> "" And Me.StartDate <> "" And Me.EndDate <> "" Then
If DCount("*", "tblLeaveRequest", "[Title]= '" & [Title] & "' And [StartDate] = #" & [StartDate] & "# And [EndDate] = #" & [EndDate] & "# ") > 0 Then
MsgBox "Leave request already exists."
Cancel = True
Me.Undo
End If
End If
End Sub
This code successfully prevents saving of duplicate record. But the bad part is that this code is also preventing editing of any existing records. I do want to keep the ability to update existing records.
Could someone please tell me what is wrong with this code