473,386 Members | 1,766 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.

Find duplicate records in Access database

3
Hello everyone! Can somebody please help me with this... I have a VB program that inputs Name and Address. I've made a database in MS Access using ADO Control:

Table Name: Contacts
Fields are: ID, CompanyName, CompanyAddress

My only problem is that how can I find duplicate entries in the CompanyName field. I have an ADD command button when adding new Company Name from a textbox. What I would like to do is that before the program add the Company Name in the textbox to the database, I wanted to check first if there is any duplicate. Ive made a code but it wont work...:


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAdd_Click()
  2.  
  3.     adoWC.Recordset.MoveFirst
  4.  
  5.     while not adoWC.Recordset.EOF
  6.  
  7.          if txtCompanyName.text = adoWC.Recordset("CompanyName") then
  8.             Msgbox("Duplicate Entry")
  9.             Exit Sub
  10.          else
  11.             adoWC.Refresh
  12.             adoWC.Recordset.MoveFirst
  13.             adoWC.Recordset.AddNew
  14.             adoWC.Recordset("CompanyName") = txtCompanyName.Text
  15.             adoWC.Recordset("CompanyAddress") = txtCompanyAddress.Text
  16.             adoWC.Recordset.Update
  17.             adoWC.Recordset.MoveFirst
  18.             Exit Sub
  19.         end if
  20.  
  21.             adoWC.Recordset.MoveNext
  22.  
  23. End Sub

Can somebody help me... Thanks...
Nov 7 '07 #1
5 13836
muddasirmunir
284 100+
if you just want that no body enter duplicate name of company
just make company name field in access as a primary key.


Hello everyone! Can somebody please help me with this... I have a VB program that inputs Name and Address ...
Nov 7 '07 #2
LacrosseB0ss
113 100+
Why do you need to find duplicates? I agree with the previous poster, declaring a primary key in Access is the easiest way to prevent this. However, if there's a fair amount of data, this could corrupt some fields.

The other way to do it is nested loops. The pseudo code is below: (letters correspond to indents (loops))
1. FOR EACH ITEM
2a. store next item in a string variable (call it strCurrent).
3a. FOR EACH ITEM
4b. compare current item with strCurrent (above)
5b. if duplicate, set a boolean to true (break loops)
6c. (if needed) add to string of duplicates
7b. else, get next
8b/a. END, END

Depending on the amount of data, this may take a while to run but it's quicker then 1 guy sitting reading through everything.

The 2nd part to this is adding a new record.
1. FOR EACH ITEM
2a. compare with input, if same - break loop, error
3a. else - add to DB
4. END

Hope this helps.
- LB
Nov 8 '07 #3
debasisdas
8,127 Expert 4TB
No need to go for all the tricky logics . I will give you a simple solution.

When the user clicks add read value from textbox and compare it with database records using count() . If count is 0 insert the record if 1 then you exit and display a message saying the entry already exists.
Nov 8 '07 #4
Sheol
3
hi muddasirmunir, LacrosseB0ss, debasisdas thanks to all your reply and help.
i have a sample code here debasisdas is this what you mean?


Expand|Select|Wrap|Line Numbers
  1. Dim noDUP as integer
  2.  
  3. nuDUP = "SELECT COUNT(CompanyName) FROM Contacts WHERE CompanyName = txtCompanyName.text"
  4.  
  5. if nuDUP = 0 then
  6.     adoWC.Refresh
  7.         adoWC.Recordset.MoveFirst
  8.         adoWC.Recordset.AddNew
  9.         adoWC.Recordset("CompanyName") = txtCompanyName.Text
  10.         adoWC.Recordset("CompanyAddress") = txtCompanyAddress.Text
  11.         adoWC.Recordset.Update
  12.         adoWC.Recordset.MoveFirst
  13. else
  14.         Msgbox("Already Exists.")
  15.  
  16. End if
Thanks Again...
Nov 8 '07 #5
LacrosseB0ss
113 100+
everything here looks good except for keeping txtCompanyName.text in the quotations in your SQL statement will look for that string, not the value in the text box. You'll need to say " ... WHERE CompanyName = " & txtCompanyName.text & ";".

Other than that, looks good. By the way, nice catch debasisdas. Much easier solution. (Y)
Nov 9 '07 #6

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

Similar topics

2
by: Barbara | last post by:
Hi, I have an sql database that has the primary key set to three fields, but has not been set as unique(I didn't create the table). I have 1 record that has 2 duplicates and I am unable to delete...
1
by: Brian | last post by:
I'm trying to find a way to search multiple tables for the same record. Say I have 3 tables all with a name column, I need to search all 3 tables and find matching names. Is there an easy way to...
2
by: steevp | last post by:
Hi, Please excuse the newbie type question, but I am wracking my brains to solve what should be a simple query. I have three fields in a table F1, F2, F3. Any of the fields may contain...
1
by: Jameel | last post by:
how do i find duplicate records in ASP.net/ADO.net , i know how to do it in classic asp, anything appreciated code,tutorial links etc.. (if rs.eof then add record else error message end if )
2
by: Booster | last post by:
Hi I am a beginner working with VB.net Ho to: find data in another column using the index number "ParamID", also the possibility to updata the data in "ParamVal". This is a Parameter table ...
2
by: Carroll | last post by:
I'm looking for a way in SQL to find duplicate records in a single table, that are the same based on 3 columns, regardless of what is in the other columns in the duplicate records. I would like to...
2
by: nethravathy | last post by:
Hi, The following table namely elcbtripselect contains 5147 records.I want to know wether this table contains duplicate records or not. I tried with following query 1)SELECT...
1
by: mabubakarpk | last post by:
Hello every one, I Have table e.g EmpID TDate Status *************************************** 1 01-01-08 AB 1 02-01-08 AB 1 ...
1
by: anantha raman | last post by:
Hai, I am a beginer in Developing ASP.NET webpage. I want to restrict duplicates while uploading the excel data and to inform the user that the perticular cell in excel contains duplicate value...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.