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:
Nz(DLookup("PK_JoinTable", "JoinTable", "FK_FirstTable = " & Me.FK_FirstTable & "AND FK_SecondTable= " & Me.FK_SecondTable & _
" AND PK_JoinTable <> " & Nz(Me.PK_JoinTable, 0)), 0)
Is possible to use DLookUp for this case ?
EDIT: Here is my Table design...
JoinTable
PK_JoinTable
FK_FirstTable
FK_SecondTable
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):
Dim Duplicates As Long
Duplicates = Nz(DLookup("PK_JoinTable", "JoinTable", "FK_FirstTable = " & Me.FK_FirstTable & "AND FK_SecondTable= " & Me.FK_SecondTable & _
" AND PK_JoinTable <> " & Nz(Me.PK_JoinTable, 0)), 0)
If Duplicates > 0 Then
Cancel = True
MsgBox "Duplicate entry. This record will not be saved !, vbCritical
DoCmd.RunCommand acCmdUndo
Exit Sub
Else
' nothing
End If