Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No duplicates based on 3rd index, not primary 1

Status
Not open for further replies.

billmeye

Technical User
Dec 24, 2004
24
US
I have a table with an autonumber primary field (so I can cascade update many other tables and so I can't remove this required index) a 2nd field that is a customer name and a third field that is a customer order. I can have many instances of the same customer, but I only want 1 instance of each order type. So, in the form, if the data entry person selects an order type that the customer alreay has open, I want the form to be able to determine that the type is already used and not allow this duplicate record. Thanks.
 
How are ya billmeye . . . . .

What are the actual field names?

What is the table name?

Is Customer Order string or numeric?

Calvin.gif
See Ya! . . . . . .
 
Table name CustomerOrders

Autonumber
CustomerName type text
OrderType type text

Thanks for the help
 
OK billmeye . . . . .

In the [purple]BeforeUpdate[/purple] event of [purple]OrderType[/purple], copy/paste the following:
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String
   Dim DL As String, Criteria As String
   
   DL = vbNewLine & vbNewLine
   
   Criteria = "[CustomerName]='" & Me!CustomerName & "' AND " & _
              "[OrderType]='" & Me!OrderType & "'"
   
   If Not IsNull(DLookup("[OrderType]", "CustomerOrders", Criteria)) Then
      Msg = "OrderType '" & Me!OrderType & "' already exist for " & _
            Me!CustomerName & "!" & DL & _
            "Duplicate OrderTypes are Not Allowed!" & DL & _
            "Try again using a different Type . . ."
      Style = vbInformation + vbOKOnly
      Title = "Duplicate OrderType Error! . . ."
      MsgBox Msg, Style, Title
      [purple][b]Cancel = True[/b][/purple]
   End If[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks for the quick reply, I can tell your close, except I made a mistake. The order type is an integer and I think, because of this, I'm getting a type mismatch. So, can you tell me how the code should look with that change.

Thanks.
 
Replace this:
"[OrderType]='" & Me!OrderType & "'"
By this:
"[OrderType]=" & Me!OrderType

You may also consider creating a composite index with no dup on CustomerOrders(CustomerName,OrderType)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PHV and AceMan1, you solved my troubles and taught me yet again some new code. I never tried passing multiple criteria in a dlookup.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top