473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Find and delete duplicate records from a table

77 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. Function DeleteDuplicates_Click()
  2. On Error Resume Next
  3.  
  4.  Dim db As Database, rst As Recordset
  5.  Dim strDupName As String, strSaveName As String
  6.  Dim strSQL As String
  7.  strSQL = "SELECT [JOBNUM], [trnd] FROM Tableqryduplicates"
  8.  
  9.  Set db = CurrentDb()
  10.  Set rst = db.openrecordset(strSQL, dbOpenSnapshot)
  11.  
  12.  
  13.  If rst.BOF And rst.EOF Then
  14.    MsgBox "No records to process"
  15.  Else
  16.    rst.MoveFirst
  17.    Do Until rst.EOF
  18.      strDupName = rst.Fields("JOBNUM") & rst.Fields("trnd")
  19.  
  20.      If strDupName = strSaveName Then
  21.        rst.Delete
  22.      Else
  23.        strSaveName = rst.Fields("JOBNUM") & rst.Fields("trnd")
  24.      End If
  25.      rst.MoveNext
  26.    Loop
  27.  
  28.    rst.Close
  29.    Set rst = Nothing
  30.    Set db = Nothing
  31.  End If
  32.  
  33.  
  34. End Function
Nov 15 '07 #1
7 9842
Megalog
378 Recognized Expert Contributor
Try adding a rst.edit or rst.delete before any changes, and an rst.update afterwards (before the movenext/loop)? I do pretty much the same thing, except with adding rows, and it needs an addnew/update to stick. I'm assuming it's the same with delete.

And maybe specify your recordset as DAO like:
"Dim db As Database, rst As DAO.Recordset"
Nov 15 '07 #2
NeoPa
32,556 Recognized Expert Moderator MVP
Nice ideas.
I checked out the .Edit & .AddNew and neither is required for a .Delete.
Maybe the DAO.Recordset will help. Explicitly dimming it as DAO would force it to use the methods we are expecting.

AccessHunter, can you let us know if any of this helps at all?
Nov 15 '07 #3
AccessHunter
77 New Member
I used dbOpenDynaset instead of dbOpenSnapshot and it worked.

To me it looked like when I ran this the first time successfully with dbOpenSnapshot, it might have taken a snapshot of the resultant data (after the rows got deleted) and then when I tried 2nd time (with dbOpenSnapshot again) it was looking at that same data !!..not sure about that, its just my assumption. Correct me if I am wrong.

Thanks
Nov 16 '07 #4
NeoPa
32,556 Recognized Expert Moderator MVP
I can't explain why it appears to have worked before. I wouldn't expect it to allow any amendments to a snapshot query. Only a dynamic query should work with deletions as far as I understand.
Nov 16 '07 #5
AccessHunter
77 New Member
Here is my new dilemma, please help me as I am new to VB.
I look at [job nbr], [seq nbr] and [trnd] to detemine if 2 rows are duplicates as per the code below. But I only want to delete a row based on the following logic.

If strDupName = strSaveName Then,

If ptyp(associated with strSaveName) or ptyp(associated with
strDupName) is not Null
If ptyp(associated with strSaveName) is Null,
delete that row,
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row.
Else, if ptyp(associated with strDupName) is Null,
delete that row
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row
End If
End If

If [p-lnam]+[p-fnam](associated with strSaveName) or [p-lnam]+[p-fnam]
(associated with strDupName) is not Null
If [p-lnam]+[p-fnam](associated with strSaveName) is Null
delete that row
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row.
Else, if [p-lnam]+[p-fnam] (associated with strDupName) is Null,
delete that row
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row
End If
End If


If [agency-name](associated with strSaveName) or [agency-name](associated
with strDupName) is not Null
If [agency-name](associated with strSaveName) is Null
delete that row
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row.
Else, if [agency-name](associated with strDupName) is Null,
delete that row
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") &
rst.Fields("trnd")
Get next row
End If
End If

-------------------------------------------------------------------------------------------------------------------

This is the code that works now,


Function DeleteDuplicates_Click()
On Error Resume Next

Dim db As Database, rst As Recordset
Dim strDupName As String, strSaveName As String
Dim strSQL As String
strSQL = "SELECT [job nbr], [seq nbr], [trnd], [ptyp], [p-lnam], [p-fnam], [agency-name]FROM Tableqryduplicates"

Set db = CurrentDb()
Set rst = db.openrecordset(strSQL, dbOpenDynaset)

If rst.BOF And rst.EOF Then
MsgBox "No records to process"
Else
rst.MoveFirst
Do Until rst.EOF

strDupName = rst.Fields("case nbr") & rst.Fields("seq nbr") & rst.Fields("trnd")

If strDupName = strSaveName Then
(This is where I want the logic that I just explained above)
rst.Delete
Else
strSaveName = rst.Fields("case nbr") & rst.Fields("seq nbr") & rst.Fields("trnd")

End If

rst.MoveNext
Loop

rst.Close
Set rst = Nothing
Set db = Nothing
End If

End Function
-----------------------------------------------------------------------------------------------------------------

Thanks a lot.
Nov 16 '07 #6
Relui
1 New Member
I use this (test.zip unpack and check module). Of course is only a model. From this you can go and develop your solution.
I don't say is the best, a very fancy or the greatest cool solution ... but it works.
I didn't put any comments about author rights but if you mention my name in your solution I realy don't mind ...
Attached Files
File Type: zip Test.zip (33.5 KB, 155 views)
May 30 '16 #7
NeoPa
32,556 Recognized Expert Moderator MVP
Hi.

Just so you know, AccessHunter hasn't been on the site for about seven years now.

There's no issue with posting suggestions or solutions on such an old thread, but you should be aware the original participants are unlikely to benefit from it.

-Adrian (Access MVP and Site Moderator).
May 30 '16 #8

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

Similar topics

1
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 ...
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...
2
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...
7
by: jmstur2 | last post by:
I have a table with what I consider duplicate records. Data in all columns are duplicate except for the date column, meaning that duplicate data was entered on different dates and those dates were...
2
by: farouqdin | last post by:
Hi all i have code which loops through table and deletes the duplicate records. This code does it for one table. How do i change it so it goes through several tables? On Error Resume Next Dim...
4
by: ramdil | last post by:
Hi All I have table and it have around 90000 records.Its primary key is autonumber field and it has also have date column and name, then some other columns Now i have problem with the table,as my...
6
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...
1
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...
1
by: DontTell | last post by:
Hi - I am trying to delete duplicate records from a single table. The table has multilple records of duplicate purchase orders. In these instances I need to delete those records that have the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.