I'm writing a small app that adds a certain amount of days to a date. I have the user input the date in 3 textboxes - one each for month, date, and year. I need to validate the entire date as a legitimate date, so that if something like month = 2 and date= 31 would be caught as invalid. Here's the code for the calculate menu item:
CODE
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
userDays = Val(txtNumberDays.Text)
userYear = Val(txtYear.Text)
userMonth = Val(txtMonth.Text)
userDate = Val(txtDate.Text)
startDate = DateSerial(userYear, userMonth, userDate)
If IsDate(startDate) Then
Dim newDate As Date = FormatDateTime(DateAdd(DateInterval.Day, userDays, startDate), DateFormat.LongDate)
txtResult.Text = newDate
Else
MessageBox.Show("Date entered is not valid.")
Me.Refresh()
End If
End Sub
This does not catch invalid dates like 2/31/2007. (I have other subroutines which are catching blank entries and numbers like 13 for months...)