Link to home
Start Free TrialLog in
Avatar of al4629740
al4629740Flag for United States of America

asked on

date syntax vb6

I have traced an issue with syntax on line 1.  Sometimes the code works and sometimes it does not based on how I type the date 7/1/2017.  What is the correct way to do the syntax

    If DTPicker1.Value < "7/01/2017" Or DTPicker1.Value > MaximumDate Then
        MsgBox "The DATE you entered is Not Authorized.  Please Re-Enter a Valid Date.  Date will be reset.", vbOKOnly, "Invalid Date"
        DTPicker1 = ServerTime
        Text2.SetFocus
    End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of al4629740

ASKER

Thank you.   I also just tested this below and it seemed to work.  Is this ok?

 
cdate(DTPicker) < "1/7/2017" Or DTPicker1.Value > MaximumDate Then

Open in new window

I believe the best approach would be either "2017-01-07" i.e. yyyy-MM-dd or "07-Jan-2017" dd-MMM-yyyy.
DTPicker should returns you with a date value, hence you no need to convert it again.

hence, you may try:

DTPicker) < cdate("1/7/2017") Or DTPicker1.Value > MaximumDate Then

Open in new window


or:
DTPicker < #1/7/2017# Or DTPicker1.Value > MaximumDate Then

Open in new window

(Don't have VS6 to test this but it may work)
or use DateSerial function

DateSerial(year, month, day)

DTPicker) < DateSerial(2017, 1, 7) Or DTPicker1.Value > MaximumDate Then

Open in new window

Is it wrong to use this format "1/7/2017" vs. "2017-01-07"
Is it wrong to use this format "1/7/2017" vs. "2017-01-07"
both are not equal, but to convert it as a date value, try apply CDate function.

you can try:

if  cdate("1/7/2017") = cdate("2017-01-07") then
    msgbox "Same"
else
    msgbox "Different"
end if

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, got to use:

If CDate(#1/7/2017#) = CDate(#1/7/2017#) Then
    MsgBox "Same"
Else
    MsgBox "Different"
End If

Open in new window


or:

If #1/7/2017# = #1/7/2017# Then
    MsgBox "Same"
Else
    MsgBox "Different"
End If

Open in new window


tested in VBA.

CDate("1/7/2017") is depending on system settings to determine the converted value, which is not reliable.

agreed with Nitin that need to use a more recognizable format for the dates
Thank you