Results 1 to 10 of 10
  1. #1
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32

    Deleting Duplicate Records

    Trying to learn a new trick here. I have a table littered with duplicate records. I'm only concerned with two data fields: Receipt Number and Receipt Date. I want to keep only the record of the latest Receipt Date.

    How do I run a delete query and single out only the older receipt dates for deletion?

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    First, do all testing in a junk version of your database, until you're certain the code will work as desired.
    Second, try this delete query. Replace the "MyTable" with your table name, and make sure the field names in the square braces are correct.
    Code:
    Delete FROM MyTable AS T1
    WHERE T1.[Receipt Date] <
       (SELECT MAX(T2.[Receipt Date])
       FROM MyTable AS T2 
       Where T2.[Receipt Number] = T1.[Receipt Number]);
    The subselect gets the highest receipt date for each receipt number, and the outer deletes any record that is less than that.

  3. #3
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Here is a simple way to do it. Look at this video tutorial

    http://www.datapigtechnologies.com/f...teproblem.html

  4. #4
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    Thanks to BOTH Dal Jeanis and alansidman! I actually used both of your solutions. Dal, yours took care of a good chunk of the dups -- all except those where the dates were identical (a few hundred). Alan, yours cleaned up the rest of the job when I moved the records to a new table. You BOTH saved me a ton of work. Much, much appreciated!!

  5. #5
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    Dal Jeanis,

    I tried this code on a database in my work PC -- which also has MS Access 2010, but is still running XP as its OS. It wouldn't run through the query -- just stalled. Any ideas?

  6. #6
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Shouldn't be the OS. OS should be transparent to the Jet database engine. You'll have to play with a backup/test/junk copy of the database.
    1) Try again, Make sure that you haven't turned off error messages
    2) Check to make sure there aren't any associated records that might have to be deleted first.

  7. #7
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    Hi Again Dal,

    Can't understand it. The query worked great the first time. Before attempting to run in on a different table, I checked error messages and associated records. Checked and double checked table and field names - same PC, same database. Checked the data to make sure there were no erred or blank records -- or records with blank data in the applicable fields. Now, it just sits there with the "Run Query" progress bar blank at the bottom. At first, I figured since the table has over 51K of records, that it might take a while to get through. But I should at least be showing some progress in the indicator, right?

  8. #8
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Is it possible that anyone else is connected to the table and has a lock on a record, any record?

  9. #9
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    Nope. It's a stand-alone app and the table I tried it on is a test copy.

  10. #10
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Then I have no idea what the holdup is.
    The next method is to create a copy with only the records you want, then delete the whole table and copy it back:
    Code:
    INSERT INTO MyTableCopy
    SELECT * FROM MyTable AS T1
    WHERE T1.[Receipt Date] =
       (SELECT MAX(T2.[Receipt Date])
       FROM MyTable AS T2 
       Where T2.[Receipt Number] = T1.[Receipt Number]);
    Code:
    DELETE FROM MyTable;
    Code:
    INSERT INTO MyTable
    SELECT * FROM MyTableCopy;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Deleting records on a form using VBA
    By lawdy in forum Programming
    Replies: 5
    Last Post: 05-06-2013, 06:06 PM
  2. Merging and deleting duplicate records
    By Rider in forum Access
    Replies: 1
    Last Post: 01-06-2012, 01:44 PM
  3. deleting records
    By radicalrik in forum Queries
    Replies: 2
    Last Post: 07-14-2010, 03:10 PM
  4. deleting records off a subform
    By jamin14 in forum Programming
    Replies: 10
    Last Post: 04-22-2010, 08:47 PM
  5. Problem Deleting Records with ADO
    By bdicasa in forum Programming
    Replies: 0
    Last Post: 08-21-2008, 09:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums