ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Date Range problems (https://www.excelbanter.com/excel-programming/333679-date-range-problems.html)

Simon Shaw

Date Range problems
 
Hi,

I have a form with two textboxes for entering a date range. When the user
clicks OK on the form, i validate that the texboxes are dates... this works
fine... then I want to check that the from date is not greater than the to
date, but it is not working consitently. My message is returning that my from
date is greater that my to date even when it isn't. sample code:

If Not IsDate(tbxFrom_Date.Value) Then
MsgBox "The value entered in the From Date field is not a valid Date"
tbxFrom_Date.SetFocus
ElseIf Not IsDate(tbxTo_Date.Value) Then
MsgBox "The value entered in the To Date field is not a valid Date"
tbxTo_Date.SetFocus
ElseIf tbxFrom_Date tbxTo_Date Then
MsgBox "The From Date is greater than the To Date. Please change. "
& _
tbxFrom_Date.Value & " - " & tbxTo_Date.Value
tbxFrom_Date.SetFocus
tbxFrom_Date.Value = ""
Else
'MsgBox "all good"
Unload Me
End If

when I read at my resulting message, the from date is definitely less than
the to date...

ideas?

Simon Shaw

Date Range problems
 
I figured it out... I changed the one line:
from:
ElseIf tbxFrom_Date tbxTo_Date Then
to:
ElseIf DateValue(tbxFrom_Date.Value) DateValue(tbxTo_Date.Value) Then



"Simon Shaw" wrote:

Hi,

I have a form with two textboxes for entering a date range. When the user
clicks OK on the form, i validate that the texboxes are dates... this works
fine... then I want to check that the from date is not greater than the to
date, but it is not working consitently. My message is returning that my from
date is greater that my to date even when it isn't. sample code:

If Not IsDate(tbxFrom_Date.Value) Then
MsgBox "The value entered in the From Date field is not a valid Date"
tbxFrom_Date.SetFocus
ElseIf Not IsDate(tbxTo_Date.Value) Then
MsgBox "The value entered in the To Date field is not a valid Date"
tbxTo_Date.SetFocus
ElseIf tbxFrom_Date tbxTo_Date Then
MsgBox "The From Date is greater than the To Date. Please change. "
& _
tbxFrom_Date.Value & " - " & tbxTo_Date.Value
tbxFrom_Date.SetFocus
tbxFrom_Date.Value = ""
Else
'MsgBox "all good"
Unload Me
End If

when I read at my resulting message, the from date is definitely less than
the to date...

ideas?



All times are GMT +1. The time now is 10:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com