Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleVerify that the user entered a date in an InputBox
DescriptionThis example shows how to verify that the user entered a date in an InputBox in Visual Basic 6.
KeywordsInputBox, validate, verify, date
CategoriesStrings
 
Thanks to Dee Townsend.

When the user unchecks the chkActive check box, the program prompts the user for a contract date. It checks whether the string is blank. It then uses the IsDate function to see if the value looks like a date. If it does look like a date, the program converts it to a date and compares it with the current date. If the date is later than today, the program prompts you for a new date and repeats the verification steps.

 
Private Sub chkActive_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkActive.CheckedChanged
    'Check to see if there is a change in a Contract status
    If Me.txtAcctName.Text.Length > 0 Then

        ' If Current contract become invalid changes, 
        ' uncheck the "chkActive" box and validate date in
        ' input box
        If Me.chkActive.CheckState = CheckState.Unchecked _
            Then 'Checked = False Then
            Dim message, title As String
            Dim myValue As Object

            message = "Contract is no longer valid as of " & _
                "date entered:"   ' Set prompt.
            title = "Term Date"   ' Set the title.
            myValue = InputBox(message, title)

            If myValue Is "" Then
                ' If input box is empty, show message and
                ' set check box to True
                MessageBox.Show("No date was entered.")
                Me.chkActive.Checked = True
            Else
                ' If inputBox contains a value, then
                ' validate its contents:
                ' Set a flag to loop trough inputbox
                ' content until
                ' information is correct
                Dim checkDate As Boolean = False
                Do
                    Dim sPA As String = Trim(CStr(myValue))
                    '  Check to see if value entered is a
                    ' date
                    '  If not, then reset the inputBox 
                    If IsDate(sPA) = False Then
                        MessageBox.Show("Please enter a " & _
                            "valid date.")
                        checkDate = False
                        myValue = InputBox(message, title)
                    Else
                        '  If it is a date, make sure its
                        ' not later than today's date
                        If CDate(sPA) > Today Then
                            MessageBox.Show("Please enter a " & _
                                "valid date no later than " & _
                                "today.")
                            checkDate = False
                            myValue = InputBox(message, _
                                title)
                        Else
                            '  If all is well, procede 
                            Dim oPA As String = _
                                Me.txtAcctName.Text
                            Me.lblOldPvtAcct.Text = oPA
                            Me.lblTermDate.Text = _
                                CStr(myValue)

                            ' Set flag to true when
                            ' information entered into
                            ' inputbox is correct.
                            checkDate = True
                            'Reset text boxes for new entry
                            With txtAcctName
                                .Text = ""
                                .Enabled = True
                            End With

                            With chkActive
                                .Checked = False
                                .Enabled = False
                            End With
                        End If
                    End If
                Loop Until checkDate = True
            End If
        End If
    End If

End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated