+ Reply to Thread
Results 1 to 4 of 4

CDate and IsDate

  1. #1
    Geoff
    Guest

    CDate and IsDate

    Hi
    The following test fails using CDate, can anyone explain why and how I can
    produce the correct answer 'is not valid'? As it is, CDate evaluates the
    invalid year 0000 to 2000 and results in an 'out of range' result incorrectly.

    Input to 3 textboxes gives:
    txtDay.Text = 12
    txtMonth.Text = 05
    txtYear.Text = 0000

    tmpdate = CDate(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    If IsDate(tmpdate) = False Then
    MsgBox "Please re-enter, " & txtSDay.Text & "/" & txtSMonth.Text & "/" &
    txtSYear.Text & " is not valid. ")
    Exit Sub
    ElseIf tmpdate < DateAdd("yyyy", -1, Now()) Or tmpdate > Now() Then
    MsgBox "Please re-enter, " & txtDay.Text & "/" & txtMonth.Text & "/" &
    txtYear.Text & " is not within range. ")
    End If

    T.I.A

    Geoff

  2. #2
    Bob Phillips
    Guest

    Re: CDate and IsDate

    Try

    tmpdate = dateValue(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)


    --
    HTH

    Bob Phillips

    "Geoff" <[email protected]> wrote in message
    news:[email protected]...
    > Hi
    > The following test fails using CDate, can anyone explain why and how I can
    > produce the correct answer 'is not valid'? As it is, CDate evaluates the
    > invalid year 0000 to 2000 and results in an 'out of range' result

    incorrectly.
    >
    > Input to 3 textboxes gives:
    > txtDay.Text = 12
    > txtMonth.Text = 05
    > txtYear.Text = 0000
    >
    > tmpdate = CDate(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    > If IsDate(tmpdate) = False Then
    > MsgBox "Please re-enter, " & txtSDay.Text & "/" & txtSMonth.Text & "/"

    &
    > txtSYear.Text & " is not valid. ")
    > Exit Sub
    > ElseIf tmpdate < DateAdd("yyyy", -1, Now()) Or tmpdate > Now() Then
    > MsgBox "Please re-enter, " & txtDay.Text & "/" & txtMonth.Text & "/" &
    > txtYear.Text & " is not within range. ")
    > End If
    >
    > T.I.A
    >
    > Geoff




  3. #3
    Geoff
    Guest

    Re: CDate and IsDate

    Hi Bob
    No, 0000 still evaluates to 2000.
    Researching further, permitted dates go back as far as Jan 1 100AD so 0000
    should fail when using IsDate.

    Geoff

    "Bob Phillips" wrote:

    > Try
    >
    > tmpdate = dateValue(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > "Geoff" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi
    > > The following test fails using CDate, can anyone explain why and how I can
    > > produce the correct answer 'is not valid'? As it is, CDate evaluates the
    > > invalid year 0000 to 2000 and results in an 'out of range' result

    > incorrectly.
    > >
    > > Input to 3 textboxes gives:
    > > txtDay.Text = 12
    > > txtMonth.Text = 05
    > > txtYear.Text = 0000
    > >
    > > tmpdate = CDate(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    > > If IsDate(tmpdate) = False Then
    > > MsgBox "Please re-enter, " & txtSDay.Text & "/" & txtSMonth.Text & "/"

    > &
    > > txtSYear.Text & " is not valid. ")
    > > Exit Sub
    > > ElseIf tmpdate < DateAdd("yyyy", -1, Now()) Or tmpdate > Now() Then
    > > MsgBox "Please re-enter, " & txtDay.Text & "/" & txtMonth.Text & "/" &
    > > txtYear.Text & " is not within range. ")
    > > End If
    > >
    > > T.I.A
    > >
    > > Geoff

    >
    >
    >


  4. #4
    Geoff
    Guest

    Re: CDate and IsDate

    Further testing reveals:
    From 01 Jan 0001 to 31 Dec 0029 the year is returned as 20xx
    From 01 Jan 0030 to 31 Dec 0099 the year is returned as 19xx
    From 01 Jan 0100 the year is returned correctly.

    Both IsDate and CDate operate on the same basis.

    Clearly I must test the year txtbox component separately for zero.

    Geoff

    "Geoff" wrote:

    > Hi Bob
    > No, 0000 still evaluates to 2000.
    > Researching further, permitted dates go back as far as Jan 1 100AD so 0000
    > should fail when using IsDate.
    >
    > Geoff
    >
    > "Bob Phillips" wrote:
    >
    > > Try
    > >
    > > tmpdate = dateValue(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    > >
    > >
    > > --
    > > HTH
    > >
    > > Bob Phillips
    > >
    > > "Geoff" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Hi
    > > > The following test fails using CDate, can anyone explain why and how I can
    > > > produce the correct answer 'is not valid'? As it is, CDate evaluates the
    > > > invalid year 0000 to 2000 and results in an 'out of range' result

    > > incorrectly.
    > > >
    > > > Input to 3 textboxes gives:
    > > > txtDay.Text = 12
    > > > txtMonth.Text = 05
    > > > txtYear.Text = 0000
    > > >
    > > > tmpdate = CDate(txtDay.Text & "/" & txtMonth.Text & "/" & txtYear.Text)
    > > > If IsDate(tmpdate) = False Then
    > > > MsgBox "Please re-enter, " & txtSDay.Text & "/" & txtSMonth.Text & "/"

    > > &
    > > > txtSYear.Text & " is not valid. ")
    > > > Exit Sub
    > > > ElseIf tmpdate < DateAdd("yyyy", -1, Now()) Or tmpdate > Now() Then
    > > > MsgBox "Please re-enter, " & txtDay.Text & "/" & txtMonth.Text & "/" &
    > > > txtYear.Text & " is not within range. ")
    > > > End If
    > > >
    > > > T.I.A
    > > >
    > > > Geoff

    > >
    > >
    > >


+ 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