Results 1 to 8 of 8
  1. #1
    MCregan is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    2

    Question How to calculate a timeframe when the end date may be a specific date or a current date

    I am making an employee database for my agency and have hit a snag. I want to present a field that calculates the number of years an employee has held a specific job title from a starting and ending date. The table I use is simple with the following fields: Employee ID, Job Title, Start Date, End Date, and Total years. Total years is calculated by (End Date - Start Date)/365. I then use the data from this table to populate a subform that is incorporated into an employee information form. It works beautifully except when there is no end date in the table because that is their current position. I am thinking I need to do something more complex but I am a pretty basic/beginner access user so I am not sure what will work best. I appreciate any suggestions.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Consider use of Nz() function:

    (Nz([End Date], Date()) - [Start Date])/365.

    Advise not to use spaces nor punctuation/special characters in naming convention.

    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.

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    The most repeated suggestion you'll get: don't store calculations. Do that in forms/reports/queries. So no field anywhere for total years. That should be calculated as required, not stored. Maybe your form (or query or report) has a calculated field that uses IIF function. So subtract if there are 2 values, and show nothing if both values are not present. Your query calculated field could be (untested) TotalYears: IIF(IsNull(EndDate),Date-StartDate,StartDate-EndDate) but I would probably use DateDiff function instead so that I could get the time span in whatever value I wanted (subject to the values that DateDiff can return of course). You might want to Google DateDiff function. Note that spaces and special characters (save for underscore) or reserved words are not used by pros (or me, for that matter ). So EndDate, not End Date and no use of the word Date for my own objects.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    MCregan is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    2
    When I enter that for the expression in the field properties an error message says it cannot be used in a calculated column. The expression does work as an update query so now I just need to remember how to make the update query run when the subform opens...

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Don't save to table, just calculate in query or textbox.
    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.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Length of time in a position sounds a lot like Age (Birthdate to current Date) in concept. There are several AGE functions in the various forums.
    Seems all employees would have a StartDate, and those people who are no longer in the position would have an End/Termination Date. For current Employees with no End Date, seems you could use CurrentDate to determine their time in the Position. The NZ(EndDate, Date()), as June suggested, seems appropriate.

    You shouldn't be storing the calculation in a table.

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    When I enter that for the expression in the field properties an error message says it cannot be used in a calculated column.
    That seems like you're trying to create the calculated field in a table. As per posts 3 and 5 - don't store calculations in a table. Might also mention that calculated table fields have very narrow limits on what you can do. Note "can do" not "should do".
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    See: Calculated Fields by Allen Browne

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

Similar Threads

  1. Replies: 9
    Last Post: 08-21-2019, 10:45 PM
  2. Replies: 3
    Last Post: 02-24-2019, 02:35 PM
  3. Replies: 3
    Last Post: 03-02-2016, 09:39 AM
  4. Replies: 3
    Last Post: 02-16-2016, 05:02 PM
  5. Using VB to calculate a specific date
    By barryg80 in forum Programming
    Replies: 8
    Last Post: 12-05-2013, 09:05 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