473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete Duplicates with VBA

283 Contributor
Hello,

I am trying to write a code that will look through a table for duplicates and delete them. I currently have a code I found on an older post in bytes that works fine for deleting duplicates but I am trying to figure out a way i can modify it to delete duplicates based on a criteria.

I have been trying to change it so that if I have strField(1) that is Not Null and another strField(2) that Is Null and strField(1) is a duplicate then delete it.

Any help would be appreciated.

Thanks,

Slen

below is the code i am working with (orginally posted by NeoPa)
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 
  22.  
  23.  
Apr 25 '12 #1
11 7410
Rabbit
12,516 Recognized Expert Moderator MVP
Why do it through VBA instead of a delete query?
Apr 25 '12 #2
slenish
283 Contributor
Mostly because I have code built that is doing a few other things to the table before and after finding the duplicates.

I was trying to avoid the whole dup/append queries and then pull that information back in to the main table.

slen :D
Apr 25 '12 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Well, then you pretty much translate what you said into VBA code
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(.Fields(strField1) And IsNull(.Fields(strField2) Then
Apr 25 '12 #4
slenish
283 Contributor
Thanks Rabbit,

That was what I needed!

I am still finding that it is not quite deleting all the duplicates which I think its because there might be some that could be triplicates.

I will continue to play with it and see if i can figure it out. I apprecaite the help in getting me in the right direction.

Thanks again,

Slen :D
Apr 25 '12 #5
ADezii
8,834 Recognized Expert Expert
The following Demo, hard coded for the sake of simplicity, will Delete any Duplicates, Triplicates, etc. where Duplication is defined as [Field1] NOT being NULL and [Field2] Is NULL. I also posted sample Before and after Data.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3.  
  4. Set MyDB = CurrentDb
  5. Set rst = MyDB.OpenRecordset("Table1", dbOpenDynaset)
  6.  
  7. With rst
  8.   Do While Not .EOF
  9.     If Not IsNull(![Field1]) And IsNull(![Field2]) Then     'Check for Duplication
  10.       If DCount("*", "Table1", "[Field1] = '" & ![Field1] & "' AND [Field2] Is Null") > 1 Then
  11.         .Delete
  12.       End If
  13.     End If
  14.       .MoveNext
  15.   Loop
  16. End With
  17.  
  18. rst.Close
  19. Set rst = Nothing
Before Executing Code:
Expand|Select|Wrap|Line Numbers
  1. PK  Field1  Field2
  2. 1     A       1
  3. 2     B
  4. 3     C
  5. 4     D       4
  6. 5     E       5
  7. 6     f       6
  8. 7     G       7
  9. 8     H       8
  10. 9     B
  11. 10    C
  12. 11    E
  13. 12    f
  14. 13    G
  15. 14    t      44
  16. 15    M
  17. 16    B
  18. 17    B
  19. 18    C
  20. 19    D
  21. 20    p
After Code Execution:
Expand|Select|Wrap|Line Numbers
  1. PK  Field1  Field2
  2. 1     A       1
  3. 4     D       4
  4. 5     E       5
  5. 6     f       6
  6. 7     G       7
  7. 8     H       8
  8. 11    E
  9. 12    f
  10. 13    G
  11. 14    t      44
  12. 15    M
  13. 17    B
  14. 18    C
  15. 19    D
  16. 20    p
P.S. - Records with Primary Key Values of 2, 3, 9, 10, and 16 have been Deleted.
Apr 25 '12 #6
slenish
283 Contributor
Hi ADezii,

Once again you skills are the stuff of legend. That worked a lot better. Now I just have to figure out how i can change it some to go in to a function :D

Really appreciate your help!

Is there anyway i can change your response to the correct answer?

Slen
Apr 26 '12 #7
slenish
283 Contributor
An add on question for you,

How would i modify this to check for duplicates and then if there is one value in the duplicate that is different to delete it?

I tried something like this but i keep getting errors...
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3.  
  4. Set MyDB = CurrentDb
  5. Set rst = MyDB.OpenRecordset("tbl", dbOpenDynaset)
  6.  
  7. With rst
  8.   Do While Not .EOF
  9.      If Not IsNull(![strField1]) And IsNull(![strField2]) Then     'Check for Duplication
  10.       If DCount("*", "tbl", "[strField1] = '" & ![strField1] & "' AND [strField2] Is Null") > 1 And _
  11.         DLookup("*", "tbl", "[Code1]= 'No Error'") Then
  12.          .Delete
  13.       End If
  14.      'End If
  15.     End If
  16.       .MoveNext
  17.   Loop
  18. End With
  19.  
  20. rst.Close
  21. Set rst = Nothing
  22.  
Apr 26 '12 #8
ADezii
8,834 Recognized Expert Expert
if there is one value in the duplicate that is different to delete it?
Kindly explain, not sure what you mean...
Apr 26 '12 #9
slenish
283 Contributor
Going off of the code you did if I wanted to make adjustments to it so if I have a duplicate record the identifier would be strField1 (an ID #), in strField2 one record is Null and the other is Not Null and in a strField3 I could have two different values, for example code1 and code2. If I wanted to delete only the record where strField1 is a duplicate, strField2 is Null and strField3 = code2, how could I adjust this??

I can get this to run but it is not deleting anything...any ideas?

Expand|Select|Wrap|Line Numbers
  1. With rst
  2.   Do While Not .EOF
  3.      If Not IsNull(![ID]) And IsNull(![Amt]) Then    'Check for Duplication
  4.       If DCount("*", "tblName", "[ID] = '" & ![ID] & "' >1 And [Amt] Is Null And [ErrCode] = '" & strText & "'") Then
  5.          .Delete
  6.       End If
  7.      'End If
  8.     End If
  9.       .MoveNext
  10.   Loop
  11. End With
  12.  
  13. rst.Close
  14. Set rst = Nothing
  15.  
Apr 26 '12 #10

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

Similar topics

22
35460
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an easy but somehow contrived solution would be >>> a = >>> d = {}.fromkeys(a) >>> b = d.keys()
3
2406
by: Alexander Anderson | last post by:
I have a DELETE statement that deletes duplicate data from a table. It takes a long time to execute, so I thought I'd seek advice here. The structure of the table is little funny. The following is NOT the table, but the representation of the data in the table: +-----------+ | a | b | +-----+-----+ | 123 | 234 | | 345 | 456 |
0
1053
by: GlobalBruce | last post by:
The GAC on my development computer has several assemblies which are duplicated. For instance, the System assembly is present as two different native images as well as the non-native version. The native images differ by date/time stamp -- although they are only different by 3 days during Feb/2004. Many of the System assemblies have similar...
3
2329
by: EoRaptor013 | last post by:
I'm having trouble figuring out how to delete some _almost_ duplicate records in a look-up table. Here's the table: CREATE TABLE ( (16) NOT NULL , (2) NOT NULL , (20) NULL , (50) NULL , NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
1
3526
by: Smythe32 | last post by:
If anyone could help, I would appreciate it. I have a table as listed below. I need to check for duplicates by the OrderItem field and if there are duplicates, it then needs to keep the OrderItem with the highest interval and discard all other duplicates for that OrderItem number. If the OrderItem and the Interval are exactly the same, it...
4
13956
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
3
2352
allingame
by: allingame | last post by:
Need help with append and delete duplicates I have tables namely 1)emp, 2)time and 3)payroll TABLE emp ssn text U]PK name text
1
4092
watertraveller
by: watertraveller | last post by:
Hi all. My ultimate goal is to return two columns, where no single value appears anywhere twice. This means that not only do I want to check that nothing from column A appears in column B and vice-versa, but I also don't want the same value appearing twice in A and twice in B. So far I have: --Diff the columns INSERT INTO @Table SELECT...
4
4756
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have: #include <iostream> using namespace std; class NumberList {
1
2089
by: KimmyG | last post by:
I'm just starting to use SQL and am much more experienced in Access. Here is what I do in Access Copy a table and rename the new table "copytable" also select structure only. Open "copytable" and highlight columns where I want to delete duplicates if there are more than one. Put a primary key on Outreach name and days.
0
7915
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...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
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. ...
0
8339
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...
0
6619
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...
1
5712
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...
0
5392
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...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.