Userforms and Dates

D

Derek Johansen

Hey again guys,

I am practicing using a userform to perform custom type filtering based on a
criteria and a date range. The code I am using is below:
Private Sub iSubmit_Click()
Dim StartDate As Date
Dim EndDate As Date
Dim SortBy As String
If IsDate(Me.iStart.Value) = False Then
MsgBox "Please enter a valid START DATE!", vbExclamation, "Error"
Me.iStart.SetFocus
Exit Sub
End If
If IsDate(Me.iEnd.Value) = False Then
MsgBox "Please enter a valid END DATE!", vbExclamation, "Error"
Me.iEnd.SetFocus
Exit Sub
End If
If Me.iSort.Value = "" Then
MsgBox "Please select a valid SORT CRITERIA!", vbExclamation, "Error"
Me.iEnd.SetFocus
Exit Sub
End If
If Me.iEnd.Value < Me.iStart.Value Then
MsgBox "Please enter an END DATE that is after the START DATE!",
vbExclamation, "Error"
Exit Sub
End If
SortBy = Me.iSort.Value
StartDate = Me.iStart.Value
EndDate = Me.iEnd.Value
MsgBox StartDate & " comes before " & EndDate
End Sub

This code works well for me, as long as the date is within the same calendar
year. If I put "2/2/2009" and "2/3/2008" it tells me that the first date
comes before the second date, when it really should have told me to pick a
new end date!

Does anyone have any idea why it's not recognizing the year in the
comparison? I am very new to VB programming and am trying to work hard to
learn, so all help (especially explanations of why) are much needed and
APPRECIATED!!

Thanks a lot,

Derek
 
M

Mishell

You are comparing 2 strings (the text values).
Try comparing the date values.

Mishell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top