startdate = #3/12/05#
enddate = #4/5/10#
x = datediff("m", startdate, enddate)
? x
61
y = x\12 & " year" & iif(x\12>1 or x\12=0, "s ", " ") & (x mod 12) & " month" & iif(x mod 12>1 or x mod 12=0, "s ", " ")
? y
5 years 1 month
startdate = #3/12/05#
enddate = #4/5/10#
x = datediff("m", startdate, enddate)
? x
61
y = x\12 & " year" & iif(x\12>1 or x\12=0, "s ", " ") & (x mod 12) & " month" & iif(x mod 12>1 or x mod 12=0, "s ", " ")
? y
5 years 1 month
I used a Switch Function to get it to display the way I want it to. In my query I entered the following expression:
Time on Job: Switch([years]=0,"",[years]=1,[years] & " year"+" ",[years]>1,[years] & " years"+" ") & Switch([months]=0,"",[months]=1,[months] & " month",[months]>1,[months] & " months")
Did I do this correct?
I tried using an iff function, but I had trouble with 3 comparison--that is, if years equal 0 then nothing, if years = 1 then "year", and if years > 1 then "years"
my advise for a complex calculation to create your oun function and send to it the startDate and endDate values.
it will be much easier for you to foloow exactly what go on.
my advise for a complex calculation to create your oun function and send to it the startDate and endDate values.
it will be much easier for you to foloow exactly what go on.
in your query where you normaly put the name f the field (from table) you put your function:
TimeOnJob([years], [months])
where Years and Months are the name of the fields.
but as you don't have these and they are also calculated why won't you make the function to also calculate them:
Code:
Function TimeOnJob(StartDate As date, EndDate As date) as string
dim years as long
dim months as long
[I]years = int((DateDiff("m",StartDate,EndDate)) / 12)
months = DateDiff("m",StartDate,EndDate) - years*12[/I]
TimeOnJob = Switch(years = 0, "", years = 1, years & " year" + " ", years > 1, years & " years" + " ") _
& Switch(months = 0, "", months = 1, months & " month", months > 1, months & " months")
End Function
in your query you put:
TimeOnJob([StartDate], [EndDate])
I think you can make your function more readable
some won't agree with me but I prefer longer code that it's easier to see what the intention was
Code:
TimeOnJob = ""
select case year
case 0
TimeOnJob = ""
case 1
TimeOnJob = iif(months > 0, "1 year and ", "1 year")
case else
TimeOnJob = iif(months > 0, years & " years and ", years & " years")
end select
select case months
case 0
TimeOnJob = TimeOnJob
case 1
TimeOnJob = TimeOnJob & "1 month"
case else
TimeOnJob = TimeOnJob & months & " months"
end select
in your query where you normaly put the name f the field (from table) you put your function:
TimeOnJob([years], [months])
where Years and Months are the name of the fields.
but as you don't have these and they are also calculated why won't you make the function to also calculate them:
Code:
Function TimeOnJob(StartDate As date, EndDate As date) as string
dim years as long
dim months as long
[I]years = int((DateDiff("m",StartDate,EndDate)) / 12)
months = DateDiff("m",StartDate,EndDate) - years*12[/I]
TimeOnJob = Switch(years = 0, "", years = 1, years & " year" + " ", years > 1, years & " years" + " ") _
& Switch(months = 0, "", months = 1, months & " month", months > 1, months & " months")
End Function
in your query you put:
TimeOnJob([StartDate], [EndDate])
I think you can make your function more readable
some won't agree with me but I prefer longer code that it's easier to see what the intention was
Code:
TimeOnJob = ""
select case year
case 0
TimeOnJob = ""
case 1
TimeOnJob = iif(months > 0, "1 year and ", "1 year")
case else
TimeOnJob = iif(months > 0, years & " years and ", years & " years")
end select
select case months
case 0
TimeOnJob = TimeOnJob
case 1
TimeOnJob = TimeOnJob & "1 month"
case else
TimeOnJob = TimeOnJob & months & " months"
end select
I agree your code looks readable. Let me take a lunch break and comb back then I may understand it better because I have been trying this for a while and my mind is off.
in your query where you normaly put the name f the field (from table) you put your function:
TimeOnJob([years], [months])
where Years and Months are the name of the fields.
but as you don't have these and they are also calculated why won't you make the function to also calculate them:
Code:
Function TimeOnJob(StartDate As date, EndDate As date) as string
dim years as long
dim months as long
[I]years = int((DateDiff("m",StartDate,EndDate)) / 12)
months = DateDiff("m",StartDate,EndDate) - years*12[/I]
TimeOnJob = Switch(years = 0, "", years = 1, years & " year" + " ", years > 1, years & " years" + " ") _
& Switch(months = 0, "", months = 1, months & " month", months > 1, months & " months")
End Function
in your query you put:
TimeOnJob([StartDate], [EndDate])
I think you can make your function more readable
some won't agree with me but I prefer longer code that it's easier to see what the intention was
Code:
TimeOnJob = ""
select case year
case 0
TimeOnJob = ""
case 1
TimeOnJob = iif(months > 0, "1 year and ", "1 year")
case else
TimeOnJob = iif(months > 0, years & " years and ", years & " years")
end select
select case months
case 0
TimeOnJob = TimeOnJob
case 1
TimeOnJob = TimeOnJob & "1 month"
case else
TimeOnJob = TimeOnJob & months & " months"
end select
It's almost there, not quite. Problem is that sometimes the employment only lasted 2 weeks; therefore, I need to say something like: 10 days, 21 days, and so on. I tried if less than 30 put the number of days, but there is February and some months go to the 31st.
Days: DateDiff("d",[StartDate],[EndDate]) works, it gives me total number of days.
I need it broken down. For instance, 3/1/10 to 4/5/10 is 0 years, 1 month, and 5 days.
I suggest you first try with times longer then a month to make sure it works for you.
as for days I'm not sure there is an easy solution. normaly I will take months as 30 days, but as you said yourself it's not very acurate.
you can of course check what was the month of startDate and the month of endDate and calculate the exact days. but this will make your function much longer and complicate