title: Checking for duplicate before inserting record into Access
body: <p>I have a form where I don't want the user to be able to enter a contact with the same name.</p>
  
  <p>The form has a save button. To check for a duplicate, part of that button's code is :-</p>
  
  <pre><code>If Me.DataEntry = False Then
  Else
      'Check for duplicate contact
      If DCount("[ContactID]", "tblContactsNew", "[first_name] = " &amp; "'" &amp; Me.FIRST_NAME &amp; "'" &amp; "And [Surname] = " &amp; "'" &amp; Me.SURNAME &amp; "'") &lt;&gt; 0 Then
          MsgBox "A contact with these details already exists:-" &amp; vbCrLf, vbOKOnly + vbExclamation
          End
      End If
  End If
  </code></pre>
  
  <p>which is before :-</p>
  
  <pre><code>DoCmd.RunCommand acCmdSaveRecord
  </code></pre>
  
  <p>If the count &lt;> 0, the message is displayed. But if I then click my Cancel button, there is still a record added for the duplicate.</p>
  
  <p>The code for the cancel button is :-</p>
  
  <pre><code>If Me.Dirty Then
  Me.Undo
  End If
  DoCmd.Close acForm, Me.Name
  </code></pre>
  
  <p>How can I prevent the duplicate record being written?</p>