Query between dates issue (inclusive)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AccessQuestion
    New Member
    • Jun 2010
    • 13

    #1

    Query between dates issue (inclusive)

    I am at my wits end. Why is this not simple? So I am trying to query records that fall between two dates, but the ending date is not showing up. So, if I want to see if there is data for today my stopping date has to be tomorrow. Here is my SQL:

    Code:
    SELECT Tasks.[Start Date], Tasks.[Parts Used]
    FROM Tasks
    WHERE (((Tasks.[Start Date]) Between Forms![Parts Used Report]![Start Date] And Forms![Parts Used Report]![End Date])) And ((Tasks.[Parts Used]) Is Not Null);
    I have tried to add a +1 to the end date, to no avail. I have Googled my butt off and haven't found anything to help. Anything you can help me with on this would be GREATLY appreciated. Thanks in advance.

    *edit*
    I have also tried doing a >= and <= and I keep getting an error with the <=. Thanks again.
  • gnawoncents
    New Member
    • May 2010
    • 214

    #2
    You were very close. Try this:

    Code:
    SELECT Tasks.[Start Date], Tasks.[Parts Used]
    FROM Tasks
    WHERE (((Tasks.[Start Date])>=[Forms]![Parts Used Report]![Start Date] And (Tasks.[Start Date])<[Forms]![Parts Used Report]![End Date]+1) AND ((Tasks.[Parts Used]) Is Not Null));

    Comment

    • OldBirdman
      Contributor
      • Mar 2007
      • 675

      #3
      If the start and end dates in the table contain a time component, then any time after midnight 0:00:00 will be greater than the date without a time entered in the textbox of the form, and therefore fail the BETWEEN ... AND ... test.
      Unless the DATEADD() function is used to add 1 day, that logic will fail also.
      Also, the WHERE clause needs to explicitly state that the text be treated as a date, so the final result should look like:
      Code:
      WHERE (Tasks.[Start Date] Between #4/1/2010# And #6/30/2010# and ....
      This is usually done in VBA by building an SQL string, and then running the query

      Comment

      • AccessQuestion
        New Member
        • Jun 2010
        • 13

        #4
        Originally posted by gnawoncents
        You were very close. Try this:

        Code:
        SELECT Tasks.[Start Date], Tasks.[Parts Used]
        FROM Tasks
        WHERE (((Tasks.[Start Date])>=[Forms]![Parts Used Report]![Start Date] And (Tasks.[Start Date])<[Forms]![Parts Used Report]![End Date]+1) AND ((Tasks.[Parts Used]) Is Not Null));
        Thank you so much gnawoncents!! The old position of the +1 mistake. It's working perfectly. Thanks again.

        Comment

        • gnawoncents
          New Member
          • May 2010
          • 214

          #5
          Glad to hear it's working for you!

          Comment

          Working...