473,397 Members | 1,974 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,397 software developers and data experts.

deleting a duplicate record based on the most current date

hi - i'm trying to delete one of the duplicate records based on the
most current date.

here's the code for my access 2000 db. any help would be
appreciated!!! - thank you kindly

Sub DeleteDuplicateRecords()
' Deletes duplicates from the specified table, keeping the most
current received date record.
' No user confirmation is required.
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strSQL As String
Dim varBookmark As Variant
strTableName = "tlbtest1"
Set tdf = DBEngine(0)(0).TableDefs(strTableName)
strSQL = "SELECT tlbtest1.[num], tlbtest1.[Rcvddate]" _
& "FROM tlbtest1 " _
& "ORDER BY tlbtest1.[num], tlbtest1.[RcvdDate] DESC;"

'Set tdf = Nothing
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL)
Set rst2 = rst.Clone
rst.MoveNext
Do Until rst.EOF
varBookmark = rst.Bookmark
For Each fld In rst.Fields
If fld.Value <rst2.Fields(fld.Name).Value Then
GoTo NextRecord
End If
Next fld
rst.Delete
GoTo SkipBookmark
NextRecord:
rst2.Bookmark = varBookmark
SkipBookmark:
rst.MoveNext
Loop
End Sub

May 4 '07 #1
4 3529
On May 4, 4:29 pm, mcca0081 <mcca0...@gmail.comwrote:
hi - i'm trying to delete one of the duplicate records based on the
most current date.

here's the code for my access 2000 db. any help would be
appreciated!!! - thank you kindly

Sub DeleteDuplicateRecords()
' Deletes duplicates from the specified table, keeping the most
current received date record.
' No user confirmation is required.
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strSQL As String
Dim varBookmark As Variant
strTableName = "tlbtest1"
Set tdf = DBEngine(0)(0).TableDefs(strTableName)
strSQL = "SELECT tlbtest1.[num], tlbtest1.[Rcvddate]" _
& "FROM tlbtest1 " _
& "ORDER BY tlbtest1.[num], tlbtest1.[RcvdDate] DESC;"

'Set tdf = Nothing
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL)
Set rst2 = rst.Clone
rst.MoveNext
Do Until rst.EOF
varBookmark = rst.Bookmark
For Each fld In rst.Fields
If fld.Value <rst2.Fields(fld.Name).Value Then
GoTo NextRecord
End If
Next fld
rst.Delete
GoTo SkipBookmark
NextRecord:
rst2.Bookmark = varBookmark
SkipBookmark:
rst.MoveNext
Loop
End Sub
One of my favorite method is to use a list or combo box based on the
record set
Set one of the columns to the date field in your table
Then use the listindex method to move to the sorted listbox item and
extract the date in the date column. Use the extracted date in a SQL
statement of a simple delete query and presto, all records without the
date will be deleted. Of course if you have records with the same date
you might need to use the date/time format to ensure the very latest
updated duplicate record.
Question: instead of duplicating records why not overwrite the older
one at time of update?

May 6 '07 #2
On May 4, 3:29 pm, mcca0081 <mcca0...@gmail.comwrote:
hi - i'm trying to delete one of the duplicate records based on the
most current date.

here's the code for my access 2000 db. any help would be
appreciated!!! - thank you kindly

Sub DeleteDuplicateRecords()
' Deletes duplicates from the specified table, keeping the most
current received date record.
...
Set rst = CurrentDb.OpenRecordset(strSQL)
Set rst2 = rst.Clone
I was able to delete directly from the original table, but it involved
using the []. subquery syntax. It's a little easier to create a new
table with just the records you want. The recordset technique with a
clone that you used looked too gnarly for me to start from.

'-----Begin Module Code-----
Sub KeepUnique(strTableName As String, strDateField As String,
strIDField As String, strMakeTableName As String)
Dim MyDB As Database
Dim CountRS As Recordset
Dim strSQL As String
Dim boolExecute As Boolean
Dim tdf As TableDef
Dim fld As Field
Dim lngCount As Long
Dim strLastField As String

Set MyDB = CurrentDb
Set tdf = MyDB.TableDefs(strTableName)
boolExecute = False
'SELECT First(tblOrders.OID) AS OID FROM tblOrders WHERE OrderDate =
(SELECT Min(A.OrderDate) FROM tblOrders AS A WHERE A.OrderData =
tblOrders.OrderData AND A.Other = tblOrders.Other) GROUP BY OrderData,
Other;
strSQL = ""
If tdf.Fields.Count = 0 Then
MsgBox ("No fields in table " & strTableName)
Set tdf = Nothing
Set MyDB = Nothing
Exit Sub
End If
strSQL = "SELECT First(" & strTableName & "." & strIDField & ") AS " &
strIDField & " FROM " & strTableName
strSQL = strSQL & " WHERE " & strDateField & " = (SELECT Min(A." &
strDateField & ") FROM " & strTableName & " AS A WHERE "
For Each fld In tdf.Fields
If fld.Name <strIDField And fld.Name <strDateField Then ' And
fld.Name <any field not used to determine uniqueness
strSQL = strSQL & "A." & fld.Name & " = tblOrders." & fld.Name & "
AND "
End If
Next fld
strSQL = Left(strSQL, Len(strSQL) - 5)
strSQL = strSQL & ") GROUP BY "
For Each fld In tdf.Fields
If fld.Name <strIDField And fld.Name <strDateField Then ' And
fld.Name <any field not used to determine uniqueness
strSQL = strSQL & fld.Name & ", "
End If
Next fld
strSQL = Left(strSQL, Len(strSQL) - 2) & ";"
Set CountRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
If CountRS.RecordCount 0 Then
boolExecute = True
CountRS.MoveLast
lngCount = CountRS.RecordCount
End If
CountRS.Close
Set CountRS = Nothing
'SELECT * INTO tblKeep FROM tblOrders WHERE OID In (SELECT
First(A.OID) AS OID FROM tblOrders AS A WHERE A.OrderDate = (SELECT
Min(B.OrderDate) FROM tblOrders AS B WHERE B.OrderData = A.OrderData
AND B.Other = A.Other) GROUP BY A.OrderData, A.Other);
If boolExecute = True Then
'Place any unique records in strMakeTableName
'Always nuke the old table
If IsTable(strMakeTableName) Then
MyDB.TableDefs.Delete strMakeTableName
DoEvents
End If
strSQL = "SELECT * INTO " & strMakeTableName & " FROM " &
strTableName & " WHERE " & strIDField & " IN (SELECT First(A." &
strIDField & ") "
strSQL = strSQL & "AS " & strIDField & " FROM " & strTableName & "
AS A WHERE A." & strDateField & " = (SELECT Min(B." & strDateField &
") FROM " & strTableName & " AS B WHERE "
For Each fld In tdf.Fields
If fld.Name <strIDField And fld.Name <strDateField Then ' And
fld.Name <any field not used to determine uniqueness
strSQL = strSQL & "B." & fld.Name & " = A." & fld.Name & " AND "
End If
Next fld
strSQL = Left(strSQL, Len(strSQL) - 5)
strSQL = strSQL & ") GROUP BY "
For Each fld In tdf.Fields
If fld.Name <strIDField And fld.Name <strDateField Then ' And
fld.Name <any field not used to determine uniqueness
strSQL = strSQL & "A." & fld.Name & ", "
End If
Next fld
strSQL = Left(strSQL, Len(strSQL) - 2) & ");"
MyDB.Execute strSQL, dbFailOnError
If lngCount 1 Then
Do While MyDB.RecordsAffected = 0
DoEvents
Loop
End If
Else
MsgBox ("No duplicate records.")
End If
End Sub

Public Function IsTable(strTableName As String)
Dim MyDB As Database
Dim tdf As TableDef
Dim tdfs As TableDefs

IsTable = False
Set MyDB = CurrentDb
Set tdfs = MyDB.TableDefs
For Each tdf In tdfs
If tdf.Name = strTableName Then
IsTable = True
Exit For
End If
Next tdf
End Function
'-------End Module Code-----

Sample call:

Call KeepUnique("tblOrders", "OrderDate", "OID", "tblKeep")

The test I ran:

tblOrders
OID AutoNumber
OrderData Text
OrderDate Date/Time
Other Text
OID OrderData OrderDate Other
1 qa 4/5/2007 1
2 qb 4/5/2007 1
3 qc 4/6/2007 2
4 qc 4/6/2007 2
7 qb 4/8/2007 3
8 qb 4/7/2007 3
9 qb 4/6/2007 3

After calling the subroutine:

tblKeep
OID OrderData OrderDate Other
1 qa 4/5/2007 1
2 qb 4/5/2007 1
3 qc 4/6/2007 2
9 qb 4/6/2007 3

This is the only test I tried. You'll need to change from Min to Max
on the date field everywhere. You'll also need to undo any browser
wordwrap. I might still have the sample []. subquery SQL statement
around if the MakeTable query won't do. I hope this helps.

James A. Fortune
CD********@FortuneJames.com

May 7 '07 #3
On May 6, 10:24 pm, CDMAPos...@FortuneJames.com wrote:
I might still have the sample []. subquery SQL statement
around if the MakeTable query won't do. I hope this helps.
I found it. Make a backup first if you decide to run it. Maybe
there's a simpler way to keep the most recent duplicated record and to
delete the rest directly from the table using a single query.

DELETE tblOrders FROM tblOrders WHERE OID IN (SELECT tblOrders.OID
FROM tblOrders LEFT JOIN [SELECT OID FROM tblOrders WHERE OID IN
(SELECT First(A.OID) AS OID FROM tblOrders AS A WHERE A.OrderDate =
(SELECT Max(B.OrderDate) FROM tblOrders AS B WHERE B.OrderData =
A.OrderData AND B.Other = A.Other) GROUP BY A.OrderData, A.Other)]. AS
qryKeep ON tblOrders.OID = qryKeep.OID WHERE qryKeep.OID Is Null);

James A. Fortune
CD********@FortuneJames.com
May 9 '07 #4
On May 4, 4:29 pm, mcca0081 <mcca0...@gmail.comwrote:
hi - i'm trying to delete one of the duplicate records based on the
most current date.

here's the code for my access 2000 db. any help would be
appreciated!!! - thank you kindly

Sub DeleteDuplicateRecords()
' Deletes duplicates from the specified table, keeping the most
current received date record.
' No user confirmation is required.
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strSQL As String
Dim varBookmark As Variant
strTableName = "tlbtest1"
Set tdf = DBEngine(0)(0).TableDefs(strTableName)
strSQL = "SELECT tlbtest1.[num], tlbtest1.[Rcvddate]" _
& "FROM tlbtest1 " _
& "ORDER BY tlbtest1.[num], tlbtest1.[RcvdDate] DESC;"

'Set tdf = Nothing
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL)
Set rst2 = rst.Clone
rst.MoveNext
Do Until rst.EOF
varBookmark = rst.Bookmark
For Each fld In rst.Fields
If fld.Value <rst2.Fields(fld.Name).Value Then
GoTo NextRecord
End If
Next fld
rst.Delete
GoTo SkipBookmark
NextRecord:
rst2.Bookmark = varBookmark
SkipBookmark:
rst.MoveNext
Loop
End Sub
What determines if two records are 'duplicates'?
How do you deal with a set of 'matched' records where you have
multiple racords on the latest date?

May 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: xool | last post by:
Hi all, can anyone tell me how to delete just the current record if tempdate >= todaydate then Set newMail=Server.CreateObject("CDONTS.newMail") newMail.to = rs.fields("Email") newMail.From =...
0
by: Luke Airig | last post by:
I am trying to merge two xml files based on common date/time and then write out a tab-delimited xml file with the header record from one of the input files concatenated in front of the merged...
8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
3
by: Nathan Bloom | last post by:
Hi, I have a data entry form (access 2000) that also allows the user to add, update, and delete records from the form. The Delete action is carried out in an event procedure and has the...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
24
by: Frank Swarbrick | last post by:
We have a batch process that inserts large numbers (100,000 - 1,000,000) of records into a database each day. (DL/I database.) We're considering converting it to a DB2 table. Currently we have...
9
by: rjshrader | last post by:
I have a table (tblStatus) with three fields (CustomerID, StatusType and StatusDate). I use an unbound form with three text boxes to enter data into the table when a command button (cmdSave) is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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
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...

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.