473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to prevent duplicate based on more than one field

mseo
181 100+
I have form based on table with following column name:

OrderID PK (autonumber), OrderNo, customer_id

OrderNo and coustomer_id are required fields

You are allowed to have Same customer, orderno one time.
For example:
1, 123 , tri-g allowed
My questions is that, for orderno with customer, I would like to prevent duplicates when entering in the form.
I can't make all the fields primary key because, as stated above that would cause duplication the customer with orderno.

Is there anyway to prevent duplicates for and when the user enters the
duplicates (customer_id with orderno, I would like to have message box pop up saying Duplicate order ?
May 27 '10 #1

✓ answered by Jim Doherty

@mseo
Firstly mseo..... please do not duplicate your posts within a day of each other!! it is a waste of other peoples time administering for this. I have deleted your other similarly worded posting on this issue. Give sufficient time to elapse without response before reposting

The idea in this instance would be to examine all pre-existing values in your table and to count the primary key value based on criteria combination of OrderNo and Customer. Where the return value is greater than zero then undo the current record edit.

The following methods pre-suppose that your OrderID and Customer_ID are TEXT fields as opposed to numeric as apostrophes are used in wrapping field contents. If numeric then merely remove the apostrophes.

The following will do that using the DCount Function in the beforeupdate property of the form based on a table called tblOrders

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. 'If CheckDuplicates(Me!OrderNo, Me!Customer_ID) Then
  3. If DCount("[OrderID]", "tblOrders", "[OrderNo]='" & Me!OrderNo & "' AND [Customer_ID]='" & Me!Customer_ID & "'") > 0 Then
  4.     msg = "You already have that OrderID and Customer_ID combination" & vbNewLine
  5.     msg = msg & "The record will now be undone"
  6.     MsgBox msg, vbExclamation, "System Duplication Message"
  7.     Me.Undo
  8.     Cancel = True
  9. End If
  10. End Sub
Notice the first commented out line? I have put this in to give you a choice here. If you do not want to use the DCount function in favour of a 'roll your own' type function then simply comment out the DCount line and uncomment that first line to call the following function that you can paste into a new module and save.

Looking at the principles of the CheckDuplicates 'roll your own' function you are passing parameter values into it that are being treated as string values by the SQL being built servicing the creation of a recordset in code.

The ultimate value of the function is true or false and is returned to calling procedure. This boolean value is arrived at by looking at the value of the 'Total' field of that created recordset (which is then closed and variables destroyed at the end of it).

This 'roll your own' method as it were, will do essentially the same thing but over very large datasets you may find faster on attached tables. Also you can pretty much adapt the specifics and flow of it to your own need.


Expand|Select|Wrap|Line Numbers
  1. Function CheckDuplicates(strOrderNo, strCustomerID) As Boolean
  2. On Error Resume Next
  3. Dim db As DAO.Database
  4. Dim rs As DAO.Recordset
  5. Set db = CurrentDb
  6. Dim mysql As String
  7. 'define an SQL string for the recordset
  8. mysql = "SELECT Count([OrderID]) as Total FROM tblOrders "
  9. mysql = mysql & "WHERE [OrderNo]='" & strOrderNo & "' AND "
  10. mysql = mysql & "[Customer_ID]='" & strCustomerID & "';"
  11. Debug.Print mysql
  12. 'open a recordset based on that sql
  13. Set rs = db.OpenRecordset(mysql, dbOpenSnapshot)
  14. 'because it counts one row will be returned to grab the value
  15. 'and depending on the result of the count set the return value
  16. 'logic to to the calling procedure
  17. Debug.Print rs!Total
  18. If rs!Total > 0 Then
  19.     CheckDuplicates = True
  20. Else
  21.     CheckDuplicates = False
  22. End If
  23.  
  24. mysql=""
  25. rs.close
  26. db.close
  27. set rs=Nothing
  28. set db=Nothing
  29.  
  30. End Function  

16 17769
Jim Doherty
897 Expert 512MB
@mseo
Firstly mseo..... please do not duplicate your posts within a day of each other!! it is a waste of other peoples time administering for this. I have deleted your other similarly worded posting on this issue. Give sufficient time to elapse without response before reposting

The idea in this instance would be to examine all pre-existing values in your table and to count the primary key value based on criteria combination of OrderNo and Customer. Where the return value is greater than zero then undo the current record edit.

The following methods pre-suppose that your OrderID and Customer_ID are TEXT fields as opposed to numeric as apostrophes are used in wrapping field contents. If numeric then merely remove the apostrophes.

The following will do that using the DCount Function in the beforeupdate property of the form based on a table called tblOrders

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. 'If CheckDuplicates(Me!OrderNo, Me!Customer_ID) Then
  3. If DCount("[OrderID]", "tblOrders", "[OrderNo]='" & Me!OrderNo & "' AND [Customer_ID]='" & Me!Customer_ID & "'") > 0 Then
  4.     msg = "You already have that OrderID and Customer_ID combination" & vbNewLine
  5.     msg = msg & "The record will now be undone"
  6.     MsgBox msg, vbExclamation, "System Duplication Message"
  7.     Me.Undo
  8.     Cancel = True
  9. End If
  10. End Sub
Notice the first commented out line? I have put this in to give you a choice here. If you do not want to use the DCount function in favour of a 'roll your own' type function then simply comment out the DCount line and uncomment that first line to call the following function that you can paste into a new module and save.

Looking at the principles of the CheckDuplicates 'roll your own' function you are passing parameter values into it that are being treated as string values by the SQL being built servicing the creation of a recordset in code.

The ultimate value of the function is true or false and is returned to calling procedure. This boolean value is arrived at by looking at the value of the 'Total' field of that created recordset (which is then closed and variables destroyed at the end of it).

This 'roll your own' method as it were, will do essentially the same thing but over very large datasets you may find faster on attached tables. Also you can pretty much adapt the specifics and flow of it to your own need.


Expand|Select|Wrap|Line Numbers
  1. Function CheckDuplicates(strOrderNo, strCustomerID) As Boolean
  2. On Error Resume Next
  3. Dim db As DAO.Database
  4. Dim rs As DAO.Recordset
  5. Set db = CurrentDb
  6. Dim mysql As String
  7. 'define an SQL string for the recordset
  8. mysql = "SELECT Count([OrderID]) as Total FROM tblOrders "
  9. mysql = mysql & "WHERE [OrderNo]='" & strOrderNo & "' AND "
  10. mysql = mysql & "[Customer_ID]='" & strCustomerID & "';"
  11. Debug.Print mysql
  12. 'open a recordset based on that sql
  13. Set rs = db.OpenRecordset(mysql, dbOpenSnapshot)
  14. 'because it counts one row will be returned to grab the value
  15. 'and depending on the result of the count set the return value
  16. 'logic to to the calling procedure
  17. Debug.Print rs!Total
  18. If rs!Total > 0 Then
  19.     CheckDuplicates = True
  20. Else
  21.     CheckDuplicates = False
  22. End If
  23.  
  24. mysql=""
  25. rs.close
  26. db.close
  27. set rs=Nothing
  28. set db=Nothing
  29.  
  30. End Function  
May 28 '10 #2
mseo
181 100+
thank you, Jim Doherty for you reply

the datatype of the table fields as the following:
OrderID PK - autonumber
OrderNo text
Customer_ID Number
so I modified your code by removing the single quote of the customer_id part
but when I run the code, msgbox fires with run-error 3075
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. 'If CheckDuplicates(Me!OrderNo, Me!Customer_ID) Then
  3. If DCount("[OrderID]", "tbl_Order", "[OrderNo]='" & Me!SON & " AND [Customer_ID]=" & Me!Customer_ID & "") > 0 Then
  4.  
  5.     MsgBox "this order is already exist", vbExclamation, "System Duplication Message"
  6.     Me.Undo
  7.     Cancel = True
  8. End If
  9. End Sub
and about the function you provided me
how can I call the function within a form?
thank you very much
May 28 '10 #3
Jim Doherty
897 Expert 512MB
@mseo
you eventually will get your head around all the quotes for strings or none for numbers where and when to define them and so on but the amendments you made are not now syntax viable. You removed in the wrong places it should be this for that Dcount function.

Expand|Select|Wrap|Line Numbers
  1. If DCount("[OrderID]", "tbl_Order", "[OrderNo]='" & Me!SON & "' AND [Customer_ID]=" & Me!Customer_ID) > 0 Then 
As for running the CheckDuplicates function the complete code event is is already provided to you you merely paste it into the BeforeUpdate event of the form. The event will call the function before any field/record isupdated....thats the way it is
May 28 '10 #4
mseo
181 100+
thank you very much Jim Doherty
It works great
appreciate your help
May 28 '10 #5
NeoPa
32,556 Expert Mod 16PB
If your intention is merely to avoid duplicating sets of values that go across multiple fields, then a Compound Index should do this for you. In Design View of the table select View / Indexes and set it up. The Unique property for the index would need to be set to Yes.
May 28 '10 #6
mseo
181 100+
@NeoPa
hi,Neopa
nice to hear from you,
If i set the unique property to yes for one field
that would prevent the duplication for this field only
even if i set the unique property to yes for other fields
indexes will treat with them separately
May 28 '10 #7
NeoPa
32,556 Expert Mod 16PB
What you say is true, but you are ignoring the Compound Index bit. You can specify an index that is made up of multiple fields.
May 28 '10 #8
mseo
181 100+
@NeoPa
hi, Neopa
Although I got the answer of my question, I need to know about the compound index and how can I make it, specially It is very useful tool
thank you
May 29 '10 #9
NeoPa
32,556 Expert Mod 16PB
If you look at the second part of post #6 you'll see instructions on how to set this up. I suggest you have a look at that and, if you still have problems, then post back with what your problem(s) is/are specifically, and we can help from there. I expect once you find the place though, all will become clear.
Jun 1 '10 #10
mseo
181 100+
hi,
if I set the unique property to yes for orderno and customer_id (not PK)fields
access will not allow duplication for each field alone
not as a compound PK
because if it is compound primary key will allow duplication depending on the other PK field within the table
thanks
Jun 1 '10 #11
NeoPa
32,556 Expert Mod 16PB
mseo: if I set the unique property to yes for orderno and customer_id (not PK)fields
access will not allow duplication for each field alone
Absolutely. That is why a Compound Key is required for this. Handling the fields individually would have exactly that undesirable effect.
mseo: not as a compound PK
because if it is compound primary key will allow duplication depending on the other PK field within the table
thanks
I don't believe there was ever any suggestion to set the Compound Key as Primary. To be honest, I'm not really sure I understand what you're saying here. Other PK, for instance is not a possible concept. There can only ever be a single PK. Every other index is additional.
Jun 2 '10 #12
mseo
181 100+
hi, Neopa
There can only ever be a single PK. Every other index is additional
that is right but I can select two fields and click primary key button, I will get the first field as primary key and second field will be indexed.
but compound Index:
I tried to do it as you told me but, I couldn't do it, because I can set the unique property for each field alone
thank you very much
Jun 2 '10 #13
NeoPa
32,556 Expert Mod 16PB
That's one possible way of creating a Compound Index. It is certainly not the only way, nor even the normal way. Generally you do this in the Indexes window, and each field can be specified as either unique or not. It is certainly not outside of your control.
Jun 2 '10 #14
@NeoPa
Thank you for this! Perfect timing :)
Feb 2 '12 #15
NeoPa
32,556 Expert Mod 16PB
I don't know about Perfect Timing Jonny, it was posted over 18 months ago :-D

It's always good news if it helps though. I'm not convinced MSEO ever quite grasped what was being said, but at least his question allows others to learn.
Feb 2 '12 #16
Hello, I followed Jim's suggestion, but I have an error and I'm not sure this would be the right solution for me, since I need to prevent duplicates in a subform that is set to display a new entry that is auto-completed with default values and by a query search.
The project is available to download from this link: https://drive.google.com/open?id=0Bz...EppbEZGNGdtYm8
Can anyone help me, please?
Nov 18 '16 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
2
by: Sky | last post by:
Hello: Another question about trying to wring functionality from a DataGrid... Have a DB table of "Contacts" -- 14 or more fields per record Show in datagrid -- but only 5 columns (First,Last,...
5
by: johnny | last post by:
hi all, I wonder if I could do the following task in PHP before searching for javascript which I prefer not to have in my code: I have a form to create new tables in a database and, since I...
11
by: ariel81 | last post by:
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the...
6
by: Arthur Dent | last post by:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the same event handler on an event? e.g... I have some object which raises an event, and some other class which consumes...
6
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: CODE <?php $c=OCILogon("scott", "tiger",...
1
by: chicago1985 | last post by:
I have a unique constraint in my Oracle table for 3 fields. If I enter duplicate info on the table using Oracle client I will get an Ora message error ORA-00001 that tells me it is a duplicate entry...
0
by: jehrich | last post by:
Hi Everyone, I am a bit of a hobby programmer (read newbie), and I have been searching for a solution to a SQL problem for a recent pet project. I discovered that there are a number of brilliant...
7
by: billa856 | last post by:
Hi, My Project is in MS Access 2002. Now I want to know that can I add more field to table.Its maximum capacity is 255 fields.But I want to add 10 more fields to that table.Is this thing possible?...
1
by: Rocker1 | last post by:
Hello guys, I am reading a downloaded textfile which has a fields like date,NAV and Name using VB.NET. The date on which the textfile is downloaded remains the same.After each readline operation...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.