473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete Duplicate records

4 New Member
I am trying to delete duplicate records in an access table using VB. Not a programmer here, but this was one of the codes that I found on the web. I made some modifications to it, but it still did not work for me.
Expand|Select|Wrap|Line Numbers
  1. Public Function DeleteDuplicate()
  2. Dim db As DAO.Database
  3. Dim qdf As QueryDef
  4. Dim strSQL As String
  5. Dim strEmail As String
  6.  
  7. dteEmail = DMax("[email]", "tbl_email")
  8.  
  9. strSQL = "Delete * FROM tbl_email" & _
  10. "WHERE (tbl_email.email)> strEmail"
  11.  
  12. Set db = CurrentDb
  13. With db
  14. Set qdf = .CreateQueryDef("tmpQuery", strSQL)
  15. DoCmd.OpenQuery "tmpQuery"
  16. .QueryDefs.Delete "tmpQuery"
  17. End With
  18. db.Close
  19. qdf.Close
  20. End Function
Any assistance is greatly appreciated.
Oct 19 '06 #1
16 16187
comteck
179 New Member
Create a query based on the table that you are checking for duplicates from. Call the query "qryDuplicates" . Open the query in design view, and enter the following statement in the field that you are checking for duplicates (i.e if you are checking for duplicate serial numbers, and that field is called "serialnumber") , enter this statement in criteria under the field "serialnumb er":

In (SELECT [serialnumber] FROM [tablename] As Tmp GROUP BY [serialnumber] HAVING Count(*)>1 )

After doing that, create an APPEND query, and base it on the qryDuplicates query. When you run the APPEND query, it should delete all the duplicates in that table.

Hope this helps.
comteck
Oct 19 '06 #2
Killer42
8,435 Recognized Expert Expert
I don't get it. How can this delete anything? Won't it just append to somewhere the complete list of records which have duplicate serial numbers?
Oct 20 '06 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
dteEmail = DMax("[email]", "tbl_email" )

should be

strEmail = DMax("[email]", "tbl_email" )

db.Close ' never close the database just set it to nothing and close the query first. i.e.

qdf.Close
Set qdf = Nothing
Set db = Nothing
Oct 20 '06 #4
Theodore70
4 New Member
Apologies. I wasn't not clear on my original post.

I would like to delete duplicate records (email field), but keep at least one record. I have revised the script based on the replies that I have recieved, but I am still not able to get the script to run correctly.

Any help is appreciated.

Thanks.
Oct 20 '06 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
What is the value in email field. As you are using DMax I assume it's a number or date. Can you confirm?

If it's a date, are you just trying to create a list of emails by the last date sent by deleting all other emails.
Otherwise what is the criteria?

Apologies. I wasn't not clear on my original post.

I would like to delete duplicate records (email field), but keep at least one record. I have revised the script based on the replies that I have recieved, but I am still not able to get the script to run correctly.

Any help is appreciated.

Thanks.
Oct 20 '06 #6
Theodore70
4 New Member
The values of DMax are alphanumeric.

I am trying to delete duplicated (SMTP - email) since it is my only unique identifier available. It is a text field.

Thanks.

<What is the value in email field. As you are using DMax I assume it's a number or date. Can you confirm?

If it's a date, are you just trying to create a list of emails by the last date sent by deleting all other emails.
Otherwise what is the criteria>
Oct 20 '06 #7
comteck
179 New Member
I don't get it. How can this delete anything? Won't it just append to somewhere the complete list of records which have duplicate serial numbers?
Sorry Killer.... it did work for me. Might be a different case here. However, not everybody is correct in their solutions that they offer on here. Normally however, people are not rude about it.
Oct 20 '06 #8
MMcCarthy
14,534 Recognized Expert Moderator MVP
DMax gets the Maximum Value you can't do that with an AlphaNumeric field.

BACKUP the table first!!

Try the following. First add a field to the table called temp with a true/false datatype. You can delete it later.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function DeleteDuplicate()
  3. Dim db As DAO.Database
  4. Dim rs As RecordSet
  5. Dim strEmail As String
  6.  
  7. Set db = CurrentDb
  8. Set rs = db.OpenRecordset("tbl_email")
  9.  
  10. StartFile:
  11.  
  12. rs.MoveFirst
  13. Do Until rs!temp = False
  14.   If not rs.EOF then
  15.     rs.MoveNext
  16.   Else
  17.     GoTo EndFile
  18.   End If
  19. Loop
  20.  
  21. strEmail = rs!email
  22. rs!temp = True
  23. rs.MoveNext
  24.  
  25. Do Until rs.EOF
  26.   If rs!email = strEmail Then
  27.     rs.Delete
  28.   End If
  29.   rs.MoveNext
  30. Loop
  31.  
  32. GoTo StartFile
  33.  
  34. EndFile:
  35.  
  36. rs.close
  37. set rs = Nothing
  38. set db = Nothing
  39.  
  40. End Function
  41.  
  42.  
Oct 20 '06 #9
NeoPa
32,579 Recognized Expert Moderator MVP
M McCarthy - how can you do this to me?
I was working on a little routine for this thread when you posted a perfectly good answer.
Not cool ;-)

Anyway, as I've done it I might as well include it here.

This code does assume a table in the local db but it can be tweaked or enhanced.
For instance, if the one to be kept must have the lowest Primary Key value then it could be changed to do that.

Expand|Select|Wrap|Line Numbers
  1. 'DelDups Removes duplicate records in strTable matched on strField
  2. Public Sub DelDups(strTable As String, strField As String)
  3.     Dim strSQL As String, varLastVal As Variant
  4.  
  5.     'Recordset must be full table but sorted by the field we're checking
  6.     strSQL = "SELECT * FROM [" & strTable & "] ORDER BY [" & strField & "]"
  7.     With CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
  8.         varLastVal = Null
  9.         'For each record, check against previous value in strField
  10.         'If same then this is a duplicate so can be removed
  11.         Do Until .EOF
  12.             If .Fields(strField) = varLastVal Then
  13.                 Call .Delete
  14.             Else
  15.                 varLastVal = .Fields(strField)
  16.             End If
  17.             Call .MoveNext
  18.         Loop
  19.     'Ending the 'With' releases the Recordset
  20.     End With
  21. End Sub
Hope it helps.
Oct 20 '06 #10

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

Similar topics

0
4052
by: Joe | last post by:
I used LOAD DATA FILE to load data into a mysql database. I discovered that there are lots of duplicate records. Can anyone give me a simple list of mysql commands to search and destroy duplicate records? Thanks
1
10810
by: Patrizio | last post by:
Hi All, I've the following table with a PK defined on an IDENTITY column (INSERT_SEQ): CREATE TABLE MYDATA ( MID NUMERIC(19,0) NOT NULL, MYVALUE FLOAT NOT NULL, TIMEKEY INTEGER NOT NULL, TIMEKEY_DTTM DATETIME NULL,
2
7157
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 the duplicate entries. If I try to delete any of the three records(they are identical) I get the message 'key column is insufficient or incorrect. Too many rows were affected by update'. I am trying to do this within Enterprise Mgr. Any...
2
4995
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3 fields (vehicleID, BattID, and ChgHrs). VehicleID and BattID are a TEXT datatype and ChrHrs are a number(long int.) datatype. Since records to be imported can have duplicate records of the composite key I need to clean all but one of the...
4
6166
by: KT | last post by:
Is there any one click solution that would do the trick? I would like to create a button, so the person who maintains the database can perform clean up work to delete duplicate records which contain same information in the ID field and the account number field once a week. Thanks in advance! KT
4
3606
by: smack | last post by:
I was able to get a query out showing all the duplicate records but know I need the duplicates to be sent to another table any ideas?
6
3591
by: hisham123 | last post by:
hi, i had more duplicate records in my table. i want to keep one rest i want to delete. can you give me sql server 2000 query
7
9878
by: AccessHunter | last post by:
I am using the following code to find and delete records in a table. The logic will go through each record and if a duplicate row is found will delete it. I ran this code and it worked the first time. Its not deleting the rows when I tried the second time. I debugged the code and its actually going through the delete step but the row is not getting deleted as it did the first time. Please help. Thanks in advance. Function...
6
5937
by: Dilip1983 | last post by:
Hi All, I want to delete duplicate records from a large table. There is one index(INDEX_U1) on 4 columns(col1,col2,col3,col4) which is in unusable state. First of all when i tried to rebuild index it showed error as unique key violation. So i want to delete duplicate records for col1,col2,col3,col4 combination. How can i delete the duplicate records from this large table?
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.