Welcome to Dream.In.Code
Become a VB Expert!

Join 137,234 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,665 people online right now. Registration is fast and FREE... Join Now!




Date Validation in Visual Basic

 
Reply to this topicStart new topic

Date Validation in Visual Basic

sehohe
17 Mar, 2007 - 06:37 PM
Post #1

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 3


My Contributions
I'm writing a small app that adds a certain amount of days to a date. I have the user input the date in 3 textboxes - one each for month, date, and year. I need to validate the entire date as a legitimate date, so that if something like month = 2 and date= 31 would be caught as invalid. Here's the code for the calculate menu item:

CODE
    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

        userDays = Val(txtNumberDays.Text)
        userYear = Val(txtYear.Text)
        userMonth = Val(txtMonth.Text)
        userDate = Val(txtDate.Text)
        startDate = DateSerial(userYear, userMonth, userDate)
        If IsDate(startDate) Then
            Dim newDate As Date = FormatDateTime(DateAdd(DateInterval.Day, userDays, startDate), DateFormat.LongDate)
            txtResult.Text = newDate
        Else
            MessageBox.Show("Date entered is not valid.")
            Me.Refresh()
        End If

    End Sub


This does not catch invalid dates like 2/31/2007. (I have other subroutines which are catching blank entries and numbers like 13 for months...)
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Date Validation In Visual Basic
17 Mar, 2007 - 07:22 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
There is a couple ways to do what you want. But I will show you the quickest and easiest.

The key is to create a new Date object, supplying it with the date parameters. Use a Try/Catch to catch the resulting error if it is not a valid date. This way you can safely exit your method without crashing your program.

In the following example, this date would throw an exception causing the MessageBox to show its' message and exiting the subroutine returning control to the user.
CODE

        Try
            Dim startDate As Date = New Date(2007, 2, 31)
        Catch ex As Exception
            MessageBox.Show("This is not a valid date")
            Exit Sub
        End Try

User is online!Profile CardPM
+Quote Post

sehohe
RE: Date Validation In Visual Basic
18 Mar, 2007 - 02:09 PM
Post #3

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 3


My Contributions
Works great! Thanks.

..only 2 billion more questions to go to get the T-shirt! wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:59PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month