how do i calculate an age with DOB?

techexpressinc

Registered User.
Local time
Today, 18:32
Joined
Nov 26, 2008
Messages
185
I have a DOB on the database.
Table = Members
Field = DOB
I seen this as a formula - “AgeCalcField: AgeInYears([DOBField], Date())” -

Within my query I do this
I click the field box
and coded an expression as listed here and get a syntax error:
“AgeCalcField: AgeInYears([Members:DOB], Date())”

Can someone help in getting the age on my query working?
Thanks a lot - Russ @ scaninc.org
 
AgeInYears is not a default access function but rather a self build function.
This function you need to find if you want to use it.

I suggest you search this forum for Date of Birth and find it....
 
You are right the field with the simley is "members:dob" why it displayed has a simley is beyond me as usual.
Thx Rus
 
Do you have the module: 'AgeInYears' stored in your database?
 
You are right the field with the simley is "members:dob" why it displayed has a simley is beyond me as usual.
Thx Rus

You have to select Disable smilies in text in the options on the post submittal, is that a word?, page. : D without a space is :D


Brian
 
I do not have the module: 'AgeInYears' stored in your database? - if needed how would i add it ?
 
Heres one that you can use:

Code:
- Calculating a person's age in years, months and days
  

To accurately calculate a person's age in years, months and days, and (optionally) return that age in words, use the following syntax:

    AgeYMD(DateSerial(1956,11,5), Date(), True, True, True)

Public Function AgeYMD(DOB As Date, today As Date, Optional WithMonths As Boolean = False, _
    Optional WithDays As Boolean = False, Optional DisplayWithWords As Boolean = False) As Variant
    'Author:    © Copyright 2001 Pacific Database Pty Limited
    '           Graham R Seach [email]gseach@pacificdb.com.au[/email]
    '           Phone: +61 2 9872 9594  Fax: +61 2 9872 9593
    '
    '           You may freely use and distribute this code
    '           with any applications you may develop, on the
    '           condition that the copyright notice remains
    '           unchanged, and intact as part of the code. You
    '           may not sell or publish this code in any form
    '           without the express written permission of the
    '           copyright holder.
    '
    'Description:   This function calculates a person's age,
    '               given their date of birth, and a second date.
    '
    'Inputs:    DOB:        The person's date of birth
    '           Today:      The second date (ostensibly today)
    '           WithMonths: Boolean - If True, displays months
    '           DisplayWithWords:   Boolean - If True, displays
    '                               (ie: years / months)
    '
    'Outputs:   On error: Null
    '           On no error: Variant containing person's age in
    '               years, months and days (if selected).
    '               If DisplayWithWords = False:
    '                   Months and days, if selected, are shown
    '                   to the right of the decimal point, but
    '                   are the actual number of months and days,
    '                   not a fraction of the year. For example,
    '                   44.11.03 = 44 years 11 months and 3 days.
    '               If DisplayWithWords = True:
    '                   Output example: "44 years 11 months 3 days",
    '                   except where months = 0, in which case, no
    '                   months are shown.
    
    On Error GoTo AgeYMD_ErrorHandler
    
    Dim iYears As Integer
    Dim iMonths As Integer
    Dim iDays As Integer
    Dim dTempDate As Date
    
    'Check that the dates are valid
    If Not (IsDate(DOB)) Or Not (IsDate(today)) Then
        DoCmd.Beep
        MsgBox "Invalid date.", vbOKOnly + vbInformation, "Invalid date"
        GoTo AgeYMD_ErrorHandler
    End If
    
    'Check that DOB < Today
    If DOB > today Then
        DoCmd.Beep
        MsgBox "Today must be greater than DOB.", _
            vbOKOnly + vbInformation, "Invalid date position"
        GoTo AgeYMD_ErrorHandler
    End If
    
    iYears = DateDiff("yyyy", DOB, today) - _
        IIf(DateAdd("yyyy", DateDiff("yyyy", DOB, today), DOB) > today, 1, 0)
    dTempDate = DateAdd("yyyy", iYears, DOB)
    
    If WithMonths Then
        iMonths = DateDiff("m", dTempDate, today) - _
            IIf(DateAdd("m", iMonths, DateAdd("yyyy", iYears, DOB)) > today, 1, 0)
        dTempDate = DateAdd("m", iMonths, dTempDate)
    End If
    
    If WithDays Then
        iDays = today - dTempDate
    End If
    
    'Format the output
    If DisplayWithWords Then
        'Display the output in words
        AgeYMD = IIf(iYears > 0, iYears & " year" & IIf(iYears <> 1, "s ", " "), "")
        AgeYMD = AgeYMD & IIf(WithMonths, iMonths & " month" & IIf(iMonths <> 1, "s ", " "), "")
        AgeYMD = Trim(AgeYMD & IIf(WithDays, iDays & " day" & IIf(iDays <> 1, "s", ""), ""))
    Else
        'Display the output in the format yy.mm.dd
        AgeYMD = Trim(iYears & IIf(WithMonths, "." & Format(iMonths, "00"), "") _
            & IIf(WithDays, "." & Format(iDays, "00"), ""))
    End If
    
Exit_AgeYMD:
    Exit Function
    
AgeYMD_ErrorHandler:
    AgeYMD = Null
    Resume Exit_AgeYMD
End Function
 
Its not a module but a function that is missing, the function is stored in a module which must have a different name, use the default Module1 if this is the only function you have.

A simple function is below, its error checking merely exits , you may want to do more.

Brian


Public Function Ageinyears(dtDOB As Variant, dtDOC As Variant) As Integer

Dim dtBDay As Date
If Not IsDate(dtDOB) Or Not IsDate(dtDOC) Then Exit Function
dtBDay = DateSerial(Year(dtDOC), Month(dtDOB), Day(dtDOB))
ageinyears = DateDiff("yyyy", dtDOB, dtDOC) + (dtBDay > dtDOC)

End Function
 
I meant to add that as far as I thought a missing function would give an Undefined Function error, not a syntax error, maybe you have both.

Brian
 
Ok i am getting closer... How do I store the above function in my DB and use it?
 
Sorry for any confusion I caused, don't know why I typed 'module' instead of 'function'
 
From DB objects select modules, you will see the default Module1 on the right select it in design view and copy and paste the code into it.

You use the function as you were attempting to in the earlier post.

Brian
 
Slowly getting there I am trying this in my expression and getting a syntax error now;
"AgeYMD [(Members.DOB) (Stays.Start Date)]"
I added the function i think:
Under the objects - Modules - I have AgeYMD.
Can I get some more help - thanks. Russ
 
AgeYMD [(Members.DOB) (Stays.Start Date)] Should be
AgeYMD([Members].[DOB], [Stays].[Start Date])

Brian
 
AgeInYears is not a default access function but rather a self build function.
This function you need to find if you want to use it.

I suggest you search this forum for Date of Birth and find it....

Once again I seem to be invisible?
 
I installed one "function" for this yesterday. This is i.e. like VB. Can you gave me instructions on how to use it.

Attached a screen print of the "function".
 
Brian showed you (more or less) how, also your screeny is missing.
 
My screen image thingy was too big. So, I reattached with it. I have a module added I think to do the code. I just do not know how to use it.
 

Users who are viewing this thread

Back
Top Bottom