Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Salty Mariner is online now Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69

    Between Dates with Null values

    I have an application that needs to determine crew members who were on between certain dates. I have two data fields named [SignOn] and [SignOff]. I have a criteria field in my query that gets its date from a textbox on a form txtReportDate. My WHERE Statement is as follows;



    Code:
    WHERE ((([Forms]![frmVesselReport]![txtReportDate]) Between [SignOn] And [SignOff]))
    This works great EXCEPT when I have to deal with a Null Sign Off value. I have tried Using OR Is Null with [SignOff] But then this disregards my criteria for SignOn and just returns all records.

    What am I missing here? I have racked my brains out trying to make this work. I simply want to return all records BETWEEN [SignOn] and [SignOff] and to also include those with Null values for [SignOff] if they exist. Nothing I have tried has returned the desired results.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Try

    WHERE ((([Forms]![frmVesselReport]![txtReportDate]) Between [SignOn] And nz([SignOff],[Forms]![frmVesselReport]![txtReportDate])))

  3. #3
    Salty Mariner is online now Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    Thank you for the response but the query still returns all of those with a null [SignOff] date even if I go back a few months with my criteria a few months before they had an active [SignOn] date.

    Iam wondering if I might have to do something weird like create two separate queries, one for each condition and then conditionally exclude the unwanted values. I've never done anything like this before but it seems like the next logical step if this doesn't work would be to break this into smaller pieces and then try to combine them. I've even tried things suggested by Microsoft Co-Pilot AI and none have panned out so far. I shouldn't be this hard.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I was thinking more like:

    Between [SignOn] And Nz([SignOff], #12/31/2029#)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    but the query still returns all of those with a null [SignOff] date
    you said

    I simply want to return all records BETWEEN [SignOn] and [SignOff] and to also include those with Null values for [SignOff] if they exist.
    So what are you expecting?

  6. #6
    Salty Mariner is online now Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    pbaldy,

    Thank you! THAT was the ticket.

    I put far too many hours into frying my brain cells trying to figure out what turned out to be such a simple and elegant solution.

  7. #7
    Salty Mariner is online now Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    CJ_London,

    pbaldy refined this to where it works now. Thank you for your time and response.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Salty Mariner View Post
    pbaldy,

    Thank you! THAT was the ticket.

    I put far too many hours into frying my brain cells trying to figure out what turned out to be such a simple and elegant solution.
    Happy to help, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    TBH I cannot see why CJ_London's would not work?

    All it is doing is replacing a null date with the criteria date. Pbaldy's solution does exactly the same thing, just with a hardcoded date?

    If you are still using this program in 2030, the latter will fail?

    However the logic is the same?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    on reflection, both methods are not right as missing the date delimiters - and depending on where the OP is, reformatting the date to the SQL or US standard

    WHERE "#" & [Forms]![frmVesselReport]![txtReportDate] & "#" Between [SignOn] And nz([SignOff],"#" & [Forms]![frmVesselReport]![txtReportDate] & "#")

    or if date requires reformatting

    WHERE "#" & format([Forms]![frmVesselReport]![txtReportDate],"yyyy-mm-dd") & "#" Between [SignOn] And nz([SignOff],"#" & format([Forms]![frmVesselReport]![txtReportDate],"yyyy-mm-dd") & "#")

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Quote Originally Posted by CJ_London View Post
    on reflection, both methods are not right as missing the date delimiters - and depending on where the OP is, reformatting the date to the SQL or US standard

    WHERE "#" & [Forms]![frmVesselReport]![txtReportDate] & "#" Between [SignOn] And nz([SignOff],"#" & [Forms]![frmVesselReport]![txtReportDate] & "#")

    or if date requires reformatting

    WHERE "#" & format([Forms]![frmVesselReport]![txtReportDate],"yyyy-mm-dd") & "#" Between [SignOn] And nz([SignOff],"#" & format([Forms]![frmVesselReport]![txtReportDate],"yyyy-mm-dd") & "#")
    I thought that was only for Dates as strings?

    Code:
    SELECT tblDaily.*, tblDaily.DailyDate
    FROM tblDaily
    WHERE (((tblDaily.DailyDate) Between [Forms]![frmDailyEntry]![txtDate] And Date()));
    works fine?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I thought that was only for Dates as strings?


    which is what a textbox control datatype is.

    But if it works, it works





  13. #13
    Salty Mariner is online now Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    Quote Originally Posted by Welshgasman View Post
    TBH I cannot see why CJ_London's would not work?

    All it is doing is replacing a null date with the criteria date. Pbaldy's solution does exactly the same thing, just with a hardcoded date?

    If you are still using this program in 2030, the latter will fail?

    However the logic is the same?
    I agree the Logic seems to be the same, and I don't understand why either. But with some of this stuff logic doesn't always seem to apply. Many of the suggested examples I found through searches Seemed like the logic was sound but somehow they also failed. I'm guessing this where some of you guys who have been at this longer than others have an edge. At some point you had to struggle and learn things the hard way too which in turn influenced how you choose to approach things. I'm guessing that the hard coded end date can pushed further into the future to stave off obsolescence. In any event I appreciate your concern and assistance. I usually try to figure things out myself but sometimes I just get stuck.

    Thank you again.

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I'm guessing that the hard coded end date can pushed further into the future to stave off obsolescence.
    You could also use something like
    Code:
    Between [SignOn] And Nz([SignOff], DateAdd("y",50,date))
    which will never expire.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Welshgasman View Post
    All it is doing is replacing a null date with the criteria date. Pbaldy's solution does exactly the same thing, just with a hardcoded date?
    No, I replace the null date with a date in the future. Comparing the criteria date to itself will result in all records being returned.

    Moke's suggestion is a good one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-07-2019, 10:46 AM
  2. Calculating Work Days, help with Null Values for dates
    By rdougherty in forum Programming
    Replies: 9
    Last Post: 08-21-2018, 02:43 PM
  3. Replies: 2
    Last Post: 04-17-2017, 01:40 PM
  4. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  5. Replies: 2
    Last Post: 08-01-2011, 09:30 AM

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