0

Ive tried using IsDate() and made a very simple code just to try and validate one textbox which is the user's input. Everytime i get the Else code meaning the date is not validated properly. Any ideas on how to make it work?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If IsDate(TextBox1) Then
        MsgBox("well done")
    Else
        MsgBox("you failed")
    End If
End Sub
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67

2 Answers2

0

You want the value in the TextBox not the TextBox itself. Change to TextBox1.Text

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If IsDate(TextBox1.Text) Then
        MsgBox("well done")
    Else
        MsgBox("you failed")
    End If
End Sub
Gratzy
  • 9,164
  • 4
  • 30
  • 45
  • 1
    No, something else is going on, at least if this is vb6 (I don't know about vba). The default property for a textbox control is the Text property, so `IsDate(TextBox1)` is the same as `IsDate(TextBox1.Text)`. – MarkL Mar 08 '16 at 20:35
  • @MarkL I can confirm the behavior you describe works exactly like that in vba 7.0 as well. So I'm not sure if he is using something older or as if you say it was something else going on. – Gratzy Mar 08 '16 at 22:07
  • @Kamil out of curiosity what are you developing in? – Gratzy Mar 11 '16 at 13:57
  • @Gratzy A form which adds a record to the database. Ive changed the input box for date to a DateTimePicker and i use Date.Now() to check if the chosen date is not a date from the past. The code is "If DateTimePicker1.Text <= Date.Now Then" but theres a problem with it where if i pick todays date, the date is considered LESS for some reason and it pick the ELSE event – Kamil Lazarowicz Mar 11 '16 at 16:48
0

Everybody knows that IsDate function may provide several issues, i.e.:

IsDate("13.50") 'returns  True
IsDate("12.25.2010") 'may return False/True
                     'depending on current culture  (OS date-time settings)

Take a look here: IsDate function returns unexpected results

I'd strongly recommend to use CDate() function, which returns proper date or causes conversion error.

https://msdn.microsoft.com/en-us/library/2dt118h2%28v=vs.84%29.aspx

Note: i'd suggest to pass vaule into CDate function in ISO standard format: yyyy/MM/dd to be sure that conversion will return proper date (time).

Community
  • 1
  • 1
Maciej Los
  • 8,468
  • 1
  • 20
  • 35