INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

DateDiff Help!

DateDiff Help!

(OP)
Hello everyone!,

I am having a bit of trouble producing the results I need. I am trying to assign a tier level based on the persons hire date. If they have been here less than four years from today's date it will assign a "1" value or else "2". What I have below is not producing the results I need and I believe it has something do with the format "YYYY" and I don't think i have this formatted correctly. Here is an example of the hire date showing an incorrect tier assignment. The difference between the two dates is greater than 4 years and therefore should be assigned a level 2.


Hire Date Date() Tier
1/2/2013 8/9/17 1

TierLevel: IIf(DateDiff("yyyy",[SomeTable]![HireDate],Date())<4,"1","2")

Thanks!

RE: DateDiff Help!

DateDiff Function returns a Variant (Long) specifying the number of time intervals between two specified dates
So you get 4 or 5 or 6 etc (rounded)., but not 4.5 or 3.75 or 5.02

Are you going to store the Tier value in your table?

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: DateDiff Help!

(OP)
Yes I will be storing the Tier in my table. Basically, if the employee has been here over 4 years (based on their hiredate) assign a Tier "2" and if less than 4 years assign "1' to the Tier Level.

RE: DateDiff Help!

I don't think you want to use DateDiff() with years since:
Dates one day or 730 days apart can return the same value:

CODE --> debug

DateDiff("yyyy",#12/31/2016#,#1/1/2017# ) = 1
DateDiff("yyyy",#1/1/2016#,#12/31/2017# ) = 1 

If you are looking for a function or expression that returns something similar to an age calculation use this function from theAccessWeb.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: DateDiff Help!

(OP)
Thank you for your reply. I will read up on your suggestion.

RE: DateDiff Help!

"I will be storing the Tier in my table" - well, don't.
Today Employee1 has been working for the Company for 3.5 years (Tier 1), so in 6 months it will be 4 years (Tier 2). How often are you going to update this table?

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: DateDiff Help!

In addition to Andy's great comment about storing and maintaining the Tier, I would question the practice of hard-coding tier level thresholds in code/expressions rather than in a table. What happens when the tier levels change? Do you plan on finding all of the expressions where this is calculated and update them all?

The 4 years and Tier 1 and 2 belong in a small table where a user can modify them as business rules change. Minimally I would create small user-defined functions in a module named "modBusinessCalcs" like:

CODE --> vba

Function GetYears(datFrom As Date, datTo As Date) As Integer
    ' Returns the years between 2 dates
    ' Doesn't handle negative date ranges i.e. datFrom > datTo
    
    'datFrom is the starting/earlier date
    'datTo is the end/current date
    'Call this function like any other function
    '   CurrentAge = GetYears([DateOfBirth],Date())
    '   EmploymentYears = GetYears([DateOfHire],[DateOfRetire])
    
    If Month(datTo) < Month(datFrom) Or (Month(datTo) = _
                Month(datFrom) And Day(datTo) < Day(datFrom)) Then
            GetYears = Year(datTo) - Year(datFrom) - 1
    Else
            GetYears = Year(datTo) - Year(datFrom)
    End If
End Function
Public Function GetTier(datFrom As Date, datTo As Date) As Integer
    'Function to return the Tier value based on the years between datFrom and datTo
    'Call this function like any other function
    '   Tier = GetTier([DateOfHire],Date())
    
    Dim intYears As Integer
    ' First get the number of years between the dates
    intYears = GetYears(datFrom, datTo)
    Select Case intYears
        Case Is < 4
            GetTier = 1
        Case Else
            GetTier = 2
    End Select
End Function 

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: DateDiff Help!

(OP)
What a excellent thought! This is a much better way of handling these updates.

Thank you all for your time; Much Appreciated!

RE: DateDiff Help!

I should have given credit for the GetYears() function to theAccessWeb.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close