How alert user that a purchase order number aleady exists for a part number.

On a form I have a part number field ( a text field), and a purchase order number field (also a text field).

The part number is always entered first.   Then the user enters the purchase order number.    But if there is already a record with the exact same purchase order number for that part number I want a message box to appear warning the user that tis purchase order already exists for that part number.

How can I do this?
SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
Do you only want to alert him or do you want to prevent him from saving the record with the duplicate PO number?

To alert only - use the BeforeUpdate event of the PO control.
If DCount("*", "yourtable", "PartNumber = '" & Me.txtPartNumber & "' AND PONum = '" & Me.txtPONum & "'") > 0 then
    Msgbox "There is already a PO with this number for this part.",vbOKOnly
End If

Open in new window

If you want to prevent the record from being saved, then the best place to add the code is the FORM's BeforeUpdate event.  Using any other event will not catch all cases.
If DCount("*", "yourtable", "PartNumber = '" & Me.txtPartNumber & "' AND PONum = '" & Me.txtPONum & "'") > 0 then
    Msgbox "There is already a PO with this number for this part.",vbOKOnly
    Me.txtPONum.SetFocus
    Exit Sub
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Thanks.  Am using

If DCount("*", "yourtable", "PartNumber = '" & Me.txtPartNumber & "' AND PONum = '" & Me.txtPONum & "'") > 0 then
    Msgbox "There is already a PO with this number for this part.",vbOKOnly
    Me.txtPONum.SetFocus
    Exit Sub
End If
PatHartmanCommented:
Sorry, left out something important.

If DCount("*", "yourtable", "PartNumber = '" & Me.txtPartNumber & "' AND PONum = '" & Me.txtPONum & "'") > 0 then
     Msgbox "There is already a PO with this number for this part.",vbOKOnly
     Me.txtPONum.SetFocus
    Cancel = True
     Exit Sub
 End If

Please add in the Cancel = True or the update won't be cancelled.  The bad data will be saved even though the error message is displayed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.