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. - Public Function DeleteDuplicate()
-
Dim db As DAO.Database
-
Dim qdf As QueryDef
-
Dim strSQL As String
-
Dim strEmail As String
-
-
dteEmail = DMax("[email]", "tbl_email")
-
-
strSQL = "Delete * FROM tbl_email" & _
-
"WHERE (tbl_email.email)> strEmail"
-
-
Set db = CurrentDb
-
With db
-
Set qdf = .CreateQueryDef("tmpQuery", strSQL)
-
DoCmd.OpenQuery "tmpQuery"
-
.QueryDefs.Delete "tmpQuery"
-
End With
-
db.Close
-
qdf.Close
-
End Function
Any assistance is greatly appreciated.
16 16187
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
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?
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
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.
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.
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>
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.
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. -
-
Public Function DeleteDuplicate()
-
Dim db As DAO.Database
-
Dim rs As RecordSet
-
Dim strEmail As String
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("tbl_email")
-
-
StartFile:
-
-
rs.MoveFirst
-
Do Until rs!temp = False
-
If not rs.EOF then
-
rs.MoveNext
-
Else
-
GoTo EndFile
-
End If
-
Loop
-
-
strEmail = rs!email
-
rs!temp = True
-
rs.MoveNext
-
-
Do Until rs.EOF
-
If rs!email = strEmail Then
-
rs.Delete
-
End If
-
rs.MoveNext
-
Loop
-
-
GoTo StartFile
-
-
EndFile:
-
-
rs.close
-
set rs = Nothing
-
set db = Nothing
-
-
End Function
-
-
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. - 'DelDups Removes duplicate records in strTable matched on strField
-
Public Sub DelDups(strTable As String, strField As String)
-
Dim strSQL As String, varLastVal As Variant
-
-
'Recordset must be full table but sorted by the field we're checking
-
strSQL = "SELECT * FROM [" & strTable & "] ORDER BY [" & strField & "]"
-
With CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
-
varLastVal = Null
-
'For each record, check against previous value in strField
-
'If same then this is a duplicate so can be removed
-
Do Until .EOF
-
If .Fields(strField) = varLastVal Then
-
Call .Delete
-
Else
-
varLastVal = .Fields(strField)
-
End If
-
Call .MoveNext
-
Loop
-
'Ending the 'With' releases the Recordset
-
End With
-
End Sub
Hope it helps.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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,
|
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...
|
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...
|
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
| |
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?
|
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
|
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...
|
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?
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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
| |
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...
| |