how to calculate exact age in access....

kiranpanchal22

New member
Local time
Yesterday, 17:38
Joined
Apr 26, 2017
Messages
1
i write this code but dont work....getting error object not found 424 error


Dim year As Date
Dim month As Date
Dim days As Date


year = DateDiff(DateInterval.year, TxtDOB.Text, Now) - 1
month = DateDiff(DateInterval.month, TxtDOB.Text, Now) Mod 12
days = DateDiff(DateInterval.Day, TxtDOB.Text, Now) Mod 30 - 10
TxtAge.Text = year & " Years, " & month & " Months, " & days & "Days"
 
Since you are assuming every month is 30 days and are ignoring leap years, I'm going to assume you're not looking for an exact age and will not open that can of worms.

I can however see two problems with your code.

1. Your variables are set to Date type, whereas you are calculating using Integers. Change all three of those to Integer type.
1. You are using variables called "year" and "month". These are names of functions that already exist in VBA. This is bad for many reasons. Instead it is good practice to prefix your variables with something: I usually use its type. e.g. use "intYear", or "myYear", or "diffYears", etc.
 
This is what I use:

Code:
Public Function GetAge(ByVal DOB As Date)

    GetAge = DateDiff("yyyy", DOB, Date) + (CDate(DatePart("m", DOB) & "/" & DatePart("d", DOB) & "/" & DatePart("yyyy", Date)) > Date)

End Function
 
Access has reserved words--month and year are 2 such words.
Month by itself is NOT a Date; same with Year

Here is a function to calculate Age.

Code:
'---------------------------------------------------------------------------------------
' Procedure : Age
' Author    : Jack (from awf)
' Date      : 06-09-2012
' Purpose   : This routine determines the Age of a Person given their DOB.
' It accounts for the birthday this year (whether passed or not). A second parameter
' Specdate allows you to work from a different Date than today's date.
'If SpecDate is missing, the routine defaults to today's date.
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'--------------------------------------------------------------------------
'
Public Function Age(dteDOB As Date, Optional SpecDate As Variant) As Integer
    Dim dteBase As Date, intCurrent As Date, intEstAge As Integer
10  On Error GoTo Age_Error

20  If IsMissing(SpecDate) Then
30      dteBase = Date
40  Else
50      dteBase = SpecDate
60  End If
70  intEstAge = DateDiff("yyyy", dteDOB, dteBase)
80  intCurrent = DateSerial(year(dteBase), Month(dteDOB), Day(dteDOB))
90  Age = intEstAge + (dteBase < intCurrent)

100 On Error GoTo 0
110 Exit Function

Age_Error:

120 MsgBox "Error " & err.number & "  in line " & Erl & " (" & err.Description & ") in procedure Age of Module AWF_Related"
End Function

Here is an example of calling the function.

Code:
'---------------------------------------------------------------------------------------
' Procedure : TestAge
' Author    : Jack
' Date      : 06-09-2012
' Purpose   : Routine to show the current age of a person given their DOB.
'This accounts for whether their birthday this year has occurred  or not.
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'--------------------------------------------------------------------------
'
Sub TestAge()

    Dim jage As Date

10  On Error GoTo TestAge_Error

20  jage = #4/25/1948#
30  Debug.Print Age(jage)

TestAge_Exit:
40  Exit Sub

TestAge_Error:
50  MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure TestAge of Module AWF_Related"
60  Resume TestAge_Exit

End Sub
 
Posts 2 and 3 were moderated, not sure why.
 

Users who are viewing this thread

Back
Top Bottom