views:

75

answers:

3

This is the code I have in my form to check if the date the user selected is more than 14 days in advance, or in the past.

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If

It works, but if I select a date more than 14 days ahead it won't show the error message, because of the second IF checking if it is in the past and blanking it.

I really can't think of another way around this other than making another textbox to sit behind the one the user types into, and displaying the second error message to that one.

Anybody have any bright ideas? Thanks :)

+8  A: 

try this

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
jmein
+1  A: 

You are very close! Just place the check in a else if block.

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
codemeit
A: 

Hahaha, can't believe I never thought of using an elseif.

Thanks a lot! :)

Whitey
your welcome :) by the way this should be a comment not an answer to your question. If you would delete this and post as a comment to help clean up the answers that would be appreciated. ;)
jmein