Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12

    Query supposed to return values where a date is between two dates but not doing it!

    Hi everyone



    The below code returns the records that have StartDate<reportdate but not those have EndDate>reportdate. It's like it ignores the second part of the WHERE.

    SELECT students_rep.[lname], students_rep.[fname], students_rep.agm, [tblStudDiag].[diagnosis].Value, [tblStudDiag].[StartDate], [forms]![dailyreportdate]![rdate] AS reportdate, [tblStudDiag].duration, dateadd("d",[duration]-1,[StartDate]) AS EndDate, students_rep.nd_dsls & " " & students_rep.[taksi] AS Class
    FROM students_rep INNER JOIN tblStudDiag ON students_rep.agm=[tblStudDiag].[student]
    WHERE ((([tblStudDiag].[StartDate])<=forms!dailyreportdate!rdate) and ((dateadd("d",[tblStudDiag.duration]-1,[tblStudDiag.startdate]))>=forms!dailyreportdate!rdate));

    "agm" is the primary key of the query students_rep

    Click image for larger version. 

Name:	Screenshot 2023-11-17 153814.jpg 
Views:	31 
Size:	157.4 KB 
ID:	51054


    Any smart ideas??

    Many thanks!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Access always puts in too many brackets for my liking.
    I would exect the WHERE clause to be enclosed by brackets though, if only to make it obvious to me?
    So I reckon you are missing a closing bracket at the end of the SQL ?

    Perhaps try

    Code:
    SELECT students_rep.[lname], students_rep.[fname], students_rep.agm, [tblStudDiag].[diagnosis].Value, [tblStudDiag].[StartDate], [forms]![dailyreportdate]![rdate] AS reportdate, [tblStudDiag].duration, dateadd("d",[duration]-1,[StartDate]) AS EndDate, students_rep.nd_dsls & " " & students_rep.[taksi] AS Class
    FROM students_rep INNER JOIN tblStudDiag ON students_rep.agm=[tblStudDiag].[student]
    WHERE ([tblStudDiag].[StartDate]<=forms!dailyreportdate!rdate and dateadd("d",[tblStudDiag.duration]-1,[tblStudDiag.startdate]) >=forms!dailyreportdate!rdate);
    or even

    Code:
    SELECT students_rep.[lname], students_rep.[fname], students_rep.agm, [tblStudDiag].[diagnosis].Value, [tblStudDiag].[StartDate], [forms]![dailyreportdate]![rdate] AS reportdate, [tblStudDiag].duration, dateadd("d",[duration]-1,[StartDate]) AS EndDate, students_rep.nd_dsls & " " & students_rep.[taksi] AS Class
    FROM students_rep INNER JOIN tblStudDiag ON students_rep.agm=[tblStudDiag].[student]
    WHERE [tblStudDiag].[StartDate]<=forms!dailyreportdate!rdate and dateadd("d",[tblStudDiag.duration]-1,[tblStudDiag.startdate]) >=forms!dailyreportdate!rdate;
    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

  3. #3
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    I couldn't agree more!
    SQL is usually easy to read but access makes things complicated with no reason

    unfortunately neither of those did the job..

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You don't have a time element in those dates, do you?
    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

  5. #5
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    I'm guessing no?
    How can I make sure?

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    also this part of your query will return unexpected results

    ......[tblStudDiag].[StartDate])<=forms!dailyreportdate!rdate......

    the rDate control is a string and will interprete any ambiguous values as the US format of mm/dd/yyyy

    a date of 19/09/2023 is unambiguous since there are not 19 months in the year.

    But a date of 12/09/2023 will be interpreted as 9th December, not 12th September

    a date of 10/10/2023 will work either way, but 11/10/2023 will be interpreted as 10th November. not 11th October

    use the # date delimiters and the format function - recommend use the SQL standard of yyyy-mm-dd

    Also don't recommend using the shortened date (23 v 2023) as that may also be interpreted differently

    try

    ......[tblStudDiag].[StartDate])<='#' & format(forms!dailyreportdate!rdate,'yyyy-mm-dd') & '#'......



  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Could be something in your join. Also where is this value forms!dailyreportdate!rdate? Is that in the header on the form showing these records? Maybe code not recognizing that date?

    Also try to troubleshoot specific parts like replace some of that with actual dates, take out
    forms!dailyreportdate!rdate and put in an actual date there to see if it works, etc.

  8. #8
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    The problem seems to be here
    ((dateadd("d",[tblStudDiag.duration]-1,[tblStudDiag.startdate]))>=forms!dailyreportdate!rdate))
    As you can see, access does return the entries for which startdate<=reportdate but ignores the "enddate>=reportdate" part.
    The value [forms]![dailyreportdate]![rdate] is in the detail section of the form. Access doesn't recognise it for some reason, I have to enter it manually from a message box...

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Shame you didn’t mention that at the beginning, could have been answered in the first response

    You are missing square brackets around the .

    or Remove them - they are only required when name has spaces, non alpha numeric characters or is a reserved word

  10. #10
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    Which ones are you referring to?

  11. #11
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    I actually removed all the square brackets and now the code is
    Code:
    SELECT students_rep.lname, students_rep.fname, students_rep.agm, tblStudDiag.diagnosis.Value, tblStudDiag.StartDate, tblStudDiag.duration, DateAdd("d",duration-1,StartDate) AS EndDate, students_rep.nd_dsls & " " & students_rep.taksi AS Class, forms!dailyreportdate!rdate AS reportdateFROM students_rep INNER JOIN tblStudDiag ON students_rep.agm = tblStudDiag.student
    GROUP BY students_rep.lname, students_rep.fname, students_rep.agm, tblStudDiag.diagnosis.Value, tblStudDiag.StartDate, tblStudDiag.duration, DateAdd("d",duration-1,StartDate), students_rep.nd_dsls & " " & students_rep.taksi, DateAdd("d",tblStudDiag.duration-1,tblStudDiag.startdate), forms!dailyreportdate!rdate
    HAVING (((tblStudDiag.StartDate)<=forms!dailyreportdate!rdate) And (DateAdd("d",duration-1,tblstuddiag.StartDate))>=forms!dailyreportdate!rdate);
    But still, the query won't recognise the value from the form and it will return the same results (ignoring the reportdate<=enddate)

  12. #12
    Bilakos93 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    12
    Interestingly when I write OR instead of AND I get all the entries , which means that there is definitely something wrong on the (DateAdd("d",duration-1,tblstuddiag.StartDate))>=forms!dailyreportdate!r date) I'm guessing?

  13. #13
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    have you checked out post #6 ?

  14. #14
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Can you upload the Db?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Bilakos93 View Post
    Interestingly when I write OR instead of AND I get all the entries , which means that there is definitely something wrong on the (DateAdd("d",duration-1,tblstuddiag.StartDate))>=forms!dailyreportdate!r date) I'm guessing?
    Not really, completely different criteria.
    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

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

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2023, 12:16 PM
  2. Return values from yesterday date
    By pvl55 in forum Queries
    Replies: 2
    Last Post: 08-25-2021, 11:36 AM
  3. Replies: 6
    Last Post: 08-09-2018, 11:41 PM
  4. Replies: 3
    Last Post: 10-30-2016, 05:50 AM
  5. Replies: 3
    Last Post: 06-02-2014, 10:41 PM

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