G
Guest
I've posed this question in a similar thread already but would like to see if
anyone else has any further ideas. This is a stumper to me. I have a master
form where two controls are filled in (tied to a table). I need to prevent
duplicates from being entered by looking at BOTH values. I already know
about the multiple field primary keys I could set in the table, but this
won't work as the second field can contain 'null' which violates this primary
key function I guess. So I'm forced to check by code. The only option I've
gotten is by using the Dcount function but it currently allows duplicates.
My firm belief is that it has to do with encountering the null values in the
2nd field. I'm also unsure of where to even place the code (form before
update, after update OR on the controls before update, after update, lost
focus??). Anybody have any other ideas. I'll place my current code below.
This code does not prevent duplicates so far but does not generate an error
either:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "Master Contracts", "[Contract Number] = ' " _
& Me![Contract Number] & " ' AND [Order Number] = ' " _
& Me![Order Number] & " ' ") > 0 Then
Cancel = True
MsgBox "This contract number already exists in the table."
End If
End Sub
anyone else has any further ideas. This is a stumper to me. I have a master
form where two controls are filled in (tied to a table). I need to prevent
duplicates from being entered by looking at BOTH values. I already know
about the multiple field primary keys I could set in the table, but this
won't work as the second field can contain 'null' which violates this primary
key function I guess. So I'm forced to check by code. The only option I've
gotten is by using the Dcount function but it currently allows duplicates.
My firm belief is that it has to do with encountering the null values in the
2nd field. I'm also unsure of where to even place the code (form before
update, after update OR on the controls before update, after update, lost
focus??). Anybody have any other ideas. I'll place my current code below.
This code does not prevent duplicates so far but does not generate an error
either:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "Master Contracts", "[Contract Number] = ' " _
& Me![Contract Number] & " ' AND [Order Number] = ' " _
& Me![Order Number] & " ' ") > 0 Then
Cancel = True
MsgBox "This contract number already exists in the table."
End If
End Sub