Friends again problem with date function
In my form there is a text field -txtDod- of Date type formated like "dd-MMM-yyyy"
My input is also formated like "dd-MMM-yyyy"
Now see my code

 Private Sub TxtDod_KeyPress(KeyAscii As Integer)
 Select Case KeyAscii
 Case vbKeyReturn
 If Not IsDate(TxtDod) Then
 LblMsg = "Enter Date Of Deposit"
 TxtDod.SetFocus
 ElseIf (Format$(TxtDod.Text, "dd-MMM-yyyy")) > (Format$(Date, "dd-MMM-yyyy")) Then
 LblMsg = "Date Of Deposit Must Be <= Today"
 TxtDod.SetFocus
 Else
 TxtPrd.SetFocus
 End If
 End Select
 End Sub

My problem is I want this-If the input date of TxtDod is greater than Current Date(Today's Date)
the focus should not be shifted to the next text box (txtPrd)
When I tried with the following dates I got the results as follows
14-Feb-2014(Today's Date)Focus shifted to next textbox TxtPrd-Correct
14-Feb-2015 (Date is greater than today) Focus didnot shifted to next field - Correct
13-Feb-2015 (Date is greater) Focus shifted to next field even after the date is greater --Wrong result
14-Feb-2012 (Date is smaller) Focus shifted --Correct
14-Jun-2012 (Date is Smaller) Focus is not shifted even though the date is smaller than today --wrong result
Why this discreepancy? Anything wrong in my code? I am just studying VB6
kindly help me..

Dani AI

Generated

The behaviour you saw comes from comparing formatted strings, not Date values. Formatting a date as "dd-MMM-yyyy" produces strings that sort lexicographically by day first (and then by the month text), so "13-Feb-2015" will compare as less than "14-Feb-2014" even though its year is later. That explains the seemingly inconsistent results.

Compare Date values instead of strings. Validate with IsDate on the textbox text, convert with CDate (or DateValue), then do a numeric date comparison. Example:

Private Sub TxtDod_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        If Not IsDate(TxtDod.Text) Then
            LblMsg = "Enter Date Of Deposit"
            TxtDod.SetFocus
        Else
            Dim d As Date
            d = CDate(Trim$(TxtDod.Text))
            If d > Date Then
                LblMsg = "Date Of Deposit Must Be <= Today"
                TxtDod.SetFocus
            Else
                TxtPrd.SetFocus
            End If
        End If
    End If
End Sub

As suggested, DateDiff is another valid check (for example, If DateDiff("d", Date, d) > 0 Then means the entered date is in the future). If you ever must compare strings, format them as ISO-ish yyyy-mm-dd so lexical order matches chronological order (but converting to Date is safer).

Notes and cautions: use IsDate(TxtDod.Text) (explicit .Text), trim input, and be aware that IsDate/CDate honor regional settings—if input can be ambiguous, parse components and build with DateSerial(Year, Month, Day) to avoid locale issues. This will fix the discrepancies you observed and is more robust than string comparisons.

Recommended Answers

All 3 Replies

OK That works...But what is wrong with my code? Any mistake..?
Just for academic interest...

There are a few pitfalls working with dates as it always depends on how the compiler works these and can make decisions on what it thinks you want. ALso if you want to compare just numbers you will see that four comes before one because f goes when sorting before o. So generally speaking google as much as you can digest about some of th code if something goe wrong. Please mark this thread as solved.Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.