Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

calculating age using IF statement and no message boxes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J&J - 02 Nov 2006 23:32 GMT
I have created a form in Word 2000, but I'm having problems coming up with a
formula that calculates whether the person is 14 years old or older, and if
yes -- display their name, if no -- leave blank.

The form fields used in the form are named as the following bookmarks:

Name1 - this shows the person's name
DOB1 - this shows the person's date of birth
cc1 - this is where I need Name1 displayed if they are 14 years old or
older, otherwise leave it blank.

Can anyone come up with either a form calculation or macro that can do the
above, by using form fields, not message boxes?

 Thanks in advance!
Doug Robbins - Word MVP - 03 Nov 2006 00:16 GMT
The following can easily be modified to do what you want:

' a Macro to calculate the age in years, months and days

' based on a birthdate entered in cell A2 and insert the

' age in cell A3

' Macro created 16/9/00 by Doug Robbins

'

Dim Years As Long

Dim Months As Long

Dim Days As Long

Dim Birthrange As Range

Dim Birthday As Long

Dim Day As Long

Dim Age As String

Dim Agedays As String

Dim Yearnow As Long

Dim Monthnow As Long

Monthnow = Val(Format(Date, "m"))

Yearnow = Val(Format(Date, "yyyy"))

Dim mon$(12)

   mon$(1) = "January": mon$(2) = "February": mon$(3) = "March": mon$(4) =
"April": mon$(5) = "May": mon$(6) = "June": mon$(7) = "July": mon$(8) =
"August": mon$(9) = "September": mon$(10) = "October": mon$(11) =
"November": mon$(12) = "December"

Dim monthdays$(12)

   If ((Yearnow Mod 4 = 0 And Calyear Mod 400 = 0) Or (Calyear Mod 4 = 0
And Calyear Mod 100 <> 0)) Then

       monthdays$(1) = "31": monthdays$(2) = "29": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"

   Else

       monthdays$(1) = "31": monthdays$(2) = "28": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"

   End If

Set Birthrange = ActiveDocument.Tables(1).Cell(1, 2).Range

Birthrange.End = Birthrange.End - 1

Birthdate = Birthrange

Years = DateDiff("yyyy", Birthdate, Date)

Months = DateDiff("m", Birthdate, Date) - Years * 12

Birthday = Format(Birthdate, "d")

Day = Format(Date, "d")

Days = Day - Birthday

If Days > 0 Then

   Agedays = " and " & Format(Days) & " days."

ElseIf Days < 0 Then

   Agedays = " and " & Format(monthdays$(Monthnow) - Birthday + Day) & "
days."

   Months = Months - 1

Else

   Agedays = "."

End If

Age = Format(Years) & " years, " & Format(Months) & " Months" & Agedays

ActiveDocument.Tables(1).Cell(1, 3).Range.Text = Age

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I have created a form in Word 2000, but I'm having problems coming up with
>a
[quoted text clipped - 13 lines]
>
>  Thanks in advance!
Jezebel - 03 Nov 2006 04:11 GMT
If you just need to know if the person is 14 or more, then all you need
is --

If DateDif("yyyy", DOB, Now) >= 14 then ...

where DOB is your cell or formfield reference.

> The following can easily be modified to do what you want:
>
[quoted text clipped - 113 lines]
>>
>>  Thanks in advance!
Doug Robbins - Word MVP - 03 Nov 2006 05:44 GMT
True

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> If you just need to know if the person is 14 or more, then all you need
> is --
[quoted text clipped - 120 lines]
>>>
>>>  Thanks in advance!
J&J - 03 Nov 2006 23:55 GMT
Ok, the formula below is close to what I need, except I need it to figure out:
1. If the person is 14 years old or older, then show their name in the field
2. If the person is under 14 years of age, leave the form field blank.
The person's name and date of birth have been entered in separate form
fields within the same document.

I don't know how to write the formula, but maybe this is easier to understand:

If PERSONDOB =>14, then PERSONSNAME, else BLANK     ????

> > If you just need to know if the person is 14 or more, then all you need
> > is --
> >
> > If DateDif("yyyy", DOB, Now) >= 14 then ...
> >
> > where DOB is your cell or formfield reference.
Jezebel - 04 Nov 2006 03:09 GMT
If DateDiff("yyyy", ActiveDocument.FormFields("PERSONDOB ").Result, Now)
>= 14 Then
       ActiveDocument.FormFields("XXX").Result =
ActiveDocument.FormFields("PERSONSNAME").Result
   Else
       ActiveDocument.FormFields("XXX").Result = ""
   End If

where 'XXX' is the name of the field in which to display the name.

> Ok, the formula below is close to what I need, except I need it to figure
> out:
[quoted text clipped - 15 lines]
>> >
>> > where DOB is your cell or formfield reference.
Harold Druss - 04 Nov 2006 12:20 GMT
>I have created a form in Word 2000, but I'm having problems coming up with
>a
[quoted text clipped - 13 lines]
>
>  Thanks in advance!

Hi
A little more than DateDiff() is needed for this problem.

For instance: DateDiff("yyyy", #12/30/2005#, #1/1/2006#) will return 1 for a
person 2 days old.

Here is a function that will do the job.

==============================================================
Option Explicit
Const TARGETAGE As Long = 14

Private Function GetAge(dBirthDate As Date) As Boolean
Dim BirthYear As Long
Dim CurrentYear As Long
Dim BirthMonth As Long
Dim CurrentMonth As Long
Dim BirthDay As Long
Dim CurrentDay As Long

BirthYear = Year(dBirthDate)
CurrentYear = Year(Date)
BirthMonth = Month(dBirthDate)
CurrentMonth = Month(Date)
BirthDay = Day(dBirthDate)
CurrentDay = Day(Date)

If CurrentYear - BirthYear > TARGETAGE Then ' must be older than the
TARGETAGE
   GetAge = True

ElseIf CurrentYear - BirthYear < TARGETAGE Then ' must be younger the
TARGETAGE
   GetAge = False

' if we get here the difference is equal to the TARGETAGE - check the month
ElseIf CurrentMonth - BirthMonth > 0 Then ' birth month is before current
month
   GetAge = True

ElseIf CurrentMonth - BirthMonth < 0 Then 'birth month is after current
month
   GetAge = False

' if we get here the CurrentMonth is the same as the BirthMonth
ElseIf CurrentDay - BirthDay >= 0 Then
   GetAge = True

Else
   GetAge = False

End If

End Function
=========================================================================

Good luck
Harold
Harold Druss - 07 Nov 2006 10:20 GMT
>>I have created a form in Word 2000, but I'm having problems coming up with
>>a
[quoted text clipped - 74 lines]
> Good luck
> Harold

Here is another function to return the age of a person:
===================================================================
Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant

If Not IsDate(varBirthDate) Then Exit Function
   varAge = DateDiff("yyyy", varBirthDate, Now)

   If Date < DateSerial(Year(Now), Month(varBirthDate), _
                       Day(varBirthDate)) Then
       varAge = varAge - 1
   End If

   Age = CInt(varAge)

End Function
=======================================================================

Good luck
Harold
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.