Results 1 to 9 of 9
  1. #1
    djcmalvern is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Worcester, UK
    Posts
    9

    Calculate age from Date of Birth


    Using only built-in functions and expression builder how can I create a field in a table that automatically calculates the age of a user after they type in their Date of Birth. Can this be done with a calculated field using the DateDiff() function on the fly? OR, can this be done on a form with a calculated field? I tried this but got lost and could not get the Age field to update the value in the bound table or display in the text box to which I was attaching the expression. So how does this all hang together please? How do you get the calculated value to update into the table and display on the form after updating the Date of Birth field/control?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    VBA code would be required to save value into table.

    Should not save the calculated result into table. Calculate whenever needed.

    The calculation can be in query or textbox. Not sure if Calculated field in table can handle the Datediff function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    On your form have a text box that with a control source of

    =Datediff("yyyy", [birthdate], date())

    As june says, you do not want to store calculated values in your tables. Ever.

    The same formula can be used in a query and given an alias like :

    PersonAge: datediff("yyyy", [birthdate], date())

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by rpeare View Post

    ...=Datediff("yyyy", [birthdate], date())...
    Done like this, DateDiff() is simply subtracting the year of birth from the current year! The answer will only be correct if the person has already had their birthday for the current year!

    Here are three Formulas commonly used for an accurate calculation of age:

    Code:
    DateDiff("yyyy", [DOB], Date) - IIf(Format$(Date, "mmdd") < Format$([ DOB], "mmdd"), 1, 0)


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


    Code:
    DateDiff("m", Me.DOB, Date()) \ 12


    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    djcmalvern is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Worcester, UK
    Posts
    9
    Thanks everyone! Got the idea. Great help.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    shafiq037 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    3
    please clarify that your code is not working in single test table, why? I am using win7 32 bit and access 2010.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    All code given is time tested; either

    1. You've done something incorrectly, possibly using incorrect syntax
    2. You've misspelled a field or table name
    3. You have your code in the wrong place
    4. You don't have the folder holding the file declared as 'Trusted'

    If the folder has been marked as Trusted (does any code run???) you'll need to copy and paste the code you're using in order for us to trouble-shoot it.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 02-12-2013, 03:48 AM
  2. Replies: 7
    Last Post: 01-28-2013, 05:21 PM
  3. Date of birth converter
    By markyboy in forum Forms
    Replies: 1
    Last Post: 05-23-2011, 07:20 AM
  4. Date of Birth Access expression
    By Father John in forum Access
    Replies: 5
    Last Post: 12-02-2010, 10:33 PM
  5. Calculate age from birth date
    By mick in forum Forms
    Replies: 2
    Last Post: 07-03-2010, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums