al4629740
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
or:
hence, you may try:
DTPicker) < cdate("1/7/2017") Or DTPicker1.Value > MaximumDate Then
or:
DTPicker < #1/7/2017# Or DTPicker1.Value > MaximumDate Then
(Don't have VS6 to test this but it may work)
or use DateSerial function
DateSerial(year, month, day)
DateSerial(year, month, day)
DTPicker) < DateSerial(2017, 1, 7) Or DTPicker1.Value > MaximumDate Then
ASKER
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, got to use:
or:
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
If CDate(#1/7/2017#) = CDate(#1/7/2017#) Then
MsgBox "Same"
Else
MsgBox "Different"
End If
or:
If #1/7/2017# = #1/7/2017# Then
MsgBox "Same"
Else
MsgBox "Different"
End If
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
ASKER
Thank you
ASKER
Open in new window