+ Reply to Thread
Results 1 to 7 of 7

InputBox Validation?

  1. #1
    PSKelligan
    Guest

    InputBox Validation?

    Hello All,
    What would be the proper syntax for the following? The question is
    commented out in the code.

    Sub mySub()
    Dim myDate As Date
    myDate = InputBox("Please enter a date.")

    ' **Here is where I get a type mismatch error**
    ' How should I word this condition to apply to the "Date type"
    ' I defined at the start"
    If myDate = "" then 'Type mismatch error!!!
    MsgBox "You must enter a date! PLease retry."
    Exit Sub
    End If
    ' Run my proceedure here
    End Sub

    --
    Thanks,

    Patrick

  2. #2
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282

    Detemining 'Date' data type in VB

    Use the IsDate function to determine if data is a Date data type. From VB Help file:
    IsDate Function


    Returns a Boolean value indicating whether an expression can be converted to a date.

    Syntax

    IsDate(expression)

    The required expression argument is a Variant containing a date expression or string expression recognizable as a date or time.

    Remarks

    IsDate returns True if the expression is a date or is recognizable as a valid date; otherwise, it returns False. In Microsoft Windows, the range of valid dates is January 1, 100 A.D. through December 31, 9999 A.D.; the ranges vary among operating systems.
    This code should work for you:
    Please Login or Register  to view this content.
    Hope this helps,
    theDude

  3. #3
    K Dales
    Guest

    RE: InputBox Validation?

    I would do it this way:

    Sub mySub()
    Dim myDateInpt asString, myDate as Date

    myDateInpt = ""
    While Not(IsDate(myDateInpt)
    myDateInpt = InputBox("Please enter a date.")
    If Not(IsDate(myDateInpt) Then MsgBox "You must enter a date! Please
    retry."
    Wend
    MyDate = DateValue(myDateInpt)
    ' Run your proceedure here
    End Sub



    "PSKelligan" wrote:

    > Hello All,
    > What would be the proper syntax for the following? The question is
    > commented out in the code.
    >
    > Sub mySub()
    > Dim myDate As Date
    > myDate = InputBox("Please enter a date.")
    >
    > ' **Here is where I get a type mismatch error**
    > ' How should I word this condition to apply to the "Date type"
    > ' I defined at the start"
    > If myDate = "" then 'Type mismatch error!!!
    > MsgBox "You must enter a date! PLease retry."
    > Exit Sub
    > End If
    > ' Run my proceedure here
    > End Sub
    >
    > --
    > Thanks,
    >
    > Patrick


  4. #4
    Jim Thomlinson
    Guest

    RE: InputBox Validation?

    That code is great but there is one thing to note (not a problem with the
    code) once you are in the loop there is no way out except to enter a date...
    The user can not cancel... Depending what you want to do... This can be very
    frustrating for the user. Like I said the code is great but be careful what
    you wish for. ;-)

    "K Dales" wrote:

    > I would do it this way:
    >
    > Sub mySub()
    > Dim myDateInpt asString, myDate as Date
    >
    > myDateInpt = ""
    > While Not(IsDate(myDateInpt)
    > myDateInpt = InputBox("Please enter a date.")
    > If Not(IsDate(myDateInpt) Then MsgBox "You must enter a date! Please
    > retry."
    > Wend
    > MyDate = DateValue(myDateInpt)
    > ' Run your proceedure here
    > End Sub
    >
    >
    >
    > "PSKelligan" wrote:
    >
    > > Hello All,
    > > What would be the proper syntax for the following? The question is
    > > commented out in the code.
    > >
    > > Sub mySub()
    > > Dim myDate As Date
    > > myDate = InputBox("Please enter a date.")
    > >
    > > ' **Here is where I get a type mismatch error**
    > > ' How should I word this condition to apply to the "Date type"
    > > ' I defined at the start"
    > > If myDate = "" then 'Type mismatch error!!!
    > > MsgBox "You must enter a date! PLease retry."
    > > Exit Sub
    > > End If
    > > ' Run my proceedure here
    > > End Sub
    > >
    > > --
    > > Thanks,
    > >
    > > Patrick


  5. #5
    PSKelligan
    Guest

    RE: InputBox Validation?

    That seemed to do the trick!
    Thanks!

    "K Dales" wrote:

    > I would do it this way:
    >
    > Sub mySub()
    > Dim myDateInpt asString, myDate as Date
    >
    > myDateInpt = ""
    > While Not(IsDate(myDateInpt)
    > myDateInpt = InputBox("Please enter a date.")
    > If Not(IsDate(myDateInpt) Then MsgBox "You must enter a date! Please
    > retry."
    > Wend
    > MyDate = DateValue(myDateInpt)
    > ' Run your proceedure here
    > End Sub


  6. #6
    PSKelligan
    Guest

    RE: InputBox Validation?

    Excelent point Jim!
    I will try to work in an Exit Sub line in there.

    Thanks for the heads up!
    Patrick

    "Jim Thomlinson" wrote:

    > That code is great but there is one thing to note (not a problem with the
    > code) once you are in the loop there is no way out except to enter a date...
    > The user can not cancel... Depending what you want to do... This can be very
    > frustrating for the user. Like I said the code is great but be careful what
    > you wish for. ;-)


  7. #7
    K Dales
    Guest

    RE: InputBox Validation?

    You are correct, Jim - and I usually try to anticipate and avoid (even in
    code snippets and examples) situations that might frustrate users - or
    (equally important!) where users can frustrate the code. In practice I would
    give the user an "out" - like if nothing was input, give a messagebox asking
    if the user wants to cancel.

    "Jim Thomlinson" wrote:

    > That code is great but there is one thing to note (not a problem with the
    > code) once you are in the loop there is no way out except to enter a date...
    > The user can not cancel... Depending what you want to do... This can be very
    > frustrating for the user. Like I said the code is great but be careful what
    > you wish for. ;-)
    >
    > "K Dales" wrote:
    >
    > > I would do it this way:
    > >
    > > Sub mySub()
    > > Dim myDateInpt asString, myDate as Date
    > >
    > > myDateInpt = ""
    > > While Not(IsDate(myDateInpt)
    > > myDateInpt = InputBox("Please enter a date.")
    > > If Not(IsDate(myDateInpt) Then MsgBox "You must enter a date! Please
    > > retry."
    > > Wend
    > > MyDate = DateValue(myDateInpt)
    > > ' Run your proceedure here
    > > End Sub
    > >
    > >
    > >
    > > "PSKelligan" wrote:
    > >
    > > > Hello All,
    > > > What would be the proper syntax for the following? The question is
    > > > commented out in the code.
    > > >
    > > > Sub mySub()
    > > > Dim myDate As Date
    > > > myDate = InputBox("Please enter a date.")
    > > >
    > > > ' **Here is where I get a type mismatch error**
    > > > ' How should I word this condition to apply to the "Date type"
    > > > ' I defined at the start"
    > > > If myDate = "" then 'Type mismatch error!!!
    > > > MsgBox "You must enter a date! PLease retry."
    > > > Exit Sub
    > > > End If
    > > > ' Run my proceedure here
    > > > End Sub
    > > >
    > > > --
    > > > Thanks,
    > > >
    > > > Patrick


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1