Results 1 to 8 of 8
  1. #1
    tdsutter is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Location
    Wichita, KS
    Posts
    3

    Query based on operator defined YEAR from an existing date

    I have a query that lists various attributes of a group of individuals. One of the attributes is the date of death. For most of the records, that field is null. However, if the individual has died, the operator inserts the date of death in the form of mm/dd/yyyy. Now, I want to create a query based that the aforementioned query that, when executed, asks the operator for a year (What year to report?), then locates only the records with a date of death in the requested year. I've been working in the second query using the design view. I know how to ask the question. I don't know how to compare the operator's response to the year of the date of death.

    Looking forward to suggestions. Thanks.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Create a calculated field in your query that returns the year from the date of death, i.e.
    Code:
    YearOfDeath: Year(Nz([DateOfDeath],DateValue("12/31/2099")))
    Then, you can make this a parameter query by by placing this under the Criteria row of this new calculated field:
    [Enter Year of Death]

    Then, anytime you run the query, it will prompt you to enter the year of death, and return all the appropriate records.

  3. #3
    tdsutter is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Location
    Wichita, KS
    Posts
    3
    Thanks. Works like a charm. BTW, why did you choose 12/31/2099 as the argument for DateValue?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,603
    Just pick any date far enough into future that it will always be greater than any value in the field. 2099 gives the database a life of 86 years before that expression would have to be modified. This works as well:

    YearOfDeath: Year(Nz([DateOfDeath], #12/31/2099#))

    Actually, with a date of death field, next year is probably far enough - DateAdd("yyyy",1,Date()) - never have to change the expression, is perpetual.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    June - using tomorrow's date is highly inadviseable. The report might get printed, you know, and the operator might ask for this year.

    DateAdd 30 years sounds good to me.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,603
    Sorry, I just realized that and editing to say 'next year' and fix calc. If they filtered on the entire date value, tomorrow should be far enough.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Thanks. Works like a charm. BTW, why did you choose 12/31/2099 as the argument for DateValue?
    I just chose some random date way out in the future in the event that the Date of Death is not populated, because that will probably return errors.
    Note the usefulness of the NZ function. Basically, you use it to say what you want to return in the event of a Null value.

  8. #8
    tdsutter is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Location
    Wichita, KS
    Posts
    3
    WOW! Thanks everybody for all the help. Much appreciated.

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

Similar Threads

  1. Group by year where date in a query
    By Ruegen in forum Queries
    Replies: 1
    Last Post: 09-16-2013, 11:09 PM
  2. Update Query based on existing date
    By axdxnco in forum Queries
    Replies: 1
    Last Post: 06-12-2013, 02:15 PM
  3. Replies: 4
    Last Post: 01-09-2013, 11:16 AM
  4. Replies: 4
    Last Post: 01-10-2012, 06:26 PM
  5. Replies: 0
    Last Post: 03-25-2011, 02:37 PM

Tags for this Thread

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