Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 21st, 2004, 10:26 AM
Tish Tish is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 15 Tish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 38 sec
Reputation Power: 0
Smile Delete duplicate records based on date

Good morning:

I have a table containing assessment date field. I've created a find duplicates query to show me which records are duplicate, but need to come up with a way of automatically deleting records with the oldest assessment dates (i.e., if dates on records is 8/20/2003, 9/21/2004, 10/23/04, I want to delete the records containing 8/20/2003 and 9/21/2004). Any ideas?

Thanks,
Tish

Reply With Quote
  #2  
Old December 21st, 2004, 01:42 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hi Tish,

The following code will create a temporary delete query that can be run to delete the records found in your duplicate query based on the criteria you wanted.

Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Dim dteDate As Date

dteDate = DMax("[DateField]", "DuplicateQueryName")

strSQL = "Delete * FROM DuplicateQueryName " & _
"WHERE (DuplicateQueryName.DateField)< #" & dteDate & "#"

Set db = CurrentDb
With db
Set qdf = .CreateQueryDef("tmpQuery", strSQL)
DoCmd.OpenQuery "tmpQuery"
.QueryDefs.Delete "tmpQuery"
End With
db.Close
qdf.Close

Just replace with your query name and field name for the date field.

lwells

Reply With Quote
  #3  
Old December 21st, 2004, 04:34 PM
Tish Tish is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 15 Tish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by lwells
Hi Tish,

The following code will create a temporary delete query that can be run to delete the records found in your duplicate query based on the criteria you wanted.

Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Dim dteDate As Date

dteDate = DMax("[DateField]", "DuplicateQueryName")

strSQL = "Delete * FROM DuplicateQueryName " & _
"WHERE (DuplicateQueryName.DateField)< #" & dteDate & "#"

Set db = CurrentDb
With db
Set qdf = .CreateQueryDef("tmpQuery", strSQL)
DoCmd.OpenQuery "tmpQuery"
.QueryDefs.Delete "tmpQuery"
End With
db.Close
qdf.Close

Just replace with your query name and field name for the date field.

lwells

I've tried to enter the above code, but on runtime, I get a compile error on Dim db As Database. It reads "User-define type not defined." Are there any references that have to be added under "tools" in order for this to run? Thanks again for your help.

Reply With Quote
  #4  
Old December 21st, 2004, 05:29 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hi Tish,

My apology...add the reference to DAO

Dim db As DAO.Database

That should correct the error.
lwells

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Delete duplicate records based on date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT