Monday, January 18, 2010

Date validation in VB 6.0

How Date Validation is done with IsDate Function?


You can easily validate the Input of date using IsDate function. The function will return a boolean value indicating the true or false result. Date validation is essential in every commercial / business application. In VB .NET you can use DateTime.Parse() method in a try catch block. If the date is not valid the catch block will execute where you can handle the error condition. In VB 6.0 you can use the IsDate() Function with the date string as input parameter.

Here is an example tutorial for IsDate function. Create a standard EXE project and put a text box name txtDate. Put a command button name cmdValidate. In the Validate event of txtDate write the following code.

Private Sub txtDate_Validate(Cancel As Boolean)
If Not IsDate(txtDate.Text) Then
Cancel = True
MsgBox "Enter valid Date", vbExclamation
txtDate.Text = ""
End If
End Sub