Results 1 to 2 of 2

Thread: Calculating age function

  1. #1
    Guest

    Question

    I am trying to use that function to calculate the age of a person in years, months, and days. I typed this function in a module and would want the result based on Date of Birth displayed in a text box on a form. MY PROBLEM IS ON HOW TO HAVE THE RESULT RESULT DISPLAYED IN A TEXT BOX. I entered the aSns=yer & "years"& mon &"month(s)"& d &"day(s) old." expression in the controlsouse property but unfortunately I am getting a #Name error in the text box. Can someone help me with how how best I can end up with the age in the text box.

    Public Function ExactAge(Date_of_Birth As Variant) As String

    Dim yer As Integer, mon As Integer, d As Integer
    Dim dt As Date
    Dim sAns As String

    If Not IsDate(Date_of_Birth) Then Exit Function
    dt = CDate(Date_of_Birth)
    If dt > Now Then Exit Function

    yer = Year(dt)
    mon = Month(dt)
    d = Day(dt)
    yer = Year(Date) - yer
    mon = Month(Date) - mon
    d = Day(Date) - d

    If Sgn(d) = -1 Then
    d = 30 - Abs(d)
    mon = mon - 1
    End If

    If Sgn(mon) = -1 Then
    mon = 12 - Abs(mon)
    yer = yer - 1
    End If

    sAns = yer & " year(s) " & mon & " month(s) " & d _
    & " day(s) old."

    ExactAge = sAns

    End Function

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I dont understand. Your code seems to work.
    i entered 09/11/1973 into text one and clicked the command
    button. text2's out put was.......27 year(s) 1 month(s) 8 day(s) old.
    So it seems to work????


    Private Sub Command1_Click()
    Dim dob As Variant
    dob = Text1.Text
    ExactAge (dob)
    End Sub



    Public Sub ExactAge(dob As Variant)

    Dim yer As Integer, mon As Integer, d As Integer
    Dim dt As Date
    Dim sAns As String


    dt = CDate(dob)
    If dt > Now Then Exit Sub

    yer = Year(dt)
    mon = Month(dt)
    d = Day(dt)
    yer = Year(Date) - yer
    mon = Month(Date) - mon
    d = Day(Date) - d

    If Sgn(d) = -1 Then
    d = 30 - Abs(d)
    mon = mon - 1
    End If

    If Sgn(mon) = -1 Then
    mon = 12 - Abs(mon)
    yer = yer - 1
    End If

    sAns = yer & " year(s) " & mon & " month(s) " & d _
    & " day(s) old."

    Text2.Text = sAns

    End Sub

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