Date Diff Years and Months

JPR

Registered User.
Local time
Today, 12:40
Joined
Jan 23, 2009
Messages
207
Hello,

can anyone help me with a date function?
I have two date fields and need to return the difference in years and months in an unbound txtbox. Thank you.
 
Code:
Function fAgeYMD(StartDate As Date, EndDate As Date) As String
'Purpose:   Returns the difference between StartDate and EndDate in full years, months and days
'Coded by:  raskew
'To call:
' ? fAgeYMD(#7/6/54#, #10/3/84#)
'Returns:
' 30 years 2 months 28 days

Dim inthold As Integer
Dim dayHold As Integer

   inthold = Int(DateDiff("m", StartDate, EndDate)) + _
             (EndDate < DateSerial(year(EndDate), month(EndDate), Day(StartDate)))

   If Day(EndDate) < Day(StartDate) Then
      dayHold = DateDiff("d", StartDate, DateSerial(year(StartDate), month(StartDate) + 1, 0)) + Day(EndDate)
   Else
      dayHold = Day(EndDate) - Day(StartDate)
   End If
   
   fAgeYMD = Int(inthold / 12) & " year" & IIf(Int(inthold / 12) <> 1, "s ", " ") _
             & inthold Mod 12 & " month" & IIf(inthold Mod 12 <> 1, "s ", " ") _
             & LTrim(str(dayHold)) & " day" & IIf(dayHold <> 1, "s", "")

End Function

Here is a function that goes down to days, you could amend it accordingly.
 
Thank you. Since I am not very good with VBA. I believe I place the code in a module and how will I call it so that is will show up in my result textbox? Thanks.
 
On your form go to the control that displays the period and enter =FageYMD(Me.YourStartDate,Me.YourEndDate) on the control source property
 

Users who are viewing this thread

Back
Top Bottom