Results 1 to 5 of 5
  1. #1
    zeusads is offline Novice
    Windows 10 Access 2007
    Join Date
    Feb 2017
    Posts
    4

    Help With a Query - Datepart

    Hello Everyone,

    I'm just trying to create this query, I think I'm almost there but can't find what is wrong or what to do.

    Imagine a table with these fields:

    Animal_name
    Arrival_Date
    Death_Date

    What I want is to create a query to grab how many animals were alive in a specific year.
    For example, let's say it is the year of 2000.

    It needs to exclude all animals that arrived after this year.
    It needs to exclude all animals that died until the end of the year 2000.

    What I did was create a Field on the Query like this: ArrivalYear: DatePart("yyyy";[Arrival_Date])
    And another one like this: DeathYear: DatePart("yyyy";[Death_Date])



    With these two I Can solve the 2 first problems. But If an animal is still alive he will not be on the list, and it needs to.
    The problem is when you search for some year and in that year you have an animal that has no Death_Date it will not be shown on the results.

    If anyone knows a way to do that I'll be very grateful.

    Thanks a Lot

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    What criteria do you use in the calculated "DeathYear" column
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    zeusads is offline Novice
    Windows 10 Access 2007
    Join Date
    Feb 2017
    Posts
    4
    Quote Originally Posted by Bob Fitz View Post
    What criteria do you use in the calculated "DeathYear" column
    <=[Type the Year] in ArrivalYear and DeathYear

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Try
    Code:
    SELECT * FROM tblYourTable WHERE Arrival_Date = DatePart("yyyy";[Arrival_Date]) As ArrivalYear AND Death_Year = DatePart("yyyy";[Death_Date]) As Death_Year
    UNION
    SELECT * FROM tblYourTable WHERE Arrival_Date = DatePart("yyyy";[Arrival_Date]) AND [Death_Date]) Is Null;

    You might get prompted for two Arrival Dates though. You could work around that by using a form, but someone else may have a better approach.

    Last edited by Micron; 04-04-2017 at 09:08 AM. Reason: added code tags
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    In query window in criteria on the same row so it does an AND for those 2 fields, would it be:

    ArrivalDate: < #1/1/2001#
    DeathDate: Null or >#12/31/2000#

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

Similar Threads

  1. DATEPART not working in VBA recordset
    By merebag in forum Access
    Replies: 4
    Last Post: 09-02-2016, 12:34 PM
  2. Replies: 2
    Last Post: 08-04-2016, 03:36 PM
  3. Replies: 10
    Last Post: 01-07-2014, 04:03 PM
  4. datepart in sql string
    By Dannasoft in forum Queries
    Replies: 1
    Last Post: 12-19-2012, 04:52 PM
  5. iff(datepart..
    By pranvera in forum Access
    Replies: 4
    Last Post: 11-19-2010, 11:38 AM

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