+ Reply to Thread
Results 1 to 15 of 15

Stipulate criteria for Delete query

  1. #1
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Stipulate criteria for Delete query

    My Access knowledge is very limited, but I have set up a one table database (with a dual key of NAME & DATE) to use as a storage facility for Excel. I'd like to be able to do a Delete Query for all records for any horse that had its last race more than 3 years ago (Say 1100 days). So in the following example the 3 records for both Suavito & Scandiva would be expunged based on the date in red.

    From research it seems I have to do a Select Query then change it to a Delete Query, but I've got no idea how to phrase the criteria to achieve the result I want.

    Any help appreciated.

    NAME DATE
    Zevada 13/07/2014
    Zevada 25/04/2013
    Zevada 10/01/2011

    Suavito 10/09/2013
    Suavito 1/09/2013
    Suavito 15/07/2013

    Stipulate 8/03/2015
    Stipulate 9/11/2014
    Stipulate 13/10/2012

    Scandiva 7/09/2013
    Scandiva 18/02/2013
    Scandiva 17/10/2012
    Steve W.
    Vba is my hobby, racing the means.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Stipulate criteria for Delete query

    Hi swallis,

    You need to build a query that will only show "last race more than 1100 days ago". Read about the DateDiff function at:
    https://www.techonthenet.com/access/...e/datediff.php

    Then create a query using the DateDiff for one of your columns in the query builder. After it is selecting those old races, change the query to a Delete Query and run it. All those old records will then be deleted.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Thanks Marvin. If I've read that right, it will help, but I can't see how that selects the most recent date for each horse?

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    As Marvin has suggested, here is the SQL statement needed to delete records greater than 1100 days old.
    PHP Code: 
    DELETE Table1.Name1Table1.Date1DateDiff("d",[Date1],Date()) AS Diff
    FROM Table1
    WHERE 
    (((DateDiff("d",[Date1],Date()))>1100)); 
    BTW: The words Name and Date are reserved words in Access and should never be used as a field name. You can see that in my SQL statement I have changed the field names. Here is a link to reserved Access terms.

    http://allenbrowne.com/AppIssueBadWord.html
    Last edited by alansidman; 09-22-2016 at 08:56 AM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Thanks for the response, Alan. Your solution is the same as Marvin's - it removes all older records for all horses. If you look at the table in my original post, what I'm trying to achieve is deletion of all 3 starts for Suavito and Scandiva, but keep all 3 starts for the other two, as they have had starts Less than 1100 days ago.

    I looked at your link to reserved names and it's pretty scary for a dabbler. Suppose I'll have to change the name of my table - FORM

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    I don't understand your request. You have stated twice that you want to delete any records older than 1100 days. Both Marvin's and my solution do just that. Any thing older than 1100 days is deleted. Are you only interested in particular horses with records older than 1100 days? If that is is the case then you will need to stipulate in your query to only delete particular horses.

  7. #7
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Yes. I'm looking to winnow out all retired/dead horses. Get rid of every record for them, but keep every record for any horse that is still likely to race, including their records which are more than 1100 days ago. The query needs to look at the most recent date raced for each horse and if it's more than 1100 days ago, delete all records for that horse. If the most recent date raced is less than 1100 days ago, I want to keep all records for that horse - including any which are more than 1100 days ago. That is what I don't know how to do and am asking help for.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    Cannot post SQL statement directly. Here is a sample db with the layouts for determining which horses have not raced in 1100 days.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Feeling frustrated. I've downloaded 2 different Accdb viewers and still can't see your database - and I did unzip. Could you possibly upload it as an Mdb file please Alan?

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    ok. Will do.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Thanks Alan. Copied your Sql across and ran both queries and it selects the right horses to delete, but only lists the names and last date. How do I use that to delete all fields and all records for each horse?

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    You will need to run a delete query against the original table.

    PHP Code: 
    DELETE Table1.HorseName
    FROM Table1
    WHERE 
    (((Table1.HorseNameIn (Select [HorseNamefrom [Query2]))); 

  13. #13
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Fantastic! Thanks for all your help Alan. Works perfectly.

  14. #14
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Stipulate criteria for Delete query

    Glad to have been of help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks. It is customary to give feedback to those that have helped you by clicking on the asterisk (Add Reputation) at the bottom of their post.

  15. #15
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Stipulate criteria for Delete query

    Did the rep. Forgot the Solved. Now done.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] MS SQL Query conditions, multiple criteria within criteria
    By Speshul in forum Access Tables & Databases
    Replies: 1
    Last Post: 07-29-2014, 12:16 PM
  2. [SOLVED] how to delete delete rows with criteria and keeping formula
    By Bremmah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2013, 07:09 AM
  3. Query criteria based of other query data
    By jik_ff in forum Access Tables & Databases
    Replies: 6
    Last Post: 03-12-2012, 05:26 PM
  4. trying to delete a query
    By mrayray09 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2009, 02:48 PM
  5. Delete multiple columns by column name no criteria need, just delete them
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2009, 10:40 AM
  6. delete query
    By JT in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2006, 01:00 PM
  7. Replies: 1
    Last Post: 03-08-2006, 06:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1