"jpr" <jp***@tin.it> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Friends, I have a form with four fields, date1, date2, date3 and date4.
All these have all a mm/dd/yyyy format and have their source to a
table.
I need to add an unbound control (I will name it txtresult) which will
count in months and years the difference between the dates.
Ex.
date1: 01/01/1970
date2: 05/05/1980
date3: 06/06/1990
date4: 06/30/2001
txtresult: .....
Fields date3 and date4 may be blank, therefore the code should consider
this option in counting only the first two.
Thanks for any help.
Create a sub to show this interval and like your previous post, you need to
call it from the OnCurrent event and also any place where you can catch the
dates changing. Here is the sort of thing:
Private Sub ShowInterval()
On Error GoTo Err_Handler
Dim lngDays As Long
Dim lngMonths As Long
Dim lngYears As Long
Dim strInterval As String
Dim dteOne As Date
Dim dteTwo As Date
If Not IsNull(Me.txtDate4) Then
If IsNull(Me.txtDate3) Then
strInterval = "Date 3 is missing"
Else
dteOne = Me.txtDate3.Value
dteTwo = Me.txtDate4.Value
If dteOne > dteTwo Then
strInterval = "Date four is before date three"
End If
End If
Else
If IsNull(Me.txtDate1) Then
If IsNull(Me.txtDate2) Then
strInterval = "Dates 1 and 2 are missing"
Else
strInterval = "Date 1 is missing"
End If
Else
If IsNull(Me.txtDate2) Then
strInterval = "Date 1 is missing"
Else
dteOne = Me.txtDate1.Value
dteTwo = Me.txtDate2.Value
If dteOne > dteTwo Then
strInterval = "Date two is before date one"
End If
End If
End If
End If
If Len(strInterval) = 0 Then
lngMonths = DateDiff("m", dteOne, dteTwo)
lngDays = DateDiff("d", DateAdd("m", lngMonths, dteOne), dteTwo)
If lngDays < 0 Then
lngMonths = lngMonths - 1
lngDays = DateDiff("d", DateAdd("m", lngMonths, dteOne), dteTwo)
End If
lngYears = lngMonths \ 12
lngMonths = lngMonths Mod 12
strInterval = CStr(lngYears) & " yrs " & _
CStr(lngMonths) & " mths " & _
CStr(lngDays) & " days"
End If
Me.txtResult = strInterval
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Sub