Results 1 to 11 of 11
  1. #1
    Jeff G is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    8

    Date Formula - Most recent within certain time frame

    I hit a little road block with creating a query that retrieves certain records with dates. I'll try to explain as simply as I can. I'm using this database to track a single event. The separate queries i need to retrieve are:

    1. Events from current date to 89 days in the past (I"ll refer to this as "current") Formula used: Between Date() And Date()-89
    2. Events from 90 days to 179 days in the past ("90+ days") Formula used: Between Date() And Date()-89
    3. Events from 180+ days in the past. ("180+ days") Formula used: Between Date()-180 And Date()-1000



    Each query will have the Name, date and other info I'll add to the query. The block I'm running into is I only want to retrieve the last (most recent) date entered. When I originally ran it with just the date criteria it retrieved all records in that date range and I only need the last date for the completed event.

    Also, I don't know if I will keep having "false positives" by having the personnel showing on each of the queries. To further explain, If i have employee A complete the event once each month he'd be considered "Current" on completing the event. Here's what I'm trying to track: I need employees to complete a certain task once within a 90 day period and they're considered "Current." After 90+days they need to get evaluated while performing the event. After 180+ days they need to get retrained.

    The problem I might run into is if I run a query for those date ranges I'll have a "Current" employee also show up on the "90+" and the "180+" queries as well, which would tell me that this person last completed the event 180 days in the past.

    Any help is greatly appreciated. Let me know if any further clarification is needed.

    V/R,

    Jeff

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Item 2 should of course be Between Date()-90 And Date()-179

    1. Run Query 1 & get the list of current employees

    2. Run Query 2 on all employees NOT in Query 1.
    To do so, create an unmatched query for the employee list & Query 1 ...then filter that for your date range

    3. Run Query 3 on all employees NOT in Query 1 or 2 - similar approach to above
    Last edited by isladogs; 03-24-2019 at 03:34 AM.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Jeff G is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    8
    Excellent, thanks for the info. I'll give it a go. That's a typo on my part for the item 2, I have the correct one in the data sheet.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    You're welcome.
    In case you aren't aware there is a wizard you can use for unmatched queries
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Jeff G is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    8
    Do i need to have the totals tab selected under Show/Hide and select either min/max? Here's my design, view, and my SQL.

    Click image for larger version. 

Name:	SQL.jpg 
Views:	16 
Size:	92.2 KB 
ID:	37940Click image for larger version. 

Name:	Design View.jpg 
Views:	17 
Size:	116.9 KB 
ID:	37941
    Attached Thumbnails Attached Thumbnails View.jpg  

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    If you want the most recent date for each person then group by person and set date to Max
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    Jeff G is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    8
    I have everything falling into place except for the "Flights - Current" Query. When I go to set the parameters for Total - Group By I'm having issues. Under "Date" I have it set to "Max". It only retrieves one record for that date and leaves out other names that were on that same date. Which I understand that. When I also have the Name column set to max the query is blank when I click Run. If I only select Max on the Name column only and leave the Date column Group By blank it only retrieves 5/7 personnel that have entries. Again I'm just trying to pull the most recent date a person completed the event on the Table I'm querying. Sorry for the repeated questions, Access is fairly new to me, but it's going to make things easy when all I have to do is maintain this database.

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Group by Name and set Date field to Max. Add Date again but this time set to WHERE and add your filter.
    Note that the where filter field will be automatically unticked so it isn't displayed in the result
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    Join Date
    Apr 2017
    Posts
    1,679
    For tasks like this I have written an UDF

    ---
    Public Function ValidValue(parTable As String, IdField As String, IdCond As String, IdIsString As Boolean, RetField As String, DateField As String, parDate As Date)
    Dim dbs As Database
    Dim rs As Recordset
    Dim varQstr As String
    ' The function returns the value of RetField from table parTable
    ' where IdField equals IdCond and DateField is nearest past or equal to parDate.
    ' The parameter IdIsString must be True, when IdField has Text format

    On Error GoTo Err_ValidValue
    Set dbs = CurrentDb
    varQstr = "SELECT [" & RetField & "] FROM " & parTable & _
    " WHERE [" & DateField & "] <= " & Format(parDate, "\#mm\/dd\/yyyy\#") & _
    " And [" & IdField & "] = " & IIf(IdIsString, "'" & IdCond & "'", IdCond) & _
    " ORDER BY [" & DateField & "] DESC"
    Set rs = dbs.OpenRecordset(varQstr)
    rs.AbsolutePosition = 0
    ValidValue = rs.Fields(0).Value

    Err_ValidValue:
    rs.Close
    dbs.Close
    Set rs = Nothing
    Set dbs = Nothing
    End Function

    ---

    Current version works with non-date part of filter condition applied to single field (IdField). When you want to use more complex filter condition, then probably you have to use full WHERE clause string as parameter instead. The change to code will be minor though.

  10. #10
    Jeff G is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    8
    Isladogs, that did the trick. I very much appreciate your help. Thanks

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    You're welcome
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  2. Replies: 2
    Last Post: 10-30-2013, 11:40 AM
  3. Replies: 3
    Last Post: 06-18-2013, 08:17 PM
  4. Display Remaining Units within time frame
    By Stanggirlie in forum Access
    Replies: 3
    Last Post: 01-05-2013, 10:44 AM
  5. Calculating age during a specific time frame
    By mommaof4kids in forum Reports
    Replies: 1
    Last Post: 09-06-2012, 06:08 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