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.
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
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
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
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/??
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
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
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
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:
Private Sub cmdProfile_Click()
Dim Message As String
Dim StudentAge As Integer 'Changed from String to Integer (number)
Dim StudentDate As Date
Dim Grade As String 'added
'Remove this ..... Dim IsDate As Variant
'Use & for strings ...... remove vbcr
'Message = txtName.Text & " is a student in the" & lblGrade.Caption
'You dont need this first one cos u are setting later
Select Case lbl_grade 'Select case is easier to follow for multi ifs
Case "1": Grade = "First"
Case "2": Grade = "Second"
Case "3": Grade = "Third"
Case "4": Grade = "Fourth"
Case "5": Grade = "Fifth"
Case "6": Grade = "Sixth"
End Select
Message = txtName.Text & " is a student in the " & Grade & "Grade. "
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?
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:
Private Sub cmdProfile_Click()
Dim Message As String
Dim StudentAge As Integer 'Changed from String to Integer (number)
Dim StudentDate As Date
Dim Grade As String 'added
'Remove this ..... Dim IsDate As Variant
'Use & for strings ...... remove vbcr
'Message = txtName.Text & " is a student in the" & lblGrade.Caption
'You dont need this first one cos u are setting later
Select Case lbl_grade 'Select case is easier to follow for multi ifs
Case "1": Grade = "First"
Case "2": Grade = "Second"
Case "3": Grade = "Third"
Case "4": Grade = "Fourth"
Case "5": Grade = "Fifth"
Case "6": Grade = "Sixth"
End Select
Message = txtName.Text & " is a student in the " & Grade & "Grade. "
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:
Private Sub Form_Load()
'Fill combos with dates, months
For x = 1 To 12
cboMonth.AddItem x
Next
For x = 1 To 31
cboDate.AddItem x
Next
cboDate.ListIndex = 0
cboMonth.ListIndex = 0
End Sub
Private Sub cmdProfile_Click()
Dim Message As String
Dim StudentAge As Integer
Dim StudentDate As Date
Dim Grade As String
'DateSerial will produce date and not create an
'error for wrong dates so 29/2/1997 becomes 1/3/1997
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?
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
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:
Private Sub cmdProfile_Click()
...........................................
'DateSerial will produce date and not create an
'error for wrong dates so 29/2/1997 becomes 1/3/1997