I have two text boxes. 1. To enter the First Name and 2. To enter the Last Name. After I enter the Last Name and click tab or place the cursor elsewhere, I would like Access to check for duplicate entries. If there is already an existing record, I would like Access not save the name, instead notify me. If there is no record, however, I would like Access to save the name.
I am using the following code. Sometimes, although there are no existing records, the code tells me there is an existing record. I can't figure out what is going on. Need some help, please.
Thanks.
Private Sub LastName_AfterUpdate()
Dim NewFN As String
Dim NewLN As String
Dim stLinkCriteriaFN As String
Dim stLinkCriteriaLN As String
NewFN = Me.FirstName.Value
NewLN = Me.LastName.Value
stLinkCriteriaFN = "[FirstName] = " & "'" & NewFN & "'"
stLinkCriteriaLN = "[LastName] = " & "'" & NewLN & "'"
If IsNull(NewFN) Or IsNull(NewLN) Then
MsgBox "Please enter First and Last Name to continue", vbInformation, "Enter Name"
End If
If Me.LastName = DLookup("[LastName]", "tbl_Participant", stLinkCriteriaLN) And Me.FirstName = DLookup("[FirstName]", "tbl_Participant", stLinkCriteriaFN) Then
MsgBox "This participant already exists in the database.", vbOKOnly, "Check the Name."
Me.Undo
End If
End Sub
I am using the following code. Sometimes, although there are no existing records, the code tells me there is an existing record. I can't figure out what is going on. Need some help, please.
Thanks.
Private Sub LastName_AfterUpdate()
Dim NewFN As String
Dim NewLN As String
Dim stLinkCriteriaFN As String
Dim stLinkCriteriaLN As String
NewFN = Me.FirstName.Value
NewLN = Me.LastName.Value
stLinkCriteriaFN = "[FirstName] = " & "'" & NewFN & "'"
stLinkCriteriaLN = "[LastName] = " & "'" & NewLN & "'"
If IsNull(NewFN) Or IsNull(NewLN) Then
MsgBox "Please enter First and Last Name to continue", vbInformation, "Enter Name"
End If
If Me.LastName = DLookup("[LastName]", "tbl_Participant", stLinkCriteriaLN) And Me.FirstName = DLookup("[FirstName]", "tbl_Participant", stLinkCriteriaFN) Then
MsgBox "This participant already exists in the database.", vbOKOnly, "Check the Name."
Me.Undo
End If
End Sub