Calculating age in a Word form field with VB6

I would like to be able to put a birthdate in to one form field in Word and have the person’s age automatically be entered into another form field. Anybody have any idea how to do this?
Thanks.

I’m pretty sure some tinkering with the DateDiff() function will give you what you need. The general syntax of the function is


DateDiff (interval, date1, date2, [firstdayofweek], [firstweekofyear])

For example,


DateDiff("yyyy", "15 Aug 1980", Now())

returns 27, my age in years (or close enough). You could put the update code in the LostFocus() event of the first form field, after checking that the date is valid using IsDate() if necessary.

The interval parameter tells you how you want to measure the time interval; I’m assuming that years will work. If that won’t the other options are detailed here: MS Access: DateDiff Function

If a bit of experimentation doesn’t work, perhaps I can give more detailed help by knowing exactly what form fields you have, and their names and properties.

Ok, I understand how to get the calculation, but I’m still stuck on pulling the date from the first text box and inserting the calculated answer into the second one. Let’s say the first text form field is called “DOB” and is set to contain a date with the format mmm. d, yyyy, and the second text box is called “age” and is set to contain a number.

Well, IIRC the value property, or something like that, would work.

Using the LostFocus event of DOB, set age.value to be the result of your DateDiff calculation on DOB.

Right. So in the method DOB_Exit, you’d insert the following code:


If IsDate(DOB.Text) Then
        age.Text = DateDiff("yyyy", DOB, Now())
End If

If you have trouble getting to the method, open the form in design mode, select the DOB text field, then right-click on it and click View Code (or double-click on the DOB text field, which does the same thing). From the right-hand drop-down menu, select Exit. You could also put the same code into the DOB_Change method, so it would instantly update when a valid date was entered.

Sorry about the references to LostFocus there; I assumed that the event names would be the same in both VB and VBA, but apparently not.