Home » C++ ProgrammingRSS

Age Calculator

Hi,

I have a question!

I've seen a lot of formulas of how to calculate a person age but I'm having a little problem.

I would like to incorporate an Age Calculator Operation with a Life Expectancy. Example:

I have a TextFormField where I enter the person DOB then I want Word to calculate the person DOB then look at the Life Expectancy for age calculated then show result in another TextFormField. Example:

I entered a DOB: 08/22/1982 then word calculated Age: 28 /The Life Expectancy for a White = 59.4 male | 64.2 female then I want Word to show result in another TextFormFiled.

In other words, I enter the person DOB, then words calculate person age then, I click on a checkbox for White/Black/Hispanic/etc...Then result will show in another TextFormField. (I will provide Life Expectancy Info).

Hope you understand.

Thanks for you time.

 

 

 

12 Answers Found

 

Answer 1

If the Date of Birth formfield has the bookmark name DOB assigned to it and the age  formfield has the bookmark name AGE assigned to it, running a macro containing the following code on exit from the DOB formfield will insert the age into the AGE formfield:

With ActiveDocument
    .FormFields("AGE").result = Int(DateDiff("yyyy", .FormFields("DOB").result, Date))
End With


-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:98e398ca-f06f-408e-9ea1-f314d1c078f0@communitybridge.codeplex.com...

Hi,

I have a question!

I've seen a lot of formulas of how to calculate a person age but I'm having a little problem.

I would like to incorporate an Age Calculator Operation with a Life Expectancy. Example:

I have a TextFormField where I enter the person DOB then I want Word to calculate the person DOB then look at the Life Expectancy for age calculated then show result in another TextFormField. Example:

I entered a DOB: 08/22/1982 then word calculated Age: 28 /The Life Expectancy for a White = 59.4 male | 64.2 female then I want Word to show result in another TextFormFiled.

In other words, I enter the person DOB, then words calculate person age then, I click on a checkbox for White/Black/Hispanic/etc...Then result will show in another TextFormField. (I will provide Life Expectancy Info).

Hope you understand.

Thanks for you time.

 

Answer 2

Perfect!

Now, i know how to calculate the age! SO.

If this man was born on 1970,he is today 40 years old. In the Life Expectancy Table shows, he will work for 37.9 years more. SO i would like word to show the 37.9 years. Hope you understand.

Then i have 3 checkbox bookmark as, CheckBox1= White, CheckBox2=Black, CheckBox3=Total Population. Also, i have two more checkbox bookmark as CheckBoxMale= Male, CheckBoxFemale= Female.

If he was born on 1970 the life expectancy of a white male is 37.9, female is 41.9

If he was born on 1970 the life expectancy of a black male is 33.5, female is 38.9

If he was born on 1970 the life expectancy of a Total Population male is 37.6, female is 41.7 

So if i check the following:

Enter: DOB=1970 then word calculate=40, then the CheckBox1 is checked + CheckBoxMale is checked. Word wil show 37.9

Please let me know if i'm clear?

I truly THANK YOU!! I've been a great help.

 

Answer 3

Where is the Life Expectancy Table?  How is the data in it arranged?

-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:3a3739b7-9432-413a-9390-caacfd6f82ec@communitybridge.codeplex.com...

Perfect!

Now, i know how to calculate the age! SO.

If this man was born on 1970,he is today 40 years old. In the Life Expectancy Table shows, he will work for 37.9 years more. SO i would like word to show the 37.9 years. Hope you understand.

Then i have 3 checkbox bookmark as, CheckBox1= White, CheckBox2=Black, CheckBox3=Total Population. Also, i have two more checkbox bookmark as CheckBoxMale= Male, CheckBoxFemale= Female.

If he was born on 1970 the life expectancy of a white male is 37.9, female is 41.9

If he was born on 1970 the life expectancy of a black male is 33.5, female is 38.9

If he was born on 1970 the life expectancy of a Total Population male is 37.6, female is 41.7

So if i check the following:

Enter: DOB=1970 then word calculate=40, then the CheckBox1 is checked + CheckBoxMale is checked. Word wil show 37.9

Please let me know if i'm clear?

I truly THANK YOU!! I've been a great help.

 

Answer 4

Doug,

The life expectancy is a table provided by U.S Government every year.

There is no calculation for life expectancy because it depends in the population mortality. i have all the information.

What i need is like the Drop-Down List with the cars, like the one we did before.

Example: Toyota = 9999

Now: A 40 years old white Male is expected to live 37.9 years more. (USA-Calculation)

in other words i will provide the result.

Thanks, let me know.

 

Answer 5

OK, I saved the table 105 downloaded from http://www.census.gov/compendia/statab/cats/births_deaths_marriages_divorces/life_expectancy.html in the My Documents folder with the name 10s0105(1).xls, then I have a document with a Date of Birth formfield that has the bookmark name DOB assigned to it, a DropDown formfield that has the bookmark name Category assigned to it and which contains the following items:

Total Population
White Male
White Female
Black Male
Black Female

and a TextInput FormField that has the bookmark name LifeExpectancy assigned to it and on exit from the DOB formfield and also from the Category formfield, I have a macro run that contains the following code, the appropriate life expectancy is displayed in the Live Expectancy formfield.

NOTE:  You will need to change the path and possibly the filename in the following line of code :

Set xlbook = xlapp.Workbooks.Open("C:\Users\Doug\Documents\10s0105(1).xls")

to suit where you have the table of life expectancies saved.

Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim myarray As Variant
Dim i As Long, j As Long
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
    bstartApp = True
    Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlbook = xlapp.Workbooks.Open("C:\Users\Doug\Documents\10s0105(1).xls")
Set xlsheet = xlbook.Worksheets(1)
With ActiveDocument
    i = Int(DateDiff("yyyy", .FormFields("DOB").result, Date)) + 11
    j = .FormFields("Category").DropDown.Value
    .FormFields("LifeExpectancy").result = xlsheet.Range("A1").Offset(i, j).Value
End With
xlbook.Close
If bstartApp = True Then
    xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing


-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:86232da8-a488-4d61-80f9-14a0acdb0f0f@communitybridge.codeplex.com...

Doug,

The life expectancy is a table provided by U.S Government every year.

There is no calculation for life expectancy because it depends in the population mortality. i have all the information.

What i need is like the Drop-Down List with the cars, like the one we did before.

Example: Toyota = 9999

Now: A 40 years old white Male is expected to live 37.9 years more. (USA-Calculation)

in other words i will provide the result.

Thanks, let me know.

 

Answer 6

Doug,

The table you got from the Internet is not from 2010, but it works, the only problem is that is not giving me a real result from the table.

I guess i have to specify where to look when you choose your category vs DOB. right?

I will recreate the 2010 table and use it as the Excel Doc.

 

 

Answer 7

It gave the proper results here for the table that I downloaded and saved in Excel.

If the table that you have has a different layout, you may need to adjust the offsets i and j in the following part of the code

    i = Int(DateDiff("yyyy", .FormFields("DOB").result, Date)) + 11
    j = .FormFields("Category").DropDown.Value
    .FormFields("LifeExpectancy").result = xlsheet.Range("A1").Offset(i, j).Value

In the table that I downloaded, the first row of data was row 12 which is offset row 1 by 11 rows, hence the addition of 11 to the age  (Int(DateDiff("yyyy", .FormFields("DOB").result, Date)))

and the first column of life expectancies is column 2 hence, offsetting by the .Value of the selected DropDown item selects the appropriate column as the values assigned to the dropdown items correspond to their position in the list.

-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:0a6e9254-88b5-404b-8d4d-3b1f260a7ff4@communitybridge.codeplex.com...

Doug,

The table you got from the Internet is not from 2010, but it works, the only problem is that is not giving me a real result from the table.

I guess i have to specify where to look when you choose your category vs DOB. right?

I will recreate the 2010 table and use it as the Excel Doc.

 

Answer 8

Doug,

I did exactly as you said and i continue having problems. The type of table i want to create will be simple!

 

A               B                    C                  D                  E                          F                                    G

0         White Male     White Female   Black Male   Black Female    Total Population Male       Total Population Female

 

 

100

Now see below my code and see if I'm missing something.

Sub LifeExpectancyCalculation()
'
' LifeExpectancyCalculation Macro
'
'
Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim myarray As Variant
Dim i As Long, j As Long
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
    bstartApp = True
    Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlbook = xlapp.Workbooks.Open("C:\Users\ScorpDevil\Downloads\10s0105(1).xls")
Set xlsheet = xlbook.Worksheets(1)
With ActiveDocument
    i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 2
    j = .FormFields("Category1").DropDown.Value
    .FormFields("LifeExpectancy").Result = xlsheet.Range("A1").Offset(i, j).Value
End With
xlbook.Close
If bstartApp = True Then
    xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
End Sub

Thanks

 

Answer 9

What result do you get?

Does the second row of your spreadsheet contain the "At Birth" Life Expectancies?

The way in which the age  is calculated, a person less than 1 year old has an age in years of 0 and in that case, if the second row contains the "At Birth" Life Expectancies, you would need to have

    i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 1

rather than

    i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 2

Also, for a spreadsheet with the columns that you have, the order of the items in the category dropdown formfield will need to be:

White Male
White Female
Black Male
Black Female
Total Population Male
Total Population Female

-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:7988df75-dc11-4aa6-bb2b-88ac933653f4@communitybridge.codeplex.com...

Doug,

I did exactly as you said and i continue having problems. The type of table i want to create will be simple!

A               B                    C                  D E                          F                                    G

0         White Male     White Female   Black Male   Black Female    Total Population Male       Total Population Female

100

Now see below my code and see if I'm missing something.

Sub LifeExpectancyCalculation()
'
' LifeExpectancyCalculation Macro
'
'
Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim myarray As Variant
Dim i As Long, j As Long
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
    bstartApp = True
    Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlbook = xlapp.Workbooks.Open("C:\Users\ScorpDevil\Downloads\10s0105(1).xls")
Set xlsheet = xlbook.Worksheets(1)
With ActiveDocument
    i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 2
    j = .FormFields("Category1").DropDown.Value
    .FormFields("LifeExpectancy").Result = xlsheet.Range("A1").Offset(i, j).Value
End With
xlbook.Close
If bstartApp = True Then
    xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
End Sub

Thanks

 

Answer 10

Doug,

I finally got it working with the following.

i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 1

Now, the actual calculation is not accurate. It is calculation by years and i need by date, M, years.

Example:

If a person was born in 08/15/1984 is 25 years, but when the above formula run will show 26. How can i change the Offset (i).

I did some research and i came out with same result.

Thanks

 

Answer 11

Don't include the + 1

-- Hope this helps.

Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge

"scorpdevil" wrote in message news:49a37b60-f62e-4c4f-85ac-d925a8bce8b7@communitybridge.codeplex.com...

Doug,

I finally got it working with the following.

i = Int(DateDiff("yyyy", .FormFields("DOB").Result, Date)) + 1

Now, the actual calculation is not accurate. It is calculation by years and i need by date, M, years.

Example:

If a person was born in 08/15/1984 is 25 years, but when the above formula run will show 26. How can i change the Offset (i).

I did some research and i came out with same result.

Thanks

 

Answer 12

Excellent!
 
 
Search for  Age Calculator
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter