Results 1 to 28 of 28

Thread: Age Calculation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    Angry Age Calculation

    Hi everyone
    trying to write a program to calculate and dispaly on a msgbox the age of an input birthdate and the current date. Tried this code below but not giving correct age.


    StudentDate = cboDate.Text & "/" & cboMonth.Text & "/" lblYear.Caption
    StudentAge = DateDiff("yyyy", StudentDate, Now())
    If Month(Now) < cboMonth.Text Then
    StudentAge = StudentAge -1
    ElseIf Month(Now) = cboMonth.Text Then
    If Day(Now) < cboDate.Text Then
    StudentAge = StudentAge -1
    End If
    End If

    This code makes the age correct if month is after the current month, but any date before the current month is a year out.

    Thanks Tink

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Are u sure?
    Seemed to work ok for me... can u post some examples of errors. Maybe it is to do with mm/dd vs dd/mm
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calc

    hi stuart thanks for the reply
    try this
    date 2/8/66 should be 35 years old
    coming back as 34 years old

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    I amended ur code to save me setting up all the combo's etc and it comes out correctly as 35. Maybe u should put a stop in place and see if the values are being translated correctly as numbers. Maybe add a Val() around each cbo...text variable.
    Regards
    Stuart

    VB Code:
    1. Private Sub Command1_Click()
    2. studentdate = #2/8/1966#
    3. studentage = DateDiff("yyyy", studentdate, Now())
    4. If Month(Now) < Month(studentdate) Then
    5. studentage = studentage - 1
    6. ElseIf Month(Now) = Month(studentdate) Then
    7. If Day(Now) < Day(studentdate) Then
    8. studentage = studentage - 1
    9. End If
    10. End If
    11. Debug.Print studentage
    12. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculator

    hi stuart
    thanks for the reply, if i type in the birthdate as you have, it does give the correct age but im working from a variable input date that the user types in
    and im still getting the wrong age
    im lost on this one.
    tink

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Did u try adding Val around the combo box text? That seems to work ok for me. Is prob a good idea to load the month, day and year into variables rather than keep using val over and over.
    Regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     StudentDate = cboDate.Text & "/" & cboMonth.Text & "/" & lblYear.Caption
    3.     studentage = DateDiff("yyyy", StudentDate, Now())
    4.     If Month(Now) < Val(cboMonth.Text) Then
    5.         studentage = studentage - 1
    6.     ElseIf Month(Now) = Val(cboMonth.Text) Then
    7.         If Day(Now) < Val(cboDate.Text) Then
    8.             studentage = studentage - 1
    9.         End If
    10.     End If
    11.     Debug.Print studentage
    12. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    Wink age calculation

    Hi stuart,
    thanks for your help again ,it worked a treat
    sorry to be a pain again but any idea how i can incorporate code for a leap year, gettin an error when typing in 29/2/??

    thanks Tink

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Just check the date using IsDate
    eg
    Isdate("29/2/1996") = true
    and
    IsDate("29/2/1997") = false
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculation

    hi stuart
    cos i only a vb beginner, and a pushy one at that :-)
    how do i code that ?

    thanks again Tink

  10. #10
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    lol thats ok
    When u get ur date into a date variable u simple check it
    eg
    VB Code:
    1. If IsDate(StudentDate) = True then 'u dont need to say *= True*
    2.       'do code for date being ok
    3. Else
    4.       'woops.. notify that date is wrong or just change to 28 th
    5. End If
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculation

    hi stuart me again
    how do u get your date into a date variable
    gettin very confused.

  12. #12
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    ok
    at top of form
    Dim studentDate as Date

    In your routine that u posted already
    instead of
    StudentDate = cboDate.Text & "/" & cboMonth.Text & "/" & lblYear.Caption

    do
    StudentDate = cdate(cboDate.Text & "/" & cboMonth.Text & "/" & lblYear.Caption) 'altho u dont really need

    and thats it.... sorry, i had just assumed that u had declared ur other variables ... it is a good idea to declare them all... look up Dim and Redim in help
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculation

    hi stuart
    done that but gettin an error sayin IsDate ..(Expected Array)


    tink

  14. #14
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi again
    can u post all ur code so i can see what is wrong
    put [vbcode] before ur code and [/vbcode] after it so that it is formatted and easier to read
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calc

    hi stuart
    Attached Files Attached Files

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calc

    try this
    Attached Files Attached Files

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculation

    sorry stuart dont think they worked i will probably have to type it
    not good at sending things

    tink

  18. #18
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    no dont type it
    it came thru ok as txt file
    I can see one thing straight off. IsDate is a function and so u dont Dim it. but let me check rest of code and get back to u soon
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  19. #19
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    ok try this
    I havent tested cos i would have to set up combos etc
    but it should work. If not just let me know and i will set up a proper test and get back to you
    Regards
    Stuart
    VB Code:
    1. Private Sub cmdProfile_Click()
    2.     Dim Message As String
    3.     Dim StudentAge As Integer 'Changed from String to Integer (number)
    4.     Dim StudentDate As Date
    5.     Dim Grade As String 'added
    6.  
    7.     'Remove this ..... Dim IsDate As Variant
    8.  
    9.     'Use & for strings     ...... remove vbcr
    10.     'Message = txtName.Text & " is a student in the" & lblGrade.Caption
    11.     'You dont need this first one cos u are setting later
    12.  
    13.     Select Case lbl_grade 'Select case is easier to follow for multi ifs
    14.         Case "1": Grade = "First"
    15.         Case "2": Grade = "Second"
    16.         Case "3": Grade = "Third"
    17.         Case "4": Grade = "Fourth"
    18.         Case "5": Grade = "Fifth"
    19.         Case "6": Grade = "Sixth"
    20.     End Select
    21.     Message = txtName.Text & " is a student in the " & Grade & "Grade. "
    22.  
    23.     'Calculate students age
    24.     StudentDate = cboDate.Text & "/" & cboMonth.Text & "/" & lblYear.Caption
    25.     If IsDate(StudentDate) Then
    26.         StudentAge = DateDiff("yyyy", StudentDate, Now())
    27.         If Month(Now) < Val(cboMonth.Text) Then
    28.             StudentAge = StudentAge - 1
    29.         ElseIf Month(Now) = Val(cboMonth.Text) Then
    30.             If Day(Now) < Val(cboDate.Text) Then
    31.                 StudentAge = StudentAge - 1
    32.             End If
    33.         End If
    34.     Else
    35.         MsgBox "Please enter valid date"
    36.         Exit Sub
    37.     End If
    38.  
    39.     If optMale.Value = True Then
    40.         Message = Message & "He is " & StudentAge & " Years Old"
    41.     Else
    42.         Message = Message & "She is " & StudentAge & " Years Old"
    43.     End If
    44.     MsgBox Message, vbOKOnly, "Student Profile"
    45. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calculator

    hi stuart
    tried your code but no grade message
    no age message
    managed to get the code working, it will accept leap years
    but no message box when not a leap year eg:- 29/2/ 97
    how do i get it to display a msgbox when an invalid date is input?

  21. #21
    New Member beachbun's Avatar
    Join Date
    Sep 2001
    Location
    Wollongong, NSW, Australia
    Posts
    7
    ok try this
    I havent tested cos i would have to set up combos etc
    but it should work. If not just let me know and i will set up a proper test and get back to you
    Regards
    Stuart
    VB Code:
    1. Private Sub cmdProfile_Click()
    2.     Dim Message As String
    3.     Dim StudentAge As Integer 'Changed from String to Integer (number)
    4.     Dim StudentDate As Date
    5.     Dim Grade As String 'added
    6.  
    7.     'Remove this ..... Dim IsDate As Variant
    8.  
    9.     'Use & for strings     ...... remove vbcr
    10.     'Message = txtName.Text & " is a student in the" & lblGrade.Caption
    11.     'You dont need this first one cos u are setting later
    12.  
    13.     Select Case lbl_grade 'Select case is easier to follow for multi ifs
    14.         Case "1": Grade = "First"
    15.         Case "2": Grade = "Second"
    16.         Case "3": Grade = "Third"
    17.         Case "4": Grade = "Fourth"
    18.         Case "5": Grade = "Fifth"
    19.         Case "6": Grade = "Sixth"
    20.     End Select
    21.     Message = txtName.Text & " is a student in the " & Grade & "Grade. "
    22.  
    23.     'Calculate students age
    24.     StudentDate = cboDate.Text & "/" & cboMonth.Text & "/" & lblYear.Caption
    25.     If IsDate(StudentDate) Then
    26.         StudentAge = DateDiff("yyyy", StudentDate, Now())
    27.         If Month(Now) < Val(cboMonth.Text) Then
    28.             StudentAge = StudentAge - 1
    29.         ElseIf Month(Now) = Val(cboMonth.Text) Then
    30.             If Day(Now) < Val(cboDate.Text) Then
    31.                 StudentAge = StudentAge - 1
    32.             End If
    33.         End If
    34.     Else
    35.         MsgBox "Please enter valid date"
    36.         Exit Sub
    37.     End If
    38.  
    39.     If optMale.Value = True Then
    40.         Message = Message & "He is " & StudentAge & " Years Old"
    41.     Else
    42.         Message = Message & "She is " & StudentAge & " Years Old"
    43.     End If
    44.    
    45.     MsgBox Message, vbOKOnly, "Student Profile"
    46. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calc

    hi stuart still not working
    I leave you alone b4 u get fed up with me

    thanks thanks thanks again 4 all your help :-) i wouldn't have got this far without u xxx

  23. #23
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Try this on a new project and then adapt to ur specific app. It works as is but does not include error checking or proper layout etc. The pic shows the names of each control and the program running
    Regards
    Stuart
    VB Code:
    1. Private Sub Form_Load()
    2.     'Fill combos with dates, months
    3.     For x = 1 To 12
    4.         cboMonth.AddItem x
    5.     Next
    6.     For x = 1 To 31
    7.         cboDate.AddItem x
    8.     Next
    9.     cboDate.ListIndex = 0
    10.     cboMonth.ListIndex = 0
    11. End Sub
    12.  
    13. Private Sub cmdProfile_Click()
    14.     Dim Message As String
    15.     Dim StudentAge As Integer
    16.     Dim StudentDate As Date
    17.     Dim Grade As String
    18.    
    19.     'DateSerial will produce date and not create an
    20.     'error for wrong dates so 29/2/1997 becomes 1/3/1997
    21.     StudentDate = DateSerial(txtyear.Text, cboMonth.Text, cboDate.Text)
    22.     If IsDate(StudentDate) Then
    23.         'Your code for age
    24.         StudentAge = DateDiff("yyyy", StudentDate, Now())
    25.         'Changed these to Month() and Day() in case Dateserial modifies date
    26.         If Month(Now) < Month(StudentDate) Then
    27.             StudentAge = StudentAge - 1
    28.         ElseIf Month(Now) = Month(StudentDate) Then
    29.             If Day(Now) < Day(StudentDate) Then
    30.                 StudentAge = StudentAge - 1
    31.             End If
    32.         End If
    33.     Else
    34.         MsgBox "Please enter valid date"
    35.         Exit Sub
    36.     End If
    37.  
    38.     'I changed this to a textbox so that u can change
    39.     Select Case txtGrade.Text 'Select case is easier to follow for multi ifs
    40.         Case "1": Grade = "first"
    41.         Case "2": Grade = "second"
    42.         Case "3": Grade = "third"
    43.         Case "4": Grade = "fourth"
    44.         Case "5": Grade = "fifth"
    45.         Case "6": Grade = "sixth"
    46.         Case Else: Grade = "far too old"
    47.     End Select
    48.     'Convert name to proper case eg john becomes John
    49.     Message = StrConv(txtName.Text, vbProperCase) & " is a student in the " & Grade & " grade. "
    50.  
    51.     If optMale.Value = True Then
    52.         Message = Message & "He is " & StudentAge & " Years Old"
    53.     Else
    54.         Message = Message & "She is " & StudentAge & " Years Old"
    55.     End If
    56.     MsgBox Message, vbOKOnly, "Student Profile"
    57. End Sub

    Attached Images Attached Images  
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Location
    cornwall, England
    Posts
    110

    age calc

    hi Stuart, thanks for all the time you've taken over this i really am gratefull. thanks for the warning about the last post
    why do people do things like that
    i did add that code , but luckily i dont think it did any damage , everything seems to be working ok.
    thanks for the code u sent its great.
    (here we go) big smiles:-) but i really need an msgbox to appear if a incorrect date is input eg:- 29/2/1997 , is it possible to do that?

    very pushy :-) tink

  25. #25
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    LOL
    Ok, I am a conscientious objector to message boxes but if you insist...
    VB Code:
    1. 'DateSerial will produce date and not create an
    2.     'error for wrong dates so 29/2/1997 becomes 1/3/1997
    3.     StudentDate = DateSerial(txtyear.Text, cboMonth.Text, cboDate.Text)
    4.  
    5. '**** ADD BELOW IN HERE ****
    6.     'If the month changes then the date was obv wrong
    7.     If Val(cboMonth.Text) <> month(StudentDate) then
    8.           MsgBox "Invalid Date", vbOKOnly, "Woops"
    9.           Exit Sub
    10.      End If
    11. '**** ADD ABOVE IN HERE ****
    12.    
    13.     If IsDate(StudentDate) Then
    14.         'Your code for age
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  26. #26
    sql_lall
    Guest
    Can't you just cut out the:
    If Month(Now) < cboMonth.Text Then
    StudentAge = StudentAge -1
    part?

  27. #27
    Hyperactive Member deane034's Avatar
    Join Date
    May 2001
    Location
    Sri Lanka
    Posts
    485

    Red face why not dadtedif?

    u should have posted this in general vb section! that way you would have gotten answres quick, any way why didn't you dudes use the datediff() function it to me it seems the most suitable

  28. #28
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274

    Re: why not dadtedif?

    Originally posted by deane034
    u should have posted this in general vb section! that way you would have gotten answres quick, any way why didn't you dudes use the datediff() function it to me it seems the most suitable
    Were u smoking something when u posted this? The topic was answered like 2 weeks ago and Datediff was used
    VB Code:
    1. Private Sub cmdProfile_Click()
    2. ...........................................
    3.     'DateSerial will produce date and not create an
    4.     'error for wrong dates so 29/2/1997 becomes 1/3/1997
    5.     StudentDate = DateSerial(txtyear.Text, cboMonth.Text, cboDate.Text)
    6.     If IsDate(StudentDate) Then
    7.         'Your code for age
    8.         StudentAge = [b]DateDiff[/b]("yyyy", StudentDate, Now())
    9. '.......................................
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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