Brianwarnock
Retired
- Local time
- Today, 12:52
- Joined
- Jun 2, 2003
- Messages
- 12,701
I knew it was a mistake to do it in a rush, Integers cannot be set to Null empty strings or Blank, I'll be back.
Brian
Brian
I knew it was a mistake to do it in a rush, Integers cannot be set to Null empty strings or Blank, I'll be back.
Brian
Public Function fageyrmthday(startdate As Date, enddate As Date) As String
Dim dayst As Integer
Dim dayend As Integer
Dim mthst As Integer
Dim mthend As Integer
Dim yrst As Integer
Dim yrend As Integer
Dim numdays As Integer
Dim nummonths As Integer
Dim Numyears As Integer
dayst = Day(startdate)
dayend = Day(enddate)
mthst = Month(startdate)
mthend = Month(enddate)
yrst = Year(startdate)
yrend = Year(enddate)
If dayst - dayend = 1 Then GoTo completemonth
If dayend >= dayst Then
numdays = dayend - dayst + 1
Else
numdays = Day(DateSerial(yrst, mthst + 1, 0) - dayst + dayend + 1) ' ie lastday of month-startday =endday+1 for inclusive day
If mthst < 12 Then ' beceause we have covered startmonth in days
mthst = mthst + 1 ' we need to adjust the month
Else
mthst = 1 ' and possibly the year
yrst = yrst + 1
End If
End If
completemonth:
If mthend >= mthst Then
nummonths = mthend - mthst
Numyears = yrend - yrst
Else
nummonths = 12 - mthst + mthend
Numyears = yrend - yrst - 1
End If
If numdays = 0 Then
fageyrmthday = ""
ElseIf numdays = 1 Then
fageyrmthday = numdays & " Day"
Else
fageyrmthday = numdays & " Days"
End If
If nummonths = 1 Then
fageyrmthday = nummonths & " Month " & fageyrmthday
ElseIf nummonths > 1 Then
fageyrmthday = nummonths & " Months " & fageyrmthday
End If
If Numyears = 1 Then
fageyrmthday = Numyears & " Year " & fageyrmthday
ElseIf Numyears > 1 Then
fageyrmthday = Numyears & " Years " & fageyrmthday
End If
End Function
If Not IsDate(enddate) Then
fageyrmthday = "No Enddate"
Exit Function
End If
As Startdate and Enddate are declared as dates in the function an immediate #error is returned if either is not. If you do not wish this to happen declare them as variants and add code like this as the first to be executed.
Code:If Not IsDate(enddate) Then fageyrmthday = "No Enddate" Exit Function End If
brian
I've no idea how that can happen with the code I posted, with the original code where Startdate and enddate are declared as Dates I get an immediate error in my query if they are not valid dates, with the later code I get No Enddate for example if the data passed is not a Date.
What is in the date field?
I'm sorry to say that i will be leaving shortly and will not be back on until tomorrow evening and then very briefly.
Brian
Ok You haven't replied and I have to go, you must have failed to declare them as dates, they are proper dates aren't they, do what I suggested a couple of posts back and include the If Not isnull etc code for both dates.
Brian
Public Function TimeOnJob(dtStart As Date, dtEnd As Date) As String
Dim intNmbrOfMonths As Integer
intNmbrOfMonths = DateDiff("m", dtStart, dtEnd)
TimeOnJob = intNmbrOfMonths \ 12 & " year" & IIf(intNmbrOfMonths \ 12 > 1 Or intNmbrOfMonths \ 12 = 0, "s ", " ") & (intNmbrOfMonths Mod 12) & " month" & IIf(intNmbrOfMonths Mod 12 > 1 Or intNmbrOfMonths Mod 12 = 0, "s ", " ")
End Function
Nothing in the date field--it is empty.
Public Function fageyrmthday(startdate As Variant, enddate As Variant) As String
Dim dayst As Integer
Dim dayend As Integer
Dim mthst As Integer
Dim mthend As Integer
Dim yrst As Integer
Dim yrend As Integer
Dim numdays As Integer
Dim nummonths As Integer
Dim Numyears As Integer
If Not IsDate(startdate) Then
fageyrmthday = "Invalid Startdate"
Exit Function
End If
If Not IsDate(enddate) Then
fageyrmthday = "Invalid Enddate"
Exit Function
End If
dayst = Day(startdate)
dayend = Day(enddate)
mthst = Month(startdate)
mthend = Month(enddate)
yrst = Year(startdate)
yrend = Year(enddate)
If dayst - dayend = 1 Then GoTo completemonth
If dayend >= dayst Then
numdays = dayend - dayst + 1
Else
numdays = Day(DateSerial(yrst, mthst + 1, 0) - dayst + dayend + 1) ' ie lastday of month-startday =endday+1 for inclusive day
If mthst < 12 Then ' beceause we have covered startmonth in days
mthst = mthst + 1 ' we need to adjust the month
Else
mthst = 1 ' and possibly the year
yrst = yrst + 1
End If
End If
completemonth:
If mthend >= mthst Then
nummonths = mthend - mthst
Numyears = yrend - yrst
Else
nummonths = 12 - mthst + mthend
Numyears = yrend - yrst - 1
End If
If numdays = 0 Then
fageyrmthday = ""
ElseIf numdays = 1 Then
fageyrmthday = numdays & " Day"
Else
fageyrmthday = numdays & " Days"
End If
If nummonths = 1 Then
fageyrmthday = nummonths & " Month " & fageyrmthday
ElseIf nummonths > 1 Then
fageyrmthday = nummonths & " Months " & fageyrmthday
End If
If Numyears = 1 Then
fageyrmthday = Numyears & " Year " & fageyrmthday
ElseIf Numyears > 1 Then
fageyrmthday = Numyears & " Years " & fageyrmthday
End If
End Function
id startdate enddate TimeOnJob
1 02/01/2009 03/05/2009 4 Months 2 Days
2 02/02/2001 03/02/2009 8 Years 2 Days
3 29/12/2008 02/02/2010 1 Year 1 Month 5 Days
4 25/02/2009 04/03/2010 1 Year 8 Days
5 21/03/2009 24/03/2009 4 Days
6 02/03/2005 01/04/2008 3 Years 1 Month
7 01/01/2010 Invalid Enddate
8 01/02/2010 Invalid Startdate
The following function will return your string formatted as you want:
Code:Public Function TimeOnJob(dtStart As Date, dtEnd As Date) As String Dim intNmbrOfMonths As Integer intNmbrOfMonths = DateDiff("m", dtStart, dtEnd) TimeOnJob = intNmbrOfMonths \ 12 & " year" & IIf(intNmbrOfMonths \ 12 > 1 Or intNmbrOfMonths \ 12 = 0, "s ", " ") & (intNmbrOfMonths Mod 12) & " month" & IIf(intNmbrOfMonths Mod 12 > 1 Or intNmbrOfMonths Mod 12 = 0, "s ", " ") End Function
The function suggested above RETURNED NOTHING! That's why you got a blank line in the immediate window. (pay attention people!) With the function I've written, typing ? TimeOnJob(#3/12/2005#, #4/5/2010#) in the immediate window will return "5 years 1 month"
id startdate enddate TimeOnJob Expr1
1 02/01/2009 03/05/2009 4 Months 2 Days 0 years 4 months
2 02/02/2001 03/02/2009 8 Years 2 Days 8 years 0 months
3 29/12/2008 02/02/2010 1 Year 1 Month 5 Days 1 year 2 months
4 25/02/2009 04/03/2010 1 Year 8 Days 1 year 1 month
5 21/03/2009 24/03/2009 4 Days 0 years 0 months
6 02/03/2005 01/04/2008 3 Years 1 Month 3 years 1 month
7 01/01/2010 Invalid Enddate #Error
8 01/02/2010 Invalid Startdate #Error
Thanks a lot Brian,
I just tested it and it works great! Loving it!
So, when you set your variables (that is, dayst = Day(startdate)) before entering the If Not IsDate Function, is that what is causing the error?
Are you expecting Null enddates, perhaps where the project is ongoing?
If so you could set it to Date() and append "to Date" to the answer with a few simple changes.
Brian