title: Access VBA - Dlookup for checking duplicates (composite unique key)
body: <p>I have a join table, that consist FK from 2 tables. These two fields are set for composite unique key index, and I need to check for duplicates with DlookUp, If possible. Here is what I tried:</p>
  
  <pre><code>Nz(DLookup("PK_JoinTable", "JoinTable", "FK_FirstTable = " &amp; Me.FK_FirstTable &amp; "AND FK_SecondTable= " &amp; Me.FK_SecondTable &amp; _
                      " AND PK_JoinTable &lt;&gt; " &amp; Nz(Me.PK_JoinTable, 0)), 0)
  </code></pre>
  
  <p>Is possible to use DLookUp for this case ?</p>
  
  <p>EDIT: Here is my Table design...</p>
  
  <pre><code>JoinTable
  
  PK_JoinTable
  FK_FirstTable
  FK_SecondTable
  </code></pre>
  
  <p>These foreign key table fields have unique index. I have a form where I enter data that joins both tables via Combobox. And I need to prevent duplicates entries. Both in Before_Update event, and Click event of "Save" button. This is my whole code for now (click_event of button):</p>
  
  <pre><code>Dim Duplicates As Long
  
  Duplicates = Nz(DLookup("PK_JoinTable", "JoinTable", "FK_FirstTable = " &amp; Me.FK_FirstTable &amp; "AND FK_SecondTable= " &amp; Me.FK_SecondTable &amp; _
                          " AND PK_JoinTable &lt;&gt; " &amp; Nz(Me.PK_JoinTable, 0)), 0)
  
   If Duplicates &gt; 0 Then
  
             Cancel = True
        MsgBox "Duplicate entry. This record will not be saved !, vbCritical
        DoCmd.RunCommand acCmdUndo
        Exit Sub
  
        Else
             ' nothing
        End If
  </code></pre>