I have a form made with VB6 and I need to check the date field for future dates.
If the date entered is in the future an error message should show and if it is not, the validation should finish.
I’ve done:
Private Sub txtDate_Validate(Index As Integer, Cancel As Boolean)
If Not IsDate(txtDate(9).Text) Then 'first I check if the data entered is a date
'error message saying the field needs a valid date
Cancel = True
Else
If (txtDate(9).Text > Date) Then 'now I check if the date entered is bigger than today’s date
'error message saying the date is in the future
Cancel = True
Else
Exit Sub
End If
End If
End If
End Sub
This code does not work because
txtDate(9).Text > Date
is always true
Even if I do:
Format(txtDate(9).Text, "dd/mm/yyyy") > Date
is always true too
What can I do to fix this? How can I know if the date entered is in the future?
Thanks!
Use CDate to convert the content of the textbox to a Date.
Can’t stress this enough if it’s got quotes round it, it’s a string.