Results 1 to 8 of 8
  1. #1
    mikichi is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    45

    question | age calculate


    hi,
    i think that i will found the answer in the net but i dont.
    the function dateDiff its accurate? i meen that calculate all month with 30 days or it do the abbservation with 28 days month and 31 days?
    thank you!!!
    miki

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    What exactly is your question? If you need info on Datediff() see http://www.techonthenet.com/access/f...e/datediff.php

    If you need a function to calculate Age, here's one

    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 & " (" & Err.Description & ") in procedure Age of Module AWF_Related"
    End Function
    and a routine for testing

    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
       On Error GoTo TestAge_Error
    
    jage = #9/29/1919#
    Debug.Print Age(jage)
    
       On Error GoTo 0
       Exit Sub
    
    TestAge_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure TestAge of Module AWF_Related"
    
    End Sub

  3. #3
    mikichi is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    45
    thank you,
    i just dont understand this line:
    (dteBase < intCurrent)

    what its mean?
    thank you,
    miki

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    It is a means to subtract 1 from intEstAge if the person's Birthday has not yet passed, this year.
    intEstAge = DateDiff("yyyy", dteDOB, dteBase)'<<<this gives the number of years using the difference in years only.

    If the person's Birthday hasn't happened yet this year, you need to subtract 1 from intEstAge.

    (dteBase < intCurrent) is a boolean 0 if False, -1 if True

    Good luck.

  5. #5
    mikichi is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    45
    Quote Originally Posted by orange View Post
    It is a means to subtract 1 from intEstAge if the person's Birthday has not yet passed, this year.
    intEstAge = DateDiff("yyyy", dteDOB, dteBase)'<<<this gives the number of years using the difference in years only.

    If the person's Birthday hasn't happened yet this year, you need to subtract 1 from intEstAge.

    (dteBase < intCurrent) is a boolean 0 if False, -1 if True

    Good luck.
    aaa ok,
    but how i can to calculate with month? like that 22.12?
    i think to use in dateDiff("d",dob,now())/365.25
    but its not accure because not all month is 30 day.
    thank you,
    miki

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I'm not following your question.
    WHAT exactly are you trying to do? Please see my post #2 where I first asked.

    Datediff with days should give you an accurate figure -- you don't have to deal with the number of days in a month.

  7. #7
    mikichi is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    45
    ok, i try to display the age in years and months.
    i.e:if i born in 23/04/1992 my age is: 21.08 - 21 years and 8 mouths.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    No it is not 21.08. It can be 21 years 8 months.

    Try this

    Code:
    Sub AgeMonths()
          Dim YearDiff As Integer
          Dim MnthDiff As Integer
          Dim Adjust As Integer
          Dim AgeWithMonths As String
          Dim Dob As Date
    10       On Error GoTo AgeMonths_Error
    
    20    Dob = #4/24/1992#
    30    If Month(Dob) < Month(Date) Then
    40     Adjust = 0
    50    ElseIf Month(Dob) = Month(Date) And _
                  Day(Dob) <= Day(Date) Then
    60            Adjust = -1
                  
    70    End If
    80    YearDiff = DateDiff("yyyy", Dob, Date) + Adjust
    90    MnthDiff = Month(Date) - Month(Dob)
    100   AgeWithMonths = YearDiff & " " & MnthDiff
    110   Debug.Print AgeWithMonths
    
    120      On Error GoTo 0
    130      Exit Sub
    
    AgeMonths_Error:
    
    140       MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure AgeMonths of Module AWF_Related"
    End Sub
    Good luck

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

Similar Threads

  1. how to calculate hours
    By ultra5219 in forum Queries
    Replies: 2
    Last Post: 05-23-2013, 04:22 PM
  2. Replies: 7
    Last Post: 05-06-2013, 02:43 AM
  3. Calculate Discount
    By ryansox in forum Reports
    Replies: 2
    Last Post: 02-23-2013, 07:47 PM
  4. How to calculate?
    By cap.zadi in forum Queries
    Replies: 1
    Last Post: 11-09-2011, 07:29 AM
  5. calculate value from two tables
    By victor in forum Programming
    Replies: 3
    Last Post: 08-20-2010, 12:13 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