Check for Duplicates (1 Viewer)

mike60smart

Registered User.
Local time
Today, 14:59
Joined
Aug 6, 2017
Messages
1,905
Hi
I appear to have a mental block on checking for Duplicates for a Members Data Entry Form.

The 2 fields are Forenames and Surname.

How would I code the AfterUpdate of the Surname Control to display the Duplicate warning?

Your help appreciated
 

bastanu

AWF VIP
Local time
Today, 06:59
Joined
Apr 13, 2010
Messages
1,402
Use a dCount or dLookup in the source table to check if you already have an entry.

Cheers,
 

cheekybuddha

AWF VIP
Local time
Today, 14:59
Joined
Jul 21, 2014
Messages
2,280
Something like:
Code:
If DCount( _
  "*", _
  "Contacts", _
  "FirstName='" & Replace(Me.txtFirstName & "", "'", "''")) & "' " & _
  "LastName='"& Replace(Me.txtLastName & "", "'", "''")) & "'" _
) > 0 Then
  MsgBox "Dupe"
End If

But perhaps you would be better off using BeforeUpdate:
Code:
Dim dupe As Boolean

dupe = DCount( _
  "*", _
  "Contacts", _
  "FirstName='" & Replace(Me.txtFirstName & "", "'", "''")) & "' " & _
  "LastName='"& Replace(Me.txtLastName & "", "'", "''")) & "'" _
) > 0

If dupe Then MsgBox "Dupe"
Cancel = dupe
 

mike60smart

Registered User.
Local time
Today, 14:59
Joined
Aug 6, 2017
Messages
1,905
Something like:
Code:
If DCount( _
  "*", _
  "Contacts", _
  "FirstName='" & Replace(Me.txtFirstName & "", "'", "''")) & "' " & _
  "LastName='"& Replace(Me.txtLastName & "", "'", "''")) & "'" _
) > 0 Then
  MsgBox "Dupe"
End If

But perhaps you would be better off using BeforeUpdate:
Code:
Dim dupe As Boolean

dupe = DCount( _
  "*", _
  "Contacts", _
  "FirstName='" & Replace(Me.txtFirstName & "", "'", "''")) & "' " & _
  "LastName='"& Replace(Me.txtLastName & "", "'", "''")) & "'" _
) > 0

If dupe Then MsgBox "Dupe"
Cancel = dupe
Hi David
How do we arrive at the field "Me.txtFirstName" & "Me.txtLastName" ?

I tried the following Code and get the error as shown

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

10        On Error GoTo Form_BeforeUpdate_Error
      Dim dupe As Boolean

20    dupe = DCount("*", "[Member Details]", _
        "Forenames='" & Replace(Me.Forenames & "", "'", "''")) & "' " & _
        "Surname='" & Replace(Me.Surname & "", "'", "''") & "'" > 0

30    If dupe Then MsgBox "Dupe"
40    Cancel = dupe
          
50        On Error GoTo 0
60        Exit Sub

Form_BeforeUpdate_Error:

70        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate, line " & Erl & "."

End Sub
 

Attachments

  • error.jpg
    error.jpg
    19 KB · Views: 51

bastanu

AWF VIP
Local time
Today, 06:59
Joined
Apr 13, 2010
Messages
1,402
Missing AND in the criteria clause of the dCount between the two fields).
 

cheekybuddha

AWF VIP
Local time
Today, 14:59
Joined
Jul 21, 2014
Messages
2,280
So, do you have a field named 'Forenames' (plural) in table Member Details?
 

cheekybuddha

AWF VIP
Local time
Today, 14:59
Joined
Jul 21, 2014
Messages
2,280
Try again with:
Code:
' ...
20    dupe = DCount("*", "[Member Details]", _
        "Forenames='" & Replace(Me.Forenames & "", "'", "''") & "' AND " & _
        "Surname='" & Replace(Me.Surname & "", "'", "''") & "'") > 0
' ...
 

cheekybuddha

AWF VIP
Local time
Today, 14:59
Joined
Jul 21, 2014
Messages
2,280
There was also a mis-placed paren - hopefully corrected above in Post #9
 

cheekybuddha

AWF VIP
Local time
Today, 14:59
Joined
Jul 21, 2014
Messages
2,280
No that still has the misplaced paren from before I edited post #9.

It should be this:
Code:
' ...
20    dupe = DCount("*", "[Member Details]", _
        "Forenames='" & Replace(Me.Forenames & "", "'", "''") & "' AND " & _
        "Surname='" & Replace(Me.Surname & "", "'", "''") & "'") > 0
' ...
 

mike60smart

Registered User.
Local time
Today, 14:59
Joined
Aug 6, 2017
Messages
1,905
No that still has the misplaced paren from before I edited post #9.

It should be this:
Code:
' ...
20    dupe = DCount("*", "[Member Details]", _
        "Forenames='" & Replace(Me.Forenames & "", "'", "''") & "' AND " & _
        "Surname='" & Replace(Me.Surname & "", "'", "''") & "'") > 0
' ...
Hi David
Many thanks yet again that works just great
 

Users who are viewing this thread

Top Bottom