473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting years and months between dates

jpr
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.

Feb 1 '06 #1
14 4035
"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

Feb 1 '06 #2
jpr
What can I say! Just perfect! Greate job.Working the way I want,
only,,,,I would like to remove the days. Since it is not necessary for
my case. Can I just show the yy and mm? Thanks.

Feb 1 '06 #3
jpr
Just forgot an important detail. I have added your code to the Open
event of the form since the OnCurrect already has code.

I will also need the same code for another group of controls meaning
that I will need to repeat the same for other fields. Where do I put
the code (I understand that i will simply change the txtnames with the
new ones). Thanks.

Feb 1 '06 #4

"jpr" <jp***@tin.it> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
What can I say! Just perfect! Greate job.Working the way I want,
only,,,,I would like to remove the days. Since it is not necessary for
my case. Can I just show the yy and mm? Thanks.

strInterval = CStr(lngYears) & " yrs " & _
CStr(lngMonths) & " mths " & _
CStr(lngDays) & " days"

Of course, just format the string however you want, e.g. change

strInterval = CStr(lngYears) & " yrs " & _
CStr(lngMonths) & " mths " & _
CStr(lngDays) & " days"

to:

strInterval = CStr(lngYears) & " years " & _
CStr(lngMonths) & " months"

or whatever you want.
Feb 1 '06 #5

"jpr" <jp***@tin.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Just forgot an important detail. I have added your code to the Open
event of the form since the OnCurrect already has code.
Do you think that you can't (or shouldn't) have more than one bit of code in
an event? This is not correct. If the code belongs in the OnCurrent event,
then that is where it should be! You can structure your code like this:

Private Sub DoThingOne()
MsgBox "Thing One"
End Sub

Private Sub DoThingTwo()
MsgBox "Thing Two"
End Sub

Private Sub Form_Current()
DoThingOne
DoThingTwo
End Sub

I will also need the same code for another group of controls meaning
that I will need to repeat the same for other fields. Where do I put
the code (I understand that i will simply change the txtnames with the
new ones). Thanks.


You could re-write the function so that one function could work for two sets
of controls, but it might not be worth the work. You could just structure
the code as I have shown above. Does that make sense?
Feb 1 '06 #6
jpr
Hello, thanks for your patience.
I have the feeling something is not working. I think it is only
counting one group of date fields. This is how I have placed the code :
Private Sub Form_Open(Cancel As Integer)
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.WEUSRFROM1) Then
If IsNull(Me.WEUSRTO1) Then
strInterval = "Date 3 is missing"
Else
dteOne = Me.WEUSRFROM1.Value
dteTwo = Me.WEUSRTO1.Value
If dteOne > dteTwo Then
strInterval = "Date four is before date three"
End If
End If
Else
If IsNull(Me.WEUSRFROM) Then
If IsNull(Me.WEUSRTO) Then
strInterval = "Dates 1 and 2 are missing"
Else
strInterval = "Date 1 is missing"
End If
Else
If IsNull(Me.WEUSRTO) Then
strInterval = "Date 1 is missing"
Else
dteOne = Me.WEUSRFROM.Value
dteTwo = Me.WEUSRTO.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) & " years " & _
CStr(lngMonths) & " months"


End If
Me.txtresult = strInterval
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Please note that I have four date fields and from what I understand
dates cannot show future dates (this is fine for me). I have placed
these dates in my form:

weusrfrom: 01/01/2000
weusrto: 02/01/2001
weusrfrom1: 01/01/2002
weusrto1: 02/01/2005
I get: 3 yrs 1mth.

How about placing additional fields named:
clusrfrom
clusrto
clusrfrom1
clusrto1

???THanks.

Feb 1 '06 #7

"jpr" <jp***@tin.it> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello, thanks for your patience.
I have the feeling something is not working. I think it is only
counting one group of date fields. This is how I have placed the code :
Private Sub Form_Open(Cancel As Integer)
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.WEUSRFROM1) Then
If IsNull(Me.WEUSRTO1) Then
strInterval = "Date 3 is missing"
Else
dteOne = Me.WEUSRFROM1.Value
dteTwo = Me.WEUSRTO1.Value
If dteOne > dteTwo Then
strInterval = "Date four is before date three"
End If
End If
Else
If IsNull(Me.WEUSRFROM) Then
If IsNull(Me.WEUSRTO) Then
strInterval = "Dates 1 and 2 are missing"
Else
strInterval = "Date 1 is missing"
End If
Else
If IsNull(Me.WEUSRTO) Then
strInterval = "Date 1 is missing"
Else
dteOne = Me.WEUSRFROM.Value
dteTwo = Me.WEUSRTO.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) & " years " & _
CStr(lngMonths) & " months"


End If
Me.txtresult = strInterval
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Please note that I have four date fields and from what I understand
dates cannot show future dates (this is fine for me). I have placed
these dates in my form:

weusrfrom: 01/01/2000
weusrto: 02/01/2001
weusrfrom1: 01/01/2002
weusrto1: 02/01/2005
I get: 3 yrs 1mth.

How about placing additional fields named:
clusrfrom
clusrto
clusrfrom1
clusrto1

???THanks.


When you write "I get: 3 yrs 1mth", are you saying this is correct or
incorrect?

To deal with the new set of controls you could either re-write the sub so it
takes the names of the controls as parameters, and then 1 sub could cope
with two different possiblities. But you could just copy and paste the sub
and change the names of the controls. The code should be written in their
own separate subs. These subs can then be called from multiple places like
the form's current event:

Private Sub DoControls1()
' Code for controls: weusrfrom, weusrto, weusrfrom1, weusrto1
End Sub

Private Sub DoControls2()
' Code for controls: clusrfrom, clusrto, clusrfrom1, clusrto1
End Sub

Private Sub Form_Current()
DoControls1
DoControls2
End Sub

Feb 1 '06 #8
jpr
Yes, I get 3 yrs 1 mth. It appears that is is only counting the first
two group of date fields.

Feb 2 '06 #9
jpr
I only counts two group of dates. Date3 and Date4. If you want I can
sent you a zip mdb file. Thanks.

Feb 2 '06 #10
"jpr" <jp***@tin.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I only counts two group of dates. Date3 and Date4. If you want I can
sent you a zip mdb file. Thanks.

That's what I thought *should* happen. In your original post you wrote:

"Fields date3 and date4 may be blank, therefore the code should consider
this option in counting only the first two."

So what should happen if all four dates are there? Have a look at the first
bit of the code - this is where I decide which two dates to compare. Can
you edit this yourself? If not, just give a clear explanation of what
should happen - If you are still stuck, I'll send you an e-mail address to
send any file.
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
Feb 2 '06 #11
jpr
Thanks for your great help.
I will try to be more clear possible.

My form has 5 fields: four date fields and a txtresult.

I use the date fields to count the number of years a person has lived
in a certain Country, meaning that his residence can show intervals of
years (like from 2000 to 2002 and from 2004 to 2005).
If a person has resided for a consecutive number of years, well I will
only use the first two date fields. If not, I will need to complete the
other two date fields.

The txtresult field should consider both options.
1) Count only the first two if the date3 and date4 are blank.
2) Count all four date fields if all are showing dates.

Hope this helps.

by the way where are you from. I am from Italy.
Ciao.

Feb 2 '06 #12
"jpr" <jp***@tin.it> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Thanks for your great help.
I will try to be more clear possible.

My form has 5 fields: four date fields and a txtresult.

I use the date fields to count the number of years a person has lived
in a certain Country, meaning that his residence can show intervals of
years (like from 2000 to 2002 and from 2004 to 2005).
If a person has resided for a consecutive number of years, well I will
only use the first two date fields. If not, I will need to complete the
other two date fields.

The txtresult field should consider both options.
1) Count only the first two if the date3 and date4 are blank.
2) Count all four date fields if all are showing dates.

Hope this helps.

by the way where are you from. I am from Italy.
Ciao.

OK now I understand. I will have to re-write it. I will try and do it
today.
Feb 2 '06 #13
jpr
Hello Anthony, still willing to help me????Thanks.

Feb 7 '06 #14
"jpr" <jp***@tin.it> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello Anthony, still willing to help me????Thanks.

I have sent a sample database in a private e-mail.
Feb 7 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12804
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
2
3403
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
1
1287
by: B W Dudley | last post by:
Trying to have a column in a report show the difference between two dates. E.G. One column has "date joined" and new column is to show difference from "join date" and today; preferably in years and...
4
1911
by: Mr C | last post by:
Hi If i have a field in a database start date and end date, how am I able to find out which financial years the two dates cover, and populate colunms in a database with how many months in each...
6
27935
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
5
8805
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec....
23
13997
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
2
4816
by: ichew | last post by:
I have two dates - Date Joined and Actual Last Day. How do I find the YIS in for format of Years and Month from these two dates. Eg, Date Joined: 28/06/1971 and Actual Last Day: 24/01/2007, then...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.