Results 1 to 13 of 13
  1. #1
    ry94080 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jul 2019
    Posts
    36

    Find any missing dates

    Hello all,

    I have an automated report that runs every day and logs to a table that it has run for that day.

    Every once in awhile, the report will fail and not run on a particular day.

    The table will be missing a date as seen below. As you can see, 1/3/2024 is missing. How can i write a query that would look for missing dates? So, that i can have my application rerun for that particular date?


    1/1/2024
    1/2/2024
    1/4/2024
    1/5/2024



    Any help appreciated.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    All I can think of is comparing against a full table of dates, with the Not Matching query.
    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
    ry94080 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jul 2019
    Posts
    36
    Thanks for the reply. That crossed my mind too, but i was trying to avoid having to maintain a table full of dates.

    May have to go with that, if there aren't any other ideas.

    Thanks again!

  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,652
    You could loop the records and compare each to the one before, or loop dates and check if each is in the table, but the suggested table is probably simplest.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Quote Originally Posted by ry94080 View Post
    Thanks for the reply. That crossed my mind too, but i was trying to avoid having to maintain a table full of dates.

    May have to go with that, if there aren't any other ideas.

    Thanks again!
    Not hard to do.
    Create a Table for Day,Month and Year and one for Dates.

    Then run this query.
    Code:
    INSERT INTO tblDates ( StepsDate )
    SELECT DateSerial([YearNo],[MonthNo],[DayNo]) AS Dates
    FROM tblDay, tblMonth, tblYear
    WHERE (((DateSerial([YearNo],[MonthNo],[DayNo])) Between [Tempvars]![DtFrom] And [Tempvars]![dtTo]) And ((IsDate([MonthNo] & "/" & [DayNo] & "/" & [YearNo]))<>False))
    ORDER BY DateSerial([YearNo],[MonthNo],[DayNo]);
    How you determine the range is up to you. I used a form to populate the TempVars.
    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

  6. #6
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    maybe create 2 work tables with an autonumber field + date field.
    First action: insert the dates in table 1, sorted ascending by date.
    Second action: duplicate this table with a make table query, make sure the number field there is a long integer
    Third action: update the number col of table 2: number = number - 1
    Then create a query where you join both tables on the number field.
    You should have something like

    Numberfield date 1 date 2
    1 1/1/2024 1/2/2024
    2 1/2/2024 1/4/2024
    3 1/4/2024 1/5/2024
    4 1/5/2024

    then query for records where col2 - col1 >= 2

  7. #7
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    you could create a tally or numbers table that goes from 0 to <pick a number> and then use DATEADD() with it to create a table of dates. Then you basically do the outer join trick. Zzzzz.

    Shame you can't just use something like LAG() or LEAD() to look at the previous or next records. Then you could just use DATEDIFF with those and if the answer isn't 1, then you have a gap.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Emulating LAG() or LEAD() in Access requires correlated subquery. Be aware this can perform slowly with large dataset.

    Consider:
    SELECT [ReportDate]-1 AS MissingDte
    FROM Reports
    WHERE ((([ReportDate]-(SELECT TOP 1 ReportDate FROM Reports AS Dupe WHERE Dupe.ReportDate < Reports.ReportDate ORDER BY Dupe.ReportDate DESC))>1));

    However, this assumes there would not be multi-date gaps.

    It is possible for query to dynamically generate a dataset of all dates within a given range, but again, can perform slowly.

    Why would a report fail to run? Could there ever be multiple reports on same date? What else is done with this report - exported, saved as PDF?
    Last edited by June7; 12-18-2024 at 07:49 AM.
    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.

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    an alternative method is to use a non standard join

    assumptions:

    the field is a date field
    the field does not contain a time element (perhaps hidden by formatting)
    there are no contiguous missing dates (although the query can be run multiple times)

    Using June's table/field names

    Code:
    SELECT A.ReportDate+1 AS MissingDate
    FROM Reports A LEFT JOIN Reports B ON A.ReportDate+1=B.ReportDate
    WHERE B.ReportDate is Null AND A.ReportDate<>dmax('ReportDate','Reports')
    You need the dmax comparision to exclude the last record which will never have a following record

    vba code might be something like this to loop through the code multiple times

    Code:
    dim sqlStr as string
    dim rs as dao.recordset
    dim hasUpdates as boolean
    
    sqlstr="SELECT A.ReportDate+1 AS MissingDate" & _
    " FROM Reports A LEFT JOIN Reports B ON A.ReportDate+1=B.ReportDate" & _
    " WHERE B.ReportDate is Null l AND A.ReportDate<>dmax('ReportDate','Reports')"
    
    hasUpdates=true
    
    while hasUpdates
        set rs=currentdb.openrecordset sqlstr
        rs.movefirst
        hasupdates=not rs.eof
        while not rs.eof
           runreport rs!missingdate 'whatever you are using to run the report
           rs.movenext
        wend 'rs.eof
    wend 'hasupdates

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Right, I keep forgetting about non-standard join.

    Will not be able to build or even view in query design view, only SQL view.
    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.

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    I was fiddling around and came up with a different approach, not a query, but I thought I'd throw it out there.
    I populate a dictionary of the dates for a given month. I then loop though the table of dates and use the dictionary exists method and if a date exists in the table I remove it from the dictionary.
    Whatever is left over in the dictionary at the end is missing dates which are written to a listbox.
    There's alot of code in the demo which is only for the demo. The top 2 subs are the relevant ones. The findmissing sub can be called in many places.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    Will not be able to build or even view in query design view, only SQL view.
    agreed, but the same goes for other queries such as union queries. And does have the benefit of being able to use indexing (assuming OP has set them)

    EDIT: However you can build the basic query - joining on ON A.ReportDate=B.ReportDate, then move to sql view to add the +1 to A.ReportDate
    Last edited by CJ_London; 12-18-2024 at 01:09 PM.

  13. #13
    ry94080 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jul 2019
    Posts
    36
    THANKS EVERYONE! Will have to go through and weigh all of these options

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

Similar Threads

  1. Find missing dates in table.
    By shod90 in forum Forms
    Replies: 4
    Last Post: 01-23-2023, 02:15 AM
  2. Replies: 2
    Last Post: 08-27-2020, 06:50 AM
  3. Replies: 2
    Last Post: 09-19-2017, 11:31 AM
  4. Replies: 2
    Last Post: 03-02-2015, 12:19 PM
  5. Replies: 4
    Last Post: 09-17-2012, 02:15 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