Results 1 to 7 of 7
  1. #1
    wes9659 is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2014
    Location
    Dover, Ohio
    Posts
    88

    Setting Code to Simple Numeric Age

    Out of curiosity just for learning if I wanted this particular code which works perfectly to generate Age simple in years i.e. 56. What changes would I need to change? This is a public function I currently use. I simply want it to give me a numeric age only. Rather than this which current code generates the following example: 93 years, 3 months and 30 days. I want to compare and contrast both and see what is different I learn easier that way. Thanks everyone for your help.

    Public Function FindAge(DOB As Date, CalcDate As Date) As String
    Dim intYears As Integer, intMonths As Integer, intDays As Integer
    intMonths = DateDiff("m", DOB, CalcDate)


    intDays = DateDiff("d", DateAdd("m", intMonths, DOB), CalcDate)
    If intDays < 0 Then
    intMonths = intMonths - 1
    intDays = DateDiff("d", DateAdd("m", intMonths, DOB), CalcDate)
    End If
    intYears = intMonths \ 12
    intMonths = intMonths Mod 12

    FindAge = intYears & " year" & IIf(intYears = 1, "", "s") _
    & ", " & intMonths & " month" & IIf(intMonths = 1, "", "s") _
    & " and " & intDays & " day" & IIf(intDays = 1, "", "s")
    End Function





  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Don't really need VBA. An expression in query or textbox can return numeric age.

    How much precision do you want?

    DateDiff("yyyy", [datefield], Now())

    will return 0 years for an infant of less than 1 year old but will return 1 year for a toddler of 18 months.


    More about DateDiff http://office.microsoft.com/en-us/ac...001228811.aspx
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Could be as simple as
    Code:
    Public Function FindAge(DOB As Date, CalcDate As Date) As String
        Dim intYears As Integer
    
        intYears = DateDiff("yyyy", DOB, CalcDate) 
        FindAge = intYears & " year" & IIf(intYears = 1, "", "s") _
    
    End Function
    But if the birth date is Nov and the current month is Aug, the person will be reported 1 year older.

    I use
    Code:
    Function GetAge(varDOB As Variant, Optional varAsOf As Variant) As Variant
       'Purpose:   Return the Age in years.
       'Arguments: varDOB = Date Of Birth
       '           varAsOf = the date to calculate the age at, or today if missing.
       'Return:    Whole number of years.
       Dim dtDOB As Date
       Dim dtAsOf As Date
       Dim dtBDay As Date  'Birthday in the year of calculation.
    
       GetAge = Null          'Initialize to Null
    
       'Validate parameters
       If IsDate(varDOB) Then
          dtDOB = varDOB
    
          If Not IsDate(varAsOf) Then  'Date to calculate age from.
             dtAsOf = Date
          Else
             dtAsOf = varAsOf
          End If
    
          If dtAsOf >= dtDOB Then      'Calculate only if it's after person was born.
             dtBDay = DateSerial(Year(dtAsOf), Month(dtDOB), Day(dtDOB))
             GetAge = DateDiff("yyyy", dtDOB, dtAsOf) + (dtBDay > dtAsOf)
          End If
       End If
    End Function

  4. #4
    wes9659 is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2014
    Location
    Dover, Ohio
    Posts
    88
    Code works beautifully I do see how by making those changes can alter the output. Hope everyone has a great day. Once again thank you for teaching me Access and VBA coding.

  5. #5
    azhar2006's Avatar
    azhar2006 is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    528
    Thank you ssanfu
    How to Add Function GetAge text box on the form
    Thanks in advance

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Call function in textbox ControlSource.

    =GetAge([DOB field])
    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.

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

    But your example had an additional parameter (as does mine). So use:

    =Getage(DOB, CalcDate)

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

Similar Threads

  1. Setting Report Height In Code
    By ccordner in forum Reports
    Replies: 1
    Last Post: 12-07-2012, 10:26 PM
  2. Setting Up DB with numeric values
    By dashingirish in forum Access
    Replies: 4
    Last Post: 02-17-2012, 03:41 PM
  3. Replies: 10
    Last Post: 01-04-2012, 01:43 PM
  4. Setting up a simple database on a network
    By crunch in forum Access
    Replies: 10
    Last Post: 07-12-2010, 01:24 PM
  5. Replies: 8
    Last Post: 02-24-2010, 01:49 PM

Tags for this Thread

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