Check for duplicates with 2 fields

G

Guest

Hi,
I have a form that gets its data from a table with 3 fields: Key (auto
number), Item (text), and Detail (text). When I add a new record I want to
test for a duplicate records based on both the Item and Detail fields. If
there is a duplicate record I want to display my own error message and undo
the entry. How is the best way to do this? Also, the Detail field can be a
blank.
Thanks,
 
P

Pieter Wijnen

One way is to add a unique index on those fields & use Form Error to trap &
replace the default error msg (probably the best)

Or you can use the Forms Before Update even

Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = DCount(*,"MyTable","Item='" & Me.Item.Value & "' And
Description='" & Me.Description.Value & "' And ID<>" & Nz(Me.ID.Value,0))
If Cancel Then MsgBox "Dooh"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top