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

al4629740Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try this instead:

    If DTPicker1.Value < cdate("01/07/2017") Or DTPicker1.Value > MaximumDate Then

Open in new window

       
or:

    If DTPicker1.Value < cdate("7 Jan 2017") Or DTPicker1.Value > MaximumDate Then

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
al4629740Author Commented:
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

0
Nitin SontakkeDeveloperCommented:
I believe the best approach would be either "2017-01-07" i.e. yyyy-MM-dd or "07-Jan-2017" dd-MMM-yyyy.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ryan ChongCommented:
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)
0
Ryan ChongCommented:
or use DateSerial function

DateSerial(year, month, day)

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

Open in new window

0
al4629740Author Commented:
Is it wrong to use this format "1/7/2017" vs. "2017-01-07"
0
Ryan ChongCommented:
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

0
Nitin SontakkeDeveloperCommented:
I think the "1/7/2017" is machine dependent and "2017-01-07" is machine independent. If the application is Windows Forms application, used in a small office in US, this probably doesn't matter much.

However, when same application is shared with yet another branch in London, it won't work as expected. Today, you may not expect your employer / client / customer would open a branch in London ever or in foreseeable future, but then again, why not cater to it right away and gain prolonged peace of mind.

This is the key to writing "resilient" software. Also notice the return on investment ratio here.
0
Ryan ChongCommented:
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
0
al4629740Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.