user2322016
user2322016

Reputation: 9

Comparing Dates With User Input

I have found a statement that compares two dates to see if one is greater than another:

If Format(Date, "m/d/yyyy") > #1/1/2000# Then MsgBox "Okay"

That's basically saying if today is later than January 1st, 2000 then pop up a message box.

This is fine if I am putting the date in the code (i.e., 1/1/2000), but I would like to get a date from the user during run-time and can't figure out how to incorporate that into the statement above.

If I provide a TextBox for the user to enter a date, I have surrounded the text with the # symbol:

If MyDate > "#" & Text1.Text & "#" then

That does not work. I've tried converting the text to Double, Integer, and Long. I have no idea how to use the enclosing # symbols with user input.

Thanks in advance for your help.

Upvotes: 0

Views: 555

Answers (1)

Euro Micelli
Euro Micelli

Reputation: 33998

Use CDate(Text1.Text) to convert a string to a date. In reality you will want to do some checking to make sure that the value entered is a valid date, something along the lines of:

Dim myDate As Date
If IsDate(Text1.Text) Then
    MyDate = CDate(Text1.Text)
Else
    'Indicate the error to the user
End If

The # characters are "date quotes", if you will - they serve the same purpose for date literals as " does to delimit a string literal. You can't just concatenate # to a value to make it into a date.

Upvotes: 1

Related Questions