I'm tired of entering a data into a form only to get a warning message about a duplicate entry AFTER I have entered everything and am ready to move onto the next record.
I have used the example code and modified it for my tables
to check single fields for duplicates before moving on. But, I need to check two fields. The combination of data in the 2 fields cannot be duplicated. Field1 can have duplicates. Field2 can have duplicates. Field1 + Field2 cannot be duplicated. I want a piece of code that checks for duplicates after I enter data into Field2 and pop up a message box stating I have a duplicate. How do I do that?
I have used the example code and modified it for my tables
Code:
Private Sub strStudentNumber_BeforeUpdate(Cancel As Integer)
'*********************************
'Code sample courtesy of srfreeman
'*********************************
Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
SID = Me.strStudentNumber.Value
stLinkCriteria = "[strStudentNumber]=" & "'" & SID & "'"
'Check StudentDetails table for duplicate StudentNumber
If DCount("strStudentNumber", "tblStudentDetails", _
stLinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "Warning Student Number " _
& SID & " has already been entered." _
& vbCr & vbCr & "You will now been taken to the record.", _
vbInformation, "Duplicate Information"
'Go to record of original Student Number
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub