What's wrong with my DCount Function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flower88
    New Member
    • Jun 2010
    • 22

    What's wrong with my DCount Function?

    Hello,

    I'm trying to get rid of duplicate entries for contacts. So in my contact entry form when the save buttom is clicked I want to use the DCount function to see if there are duplicates. Here is the code I'm trying to use

    Code:
    If DCount("*", "contacts", [First_Name] = '" & _ Me.First_Name "' & [Last_Name] ='" & _  me.Last_Name " ') >0 Then
    MsgBox "contact already exists"
    Else
    MsgBox "all good"
    End If
    It is not working and I've tried different ways to write it but I can't get it right. My fields are text fields.

    Thanks in advance for the help
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. If you've pasted your code from the actual module and not retyped it then there are some minor errors which are easy to correct. Your IF statement is currently:

    Code:
    If DCount("*", "contacts", [First_Name] = '" & Me.First_Name "' & [Last_Name] ='" &  me.Last_Name " ') >0 Then
    but it should be
    Code:
    If DCount("*", "contacts", "[First_Name] = '" & Me.First_Name & "' AND [Last_Name] ='" &  me.Last_Name & "'") > 0 Then
    Please be aware that having two contacts with the same name is quite a common occurrence and does not mean that they are necessarily duplicates - you could only tell this by looking at additional information which you don't specify here.

    -Stewart

    Comment

    • flower88
      New Member
      • Jun 2010
      • 22

      #3
      Thanks. And I was aware of the possibility of having people with the same first and last name. I'm using the company name as a third variable. So now it looks like so:

      Code:
      If DCount("*", "contacts", "[First_Name] = '" & Me.First_Name & "' AND [Last_Name] ='" & Me.Last_Name & "' AND [Company] ='" & Me.Company & "'") > 0 Then
      And it works, so thanks so much for the help Stewart!

      Comment

      Working...