Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Age in a form 1

Status
Not open for further replies.

EvilTwinOfAtrus

Technical User
Dec 30, 2002
56
GB
Hi

I have a form that has a date of birth field in it "DateOfBirth". When the date is entered I want a text box to work out the age of this person. For example, if I entered 19/07/1985 it would return "18".

Thanks in advance for your help.

-EToA
 
Age = DateDiff("d", DOB, Now())/365

Gives your pretty acurate. I may get the dates around wrong, if you get a negative result swap them. If you wanna account for leap years, you can divide by 365.25.

Try that.
Shadow
 
That works most of the time, but may not be accurate right around the birthdate anniversary. Many threads here have explored the age topic...

For absolute accuracy, I recommend:

Code:
Age=DateDiff("yyyy",[Bdate],Date())+Int(Format(Date(),"mmdd")<Format([Bdate],"mmdd"))

HTH,
Bob [morning]
 
How are ya EvilTwinOfAtrus . . . . .

Here's my own routine. Used in many DayCare Center DB's I've designed:
Code:
[blue]Public Function AgeCalc(BirthDay As Date) As String
   Dim qNow As Date, Years As Long, Months As Long
   Dim rtnYears As String, rtnMonths As String
   
   Const Yr As Integer = 12
   qNow = Int(Now())
   Months = DateDiff("m", BirthDay, qNow)
   
   If (Months Mod Yr = 0) And (Day(qNow) < Day(BirthDay)) Then Months = Months - 1
   [green]'If number of months is a multiple of 12, and the current day is less than the day
   'of the birthday, subtract 1 month. [purple][b]This allows the birthday to change when is arrives[/b][/purple].
   'LeapYear is also covered in this way![/green]
   
   'Format Years
   Years = Int(Months / Yr)
   If Years = 1 Then
      rtnYears = Years & "year"
   ElseIf Years > 1 Then
      rtnYears = Years & "years"
   End If
   
   'Format Months
   Months = Months - (Years * Yr)
   If Months = 1 Then
      rtnMonths = Months & "month"
   ElseIf Months > 1 Then
      rtnMonths = Months & "months"
   End If
      
   'Determine what to return
   If (rtnYears = "") And (rtnMonths = "") Then
      AgeCalc = "AgeErr!"
   ElseIf (rtnYears = "") And (rtnMonths <> "") Then
      AgeCalc = rtnMonths
   ElseIf (rtnYears <> "") And (rtnMonths = "") Then
      AgeCalc = rtnYears
   Else
      AgeCalc = rtnYears & " " & rtnMonths
   End If

End Function[/blue]
The return value is a string of Years & Months such as:
[purple]12years 5months[/purple]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top