error handling

Posted on 2004-11-15
Last Modified: 2010-05-02
I have a form, in which a user enters data. One of the fields on the form is a date field. I want this to happen when a users clicks on "continue":

If the user entered an invalid date, for example the correct format is "12/12/2004". If he enters "12-12-2004" . Then I should highlight the text box for date.


Question by:gbvreddy
    LVL 10

    Expert Comment

    Hi gbvreddy,

    Private Sub Continue_Button_Click()
      ' Validate date field here
      if not valid then
        exit sub
        ' do whatever needs to be done with a valid date
      end if
    End Sub

    Regards .. Alan
    LVL 4

    Expert Comment

    isValidDate will pass both of those, however you can put a date format in a registry key and then copare to that, or you can hard code it into your program though that sucks if you change your mind later on what you want to be accepted, if you use a key you can change it any time. then just focus back to text box when the form fails because of the date error.

    like this
    <body onload="document.forms['formName'].elements['elementName'].focus()">

    where formNAme is the Name of the FORM :
    <FORM name="formName" id="formName">

    and elementName is the elementNAme :
    <input type="text" name="elementName" id="elementName">

    then pass the element name back from the validation code.
    LVL 32

    Accepted Solution

    You could test if the date is valid, then format it as you wish for the user:

    Private Sub cmdContinue_Click()

        'validate Text1
        With Text1
        If IsDate(.Text) Then
            .Text = Format(.Text, "MM/DD/YYYY")
            MsgBox "Please enter a valid date"
            .SelStart = 0
            .SelLength = Len(Text1.Text)
        End If
        End With
    End Sub
    LVL 100

    Expert Comment

    How about using a calendar control like the DatePicker.  Ensures user can't enter an invalid date and ensures no confusion over proper format.


    Author Comment

    Thanks a lot everyone !


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to copy file 7 43
    z = x + y – 1 6 48
    DIR issue 7 39
    JSON Response and request in VB6 application 11 63
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now