Results 1 to 16 of 16

Thread: Calculate Age

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Question Calculate age

    What is the querry to show the age of a person (Year, Month, day) in any given date based on Birth date input.

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Calculate age

    Try searching...someone asked the same question recently
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate age

    Thanks, do you know the query?

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Calculate age

    No, try searching...it was answered
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Calculate age

    Here is the code:
    VB Code:
    1. 'A function which accepts date of birth from and returns age in exact years, months and days
    2. Public Function ExactAge(BirthDate As Variant) As String
    3.     Dim yer As Integer, mon As Integer, d As Integer
    4.     Dim dt As Date
    5.     Dim sAns As String
    6.  
    7.     If Not IsDate(BirthDate) Then Exit Function
    8.     dt = CDate(BirthDate)
    9.     If dt > Now Then Exit Function
    10.  
    11.     yer = Year(dt)
    12.     mon = Month(dt)
    13.     d = Day(dt)
    14.     yer = Year(Date) - yer
    15.     mon = Month(Date) - mon
    16.     d = Day(Date) - d
    17.  
    18.     If Sgn(d) = -1 Then
    19.         d = 30 - Abs(d)
    20.         mon = mon - 1
    21.     End If
    22.  
    23.     If Sgn(mon) = -1 Then
    24.         mon = 12 - Abs(mon)
    25.         yer = yer - 1
    26.     End If
    27.  
    28.     sAns = yer & " year(s) " & mon & " month(s) " & d & " day(s) old."
    29.     ExactAge = sAns
    30. End Function
    CS

  6. #6

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate age

    sorry I am new with this forum, I shall do it

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calculate Age

    You would use any of the date functions like DateDiff for example.
    What Office App are you using?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate Age

    I am using Access2000, try it DateDiff does not work.
    Say someone is born in May 10, 2000, I want the query to tell me 6 y, 1m, 3 d

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calculate Age

    Yes, it works but you need to build your format if you want it like that. Pass one call for the years and then another for the months from birth month to current date then again for days, etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate Age

    Could you post an example I am not sure if I understand what you mean by [Pass one call]

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Calculate Age

    VB Code:
    1. Function GetAge(BirthDate As Date) As String
    2. Dim dNow As Date
    3. Dim lTemp As Long
    4. Dim sTemp As String
    5.    
    6.     dNow = Now()
    7.    
    8.     '---------------------------------------
    9.     ' Year Part
    10.     '---------------------------------------
    11.     lTemp = DateDiff("yyyy", BirthDate, dNow)
    12.    
    13.     If lTemp = 1 Then
    14.         sTemp = CStr(lTemp) & " Year, "
    15.     Else
    16.         sTemp = CStr(lTemp) & " Years, "
    17.     End If
    18.    
    19.    
    20.     BirthDate = DateAdd("yyyy", lTemp, BirthDate)
    21.    
    22.    
    23.     '---------------------------------------
    24.     ' Month Part
    25.     '---------------------------------------
    26.     lTemp = DateDiff("m", BirthDate, dNow)
    27.    
    28.     If lTemp = 1 Then
    29.         sTemp = sTemp & CStr(lTemp) & " Month, "
    30.     Else
    31.         sTemp = sTemp & CStr(lTemp) & " Months, "
    32.     End If
    33.    
    34.     BirthDate = DateAdd("m", lTemp, BirthDate)
    35.    
    36.    
    37.     '---------------------------------------
    38.     ' Day Part
    39.     '---------------------------------------
    40.     lTemp = DateDiff("d", BirthDate, dNow)
    41.    
    42.     If lTemp = 1 Then
    43.         sTemp = sTemp & CStr(lTemp) & " Day"
    44.     Else
    45.         sTemp = sTemp & CStr(lTemp) & " Days"
    46.     End If
    47.    
    48.     '---------------------------------------
    49.     ' Output
    50.     '---------------------------------------
    51.     GetAge = sTemp
    52.    
    53. End Function
    Last edited by DKenny; Jun 13th, 2006 at 01:16 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate Age

    thank you very much I will try it

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calculate Age

    Just something like this...
    VB Code:
    1. Dim sAge As String
    2. sAge = DateDiff("yyyy", "5/10/2000", Date) & " years"
    3. MsgBox sAge
    Edit: Should have refreshed.

    Note: It will return only the years and not partials.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Calculate Age

    The date field is available from the list of individuales that are in the database and we do not want to enter their date of birth eveytime we want to see their age.

  16. #16
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Calculate Age

    OK, so just add this custom function to the DB and then call it in a query, passing the DOB of the current record.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width