473,405 Members | 2,421 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,405 software developers and data experts.

How to prevent entering duplicate value

418 256MB
My Table “Cast” has these fields:

cID (PK), cFName, cMName and cLName

The form I am using to update this table is called fCast.

I would like to prevent entering same name twice. To prevent entering same title twice I am using codes which is working. But I am not sure how to concatenate 3 fields and apply similar codes. Can someone please help? Thanks.

My code to prevent duplicating a title:


Expand|Select|Wrap|Line Numbers
  1. Private Sub iTitle_AfterUpdate()
  2.  
  3.  
  4.  
  5. Dim iTitle As String
  6.     Dim stLinkCriteria As String
  7.     Dim rsc As DAO.Recordset
  8.  
  9.     Set rsc = Me.RecordsetClone
  10.  
  11.     iTitle = Me.iTitle.Value
  12.     stLinkCriteria = "[iTitle]=" & "'" & Replace(Me![iTitle], "'", "''") & "'"
  13.  
  14.  
  15.  
  16.     'Check Items table for duplicate iTitle
  17.     If DCount("iTitle", "Items", _
  18.               stLinkCriteria) > 0 Then
  19.  
  20.  
  21.         'Undo duplicate entry
  22.         Me.Undo
  23.         'Message box warning of duplication
  24.         MsgBox "Warning Item Title " _
  25.              & iTitle & " has already been entered." _
  26.              & vbCr & vbCr & "You will now been taken to the record.", _
  27.                vbInformation, "Duplicate Information"
  28.         'Go to record of original Title
  29.         rsc.FindFirst stLinkCriteria
  30.         Me.Bookmark = rsc.Bookmark
  31.     End If
  32.  
  33.     Set rsc = Nothing
  34.  
  35. End Sub
May 24 '10 #1

✓ answered by ADezii

@MNNovice
Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria As String
  2. Dim strCriteria_2 As String
  3.  
  4. strCriteria = "[Firstname] = '" & Me![FirstName] & "' AND Nz([MI]) = '" & _
  5.                Nz(Me![MI]) & "' AND [LastName] = '" & Me![LastName] & "'"
  6.  
  7. If IsNull(Me![LastName]) Then
  8.   strCriteria_2 = "[Firstname] = '" & Me![FirstName] & "' AND Nz([MI]) = '" & Nz(Me![MI]) & "'"
  9.     If DCount("*", "Employees", strCriteria_2) > 0 Then
  10.       MsgBox Me![FirstName] & " " & IIf(Len(Me![MI]) = 0, "", Me![MI]) & _
  11.              " already exists!", _
  12.                vbExclamation, "Duplicate Entry on Name"
  13.                  Cancel = True
  14.     End If
  15. Else
  16.   If DCount("*", "Employees", strCriteria) > 0 Then
  17.     MsgBox Me![FirstName] & " " & IIf(Len(Me![MI]) = 0, "", Me![MI] & " ") & _
  18.            Me![LastName] & " already exists!", _
  19.              vbExclamation, "Duplicate Entry on Name"
  20.                Cancel = True
  21.   End If
  22. End If

28 11389
ADezii
8,834 Expert 8TB
@MNNovice
The easiest Method of making sure that no 2 Names are exact, namely the combination of First Name, MI, and Last Name is to create a Unique Index (No Duplicates allowed) consisting of these 3 Fields. If you are not sure how to do this, just let us know.
May 24 '10 #2
MNNovice
418 256MB
@ADezii
I do not know how to do this unique indexing. Please let me know. Thanks.
May 24 '10 #3
ADezii
8,834 Expert 8TB
@MNNovice
  1. Open the Cast Table in Design View.
  2. Click on View ==> Indexes.
  3. In the 1st open Row under the Index Name Column, enter FullName.
  4. In the same Row, select cFName under the Field Name Column.
  5. In the same Row, set the Sort Order for this Field.
  6. Without leaving this Row, under Index Properties set:
    • Primary = Yes
    • Unique = Yes
    • Ignore Nulls = No
  7. Drop down to the next Row, select cMName for Field Name, and again set the Sort Order.
  8. Drop down to the next Row and select cLName for Field Name, again set the Sort Order.
  9. Exit the Dialog.
Now, Alfred E Newmann will not be allowed to be entered into these Fields twice, but Alfred A Newmann and Alfred Newmann will.
May 24 '10 #4
MNNovice
418 256MB
@ADezii
Primary Key for Cast table was originally set to be cID - I had to delete the relationship of this table to make the changes you suggested. Now I am facing another problem.

1. How do I restablish the relationship so all other tables, forms and reports are not affected?
2. What, if any, choices I have in the event there are two persons with the same name?

Thanks.
May 25 '10 #5
ADezii
8,834 Expert 8TB
@MNNovice
My mistake in that you should set the Primary Key Property of the Index to No (Item #6 - Step #1), while setting the Unique Property = Yes. You could then re-establish the Original Relationships based on the [cID] Field.
May 25 '10 #6
MNNovice
418 256MB
@ADezii
I changed my tables indexes to look like this. Still it's allowing duplicates of names like Richard Gere. What am I doing wrong?
Expand|Select|Wrap|Line Numbers
  1. Rows  Index Name  Field Name  Sort Order
  2. 1     FullName    cFName      Ascending
  3. 2                 cMName      Ascending
  4. 3                 cLName      Ascending
  5. 4     ID          cID         Ascending
  6.  
  7.   Primary  No  
  8.   Unique  Yes  applies to rows 1 - 3
  9.   Ignore Nulls  No
May 25 '10 #7
ADezii
8,834 Expert 8TB
@MNNovice
The problem, as I see it, has to do with how the Index is comparing NULLS in the cMName Field within the Index. It does not equate two NULLs which makes sense since a NULL cannot be equal to anything else, even another NULL. The way I see is is that you have two Options:
  1. Create a Unique Index on the First and Last Name Fields which would not make sense since Richard Gere and Richard A Gere would be considered a Duplicate which they very well may not be.
  2. Check the Values in the First, MI, and Last Name Fields for Duplication in the BeforeUpdate() Event of a Data Entry Form. This is more complex, but more efficient.
May 25 '10 #8
OldBirdman
675 512MB
If this were my project, I would not allow cMName to be null. By setting the initial value to "", the string is zero length, but is not null.
If these names are entertainers, as the example Richard Gere suggests, then cFName and cLName should be treated the same way, as some entertainers use only their first (Cher) or their last (Liberace) name.
Now is also the time to consider whether names such as Sir Laurence Olivier or Douglas Fairbanks, Jr. are going to be entered into the database, and how you will handle them.
May 25 '10 #9
MNNovice
418 256MB
@OldBirdman
OldBirdman,

Thanks for your comments. But you didn't say what I should be doing. Is it worth the trouble of breaking up names like this? Or just keep it as "cName" to indicate full name of an actor. Because like you said there are too many variables to consider, (only first name, only last name....)

I wait for your comments. Thanks.
May 26 '10 #10
OldBirdman
675 512MB
"... I would not allow cMName to be null. By setting the initial value to "", the string is zero length, but is not null." This is a specific suggestion to eliminate null values in cMName. I suggest you not use null to mean a zero-length string. Testing/comparing to a null value is different than testing/comparing strings or numeric values. This is true in VBA and SQL. The simple/easy solution is to not let them happen, and Access table field properties make it easy and automatic.
"... cFName and cLName should be treated the same way..." This too is a specific suggestion. Once you change the properties of cMName, do the same for cFName and cLName also. This makes all name fields the same, so programming errors will be less likely in the future.
I don't know how you will handle honorifics(prefixes) and/or suffixes. I am a believer in design BEFORE implimentation. My examples were only to expand on the names, and have you consider what you needed to consider uniqueness.
When working with table fields derived from 'natural' language, many problems must be considered.
1) People's names - a few things to consider:
a) Sir Laurence Olivier wasn't always Sir Laurence.
b) Some people use their full middle name, not just an initial. Peggy Sue is not Peggy S.
c) Some people have multiple middle names - J.R.R.Tolken
d) Some people only use a first initial.
e) The suffix "Jr." may distinguish between two individuals
f) Two actors may have the same name. This is usually not a problem unless one later becomes a star. Then the old films may be revived.
2) Movie/Music/Book titles
a) Duplicates often occur
b) Normally the articles (English) "A", "An", and "The" are ignored in sorting and listing
c) Sorting of numbers within names can result in different sort order.
d) No standard spelling. English examples Grey/Gray or Theater/Theatre.
e) Numbers replace words Second/2nd or Twelve/12.
Does this answer your questions. No, because I cannot. When you decide what to store, and how, then the queries can be addressed. It may be that the best solution is to Filter the data, and let the user select from a small subset of the table. Or maybe not.
May 26 '10 #11
Marknut
42
Back to your original question and coding technique, why not try something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Function Check_Dups() 
  2.  
  3. Dim iNames As String 
  4.     Dim stLinkCriteria As String 
  5.     Dim rsc As DAO.Recordset 
  6.  
  7.     Set rsc = Me.RecordsetClone 
  8.  
  9.     iNames = Me.cFName.Value & " " & Me.cMName.Value & " " & Me.cLName.Value
  10.     stLinkCriteria = "[cFName] like Forms!frmForm1![cFName] AND [cMName] like Forms!frmForm1![cMName] AND [cLName] like Forms!frmForm1![cLName]")
  11.  
  12.  
  13.  
  14.     'Check Items table for duplicate Names
  15.     If DCount("cNameID", "tblNames", _ 
  16.               stLinkCriteria) > 0 Then 
  17.  
  18.  
  19.         'Undo duplicate entry 
  20.         Me.Undo 
  21.         'Message box warning of duplication 
  22.         MsgBox "Warning Item Name " _ 
  23.              & iNames & " has already been entered." _ 
  24.              & vbCr & vbCr & "You will now been taken to the record.", _ 
  25.                vbInformation, "Duplicate Information" 
  26.         'Go to record of original Title 
  27.         rsc.FindFirst stLinkCriteria 
  28.         Me.Bookmark = rsc.Bookmark 
  29.     End If 
  30.  
  31.     Set rsc = Nothing 
  32.  
  33. End Function
May 26 '10 #12
MNNovice
418 256MB
@MNNovice
MarkNut:

Thanks for your suggestion. I tried the following code and added it to Modules (Global Code). It didn't like the Me. command. So I added to sfCastOther (which is the actual sub form I am using to add / delete names). However, the code didn't stop entering duplicate names.

Please suggest. Thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Function Check_Dups()
  2. Dim FullName As String
  3.     Dim stLinkCriteria As String
  4.     Dim rsc As DAO.Recordset
  5.  
  6.     Set rsc = Me.RecordsetClone
  7.  
  8.     FullName = Me.cFName.Value & " " & Me.cMName.Value & " " & Me.cLName.Value
  9.     stLinkCriteria = "[cFName] like Forms!sfCastOther![cFName] AND [cMName] like Forms!sfCastOther![cMName] AND [cLName] like Forms!sfCastOther![cLName]"
  10.  
  11.  
  12.  
  13.     'Check Items table for duplicate Names
  14.     If DCount("cID", "Cast", _
  15.               stLinkCriteria) > 0 Then
  16.  
  17.  
  18.         'Undo duplicate entry
  19.         Me.Undo
  20.         'Message box warning of duplication
  21.         MsgBox "Warning Item Name " _
  22.              & FullName & " has already been entered." _
  23.              & vbCr & vbCr & "You will now been taken to the record.", _
  24.                vbInformation, "Duplicate Information"
  25.         'Go to record of original Title
  26.         rsc.FindFirst stLinkCriteria
  27.         Me.Bookmark = rsc.Bookmark
  28.     End If
  29.  
  30. End Function
May 26 '10 #13
ADezii
8,834 Expert 8TB
The following Code, placed in the BeforeUpdate() Event should handle 'most' contingencies, just substitute you own Field Names and Text Box Names. The only problem with this Code is that Andrew A Fuller and Andrew A. Fuller would NOT be considered Duplicates because of the period after the MI in the second case.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Dim strCriteria As String
  3.  
  4. strCriteria = "[Firstname] = '" & Me![FirstName] & "' AND Nz([MI]) = '" & _
  5.                Nz(Me![MI]) & "' AND [LastName] = '" & Me![LastName] & "'"
  6.  
  7. If DCount("*", "Employees", strCriteria) > 0 Then
  8.   MsgBox Me![FirstName] & " " & IIf(Len(Me![MI]) = 0, "", Me![MI] & " ") & _
  9.          Me![LastName] & " already exists!", _
  10.            vbExclamation, "Duplicate Entry on Name"
  11.              Cancel = True
  12. End If
  13. End Sub
May 26 '10 #14
Marknut
42
Right on ADezii, that should do it.

MNNovice, I've attached a very basic example for your reference in case you have any issues.
Attached Files
File Type: zip Name_Dups.zip (21.7 KB, 289 views)
May 26 '10 #15
MNNovice
418 256MB
@ADezii
AdeZii:

Thanks for your tips. I modified the code and added to the sub form. It works ONLY where the name has all 3 parts (first, middle and last). If someone has only first name - it allows duplicates.

Any modification to the codes? Here is what I used. Thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Dim strCriteria As String
  3.  
  4. strCriteria = "[cFName] = '" & Me![cFName] & "' AND Nz([cMName]) = '" & _
  5.                Nz(Me![cMName]) & "' AND [cLName] = '" & Me![cLName] & "'"
  6.  
  7. If DCount("*", "Cast", strCriteria) > 0 Then
  8.   MsgBox Me![cFName] & " " & IIf(Len(Me![cMName]) = 0, "", Me![cMName] & " ") & _
  9.          Me![cLName] & " already exists!", _
  10.            vbExclamation, "Duplicate Entry on Cast "
  11.              Cancel = True
  12. End If
  13.  
  14. End Sub
May 26 '10 #16
MNNovice
418 256MB
@Marknut
Marknut:

Thanks again. Perhaps I should have explained in greater details about the table structures.

sfCastOther is based on "Cast". It's one of several subforms that is residing on a form called fOther. I use fOther to add delete many other data like category, language etc.

fOther is based on a query called qOtherList which has a table called OtherList. This table has two fields and no primary key: olCaption and olObject. Both these fields use text data type.

Now on to your little DB sample. I understand how it works. But how do I incorporate into my form is where I am stuck.

Let me ask you this, can I use the code behind the command button on cLName Before Update event? If not, what are my choices?

Thanks.
May 26 '10 #17
MNNovice
418 256MB
@ADezii
ADzeii,

The codes working only when all three parts of a name exist. It does not work when there is only one name (cFName), like Cher. Can you direct me how to modify these codes to include only firstname and/or only last name?

Thanks.
May 27 '10 #18
ADezii
8,834 Expert 8TB
@MNNovice
You could check for every possible contingency, but it would significantly increase the amount of code. The easiest approach would be to make the First Name Required, while the MI and Last Names are not. Is this doable on your end?
May 27 '10 #19
NeoPa
32,556 Expert Mod 16PB
I'm sorry. I'm coming to this late, but has the option of setting up a Compound Index covering these three fields been discussed yet? It seems to me that this would be a viable solution. Clearly Nulls would have to be allowed in the individual composite fields, but as long as all three were not Null it should be able to include the values in the index and duplication would automatically be precluded.
May 27 '10 #20
MNNovice
418 256MB
@ADezii
Yes, requiring first name is possible. I made this change in the cFName's property making Required to Yes. What else do I need to do?
May 27 '10 #21
MNNovice
418 256MB
@NeoPa
NeoPa:

Please see Posting #7. Is this what you referred to as Compound Indexing? It didn't work. Thanks.
May 27 '10 #22
ADezii
8,834 Expert 8TB
@NeoPa
This was my advice in Post #4, maybe I missed something.
May 27 '10 #23
Marknut
42
@MNNovice
Haha, sorry, the answers are everywhere here because you definitely have multiple options. You should be able to use that code wherever you want. This doesn't seem like it should be too hard to implement in your db. How large is your database? Is it confidential information or anything? If you zip it and post it I'd be glad to make changes, note them down, and repost. I'm sure ADezii or NeoPa would be glad to do the same.
May 27 '10 #24
ADezii
8,834 Expert 8TB
@MNNovice
Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria As String
  2. Dim strCriteria_2 As String
  3.  
  4. strCriteria = "[Firstname] = '" & Me![FirstName] & "' AND Nz([MI]) = '" & _
  5.                Nz(Me![MI]) & "' AND [LastName] = '" & Me![LastName] & "'"
  6.  
  7. If IsNull(Me![LastName]) Then
  8.   strCriteria_2 = "[Firstname] = '" & Me![FirstName] & "' AND Nz([MI]) = '" & Nz(Me![MI]) & "'"
  9.     If DCount("*", "Employees", strCriteria_2) > 0 Then
  10.       MsgBox Me![FirstName] & " " & IIf(Len(Me![MI]) = 0, "", Me![MI]) & _
  11.              " already exists!", _
  12.                vbExclamation, "Duplicate Entry on Name"
  13.                  Cancel = True
  14.     End If
  15. Else
  16.   If DCount("*", "Employees", strCriteria) > 0 Then
  17.     MsgBox Me![FirstName] & " " & IIf(Len(Me![MI]) = 0, "", Me![MI] & " ") & _
  18.            Me![LastName] & " already exists!", _
  19.              vbExclamation, "Duplicate Entry on Name"
  20.                Cancel = True
  21.   End If
  22. End If
May 27 '10 #25
MNNovice
418 256MB
@ADezii
ADzeii:

Thank you very much. It worked perfectly. Much appreciated. M
May 27 '10 #26
ADezii
8,834 Expert 8TB
@MNNovice
You are quite welcome.
May 27 '10 #27
NeoPa
32,556 Expert Mod 16PB
MNNovice: Please see Posting #7. Is this what you referred to as Compound Indexing? It didn't work. Thanks.
This is pretty well what I meant yes. It would be interesting to know in what way it failed. This would certainly be the area I would explore more deeply before any other. I appreciate that may be a little more difficult for you than us of course. If you attach a database as ADezii suggested, then please explain also in what way this is not working for you then I can explore for myself a little and see if I can see why this (most sensible approach) is not working in your project.
May 27 '10 #28
MNNovice
418 256MB
@NeoPa
NeoPa / ADezii

It's working perfectly so far. Shall let you know if I encounter any problems. Thanks again.
Jun 1 '10 #29

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

Similar topics

2
by: Fatou | last post by:
I have a listbox where the user selects only one value at a time. The problem is that once the user selects the value and the record is updated in the table, they can reselect the same value...
3
by: mg | last post by:
In the click event handler of an imagebutton (WebForm), my C# code gets the values in some textboxes and insert these values into database tables The page reloads itself after this event and the...
9
by: phopman | last post by:
Hi there! Since this is my first post, I'll try to be gentle with you :-) I have a script that goes like this: Connect to db. select data from db, and list up (only one variable, text). I...
5
by: lyne_asp | last post by:
Hello everbody, Please help me to remove the duplicate value. I have this code Do until objRSEmail.eof tgroup = objRSEmail("tEmailName")&"@city.com;"&tgroup ...
4
by: lucky13 | last post by:
Dear All, can u help in how to check the duplicate value in the listview control before adding new in the list. Regards Lucky13
1
by: vishwa Ram | last post by:
Hi ScriptFamily, I need to remove a duplicate value in array. Any specific method available in perl. pls suggest Regards Raam
3
by: spl | last post by:
What is the fastest way to find a duplicate value in an array of 10 elements. I have to find just any one first occurrence of the duplication. Lets say I have ary={10, 20, 40, 90, 30, 60, 35, 40,...
2
by: =?Utf-8?B?ZG90cHJvMjAwOA==?= | last post by:
In ASP.NET 2.0 project, I have added a dataset and have setup the datatable and configured the datatableadapter (using the wizards). In my code I can use the mytableadapter.insert method to...
4
by: azura | last post by:
how i want to check if there is duplicate entry in my database?? i insert new record which is the same record that i had inserted before,it successfull insert into the database..i don't want that.....
1
by: sweetline priya | last post by:
in my project, the user can enter the date in the format of dd-mm-yyyy in the textbox. after entering the day value, automatic '-' (hyphen) has to be generated.. and after entering the month value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.