Date/Time recognition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gord

    Date/Time recognition

    Hello,

    VB6 accepts Date and Time values as 'Date'. I'm trying to verify entry into
    a database I'm creating by verifying that an appropriate Date or Time value
    has been entered. Using built-in VB functions/properties etc., I can't seem
    to unambiguously confirm that an entry is a valid date or valid time value?
    (I would like to try and do this without having to write some huge procedure
    that processes an entry character by character, especialy with all the
    permutations that are possible) 'IsDate' recognizes date or time as 'Date',
    and I can't seem to interogate the variables to confirm it's one or the
    other. I've been playing around with 'DateValue', 'DatePart', 'TimeValue',
    'Year' functions etc............ Ahhhhhh.

    A suggestion in the right direction would be appreciated.

    Thanks,

    Gord


  • Rick Rothstein

    #2
    Re: Date/Time recognition

    > VB6 accepts Date and Time values as 'Date'. I'm trying to verify
    entry into[color=blue]
    > a database I'm creating by verifying that an appropriate Date or Time[/color]
    value[color=blue]
    > has been entered. Using built-in VB functions/properties etc., I[/color]
    can't seem[color=blue]
    > to unambiguously confirm that an entry is a valid date or valid time[/color]
    value?[color=blue]
    > (I would like to try and do this without having to write some huge[/color]
    procedure[color=blue]
    > that processes an entry character by character, especialy with all the
    > permutations that are possible) 'IsDate' recognizes date or time as[/color]
    'Date',[color=blue]
    > and I can't seem to interogate the variables to confirm it's one or[/color]
    the[color=blue]
    > other. I've been playing around with 'DateValue', 'DatePart',[/color]
    'TimeValue',[color=blue]
    > 'Year' functions etc............ Ahhhhhh.
    >
    > A suggestion in the right direction would be appreciated.[/color]

    It is not completely clear to me whether you are trying to verify that
    the entries are in the correct format for Dates and Times or simply if
    the entry is a Time entry or a Date entry. I kind of think you are
    looking for the latter. If so, you should be able to use these two
    statements (don't change the colon or slash, using them makes the Format
    statement return the value that is specified in the Regional settings,
    whatever it is)...

    DateSeparator = Format$(0, "/")
    TimeSeparator = Format$(0, ":")

    coupled with the InStr function to determine if the entry (assuming it
    is properly formatted) is a Date entry, a Time entry or neither.

    If InStr(YourEntry , DateSeparator) Then
    MsgBox "Your entry is a Date."
    ElseIf InStr(YourEntry , TimeSeparator) Then
    MsgBox "Your entry is a Time."
    Else
    MsgBox "Your entry is neither!"
    End If

    Rick - MVP

    Comment

    • Raoul Watson

      #3
      Re: Date/Time recognition


      "Gord" <x1gord1x@telus .net> wrote in message
      news:1cHwc.2487 8$jl6.7297@edtn ps89...[color=blue]
      > Hello,
      >
      > VB6 accepts Date and Time values as 'Date'. I'm trying to verify entry[/color]
      into[color=blue]
      > a database I'm creating by verifying that an appropriate Date or Time[/color]
      value[color=blue]
      > has been entered. Using built-in VB functions/properties etc., I can't[/color]
      seem[color=blue]
      > to unambiguously confirm that an entry is a valid date or valid time[/color]
      value?[color=blue]
      > (I would like to try and do this without having to write some huge[/color]
      procedure[color=blue]
      > that processes an entry character by character, especialy with all the
      > permutations that are possible) 'IsDate' recognizes date or time as[/color]
      'Date',[color=blue]
      > and I can't seem to interogate the variables to confirm it's one or the
      > other. I've been playing around with 'DateValue', 'DatePart',[/color]
      'TimeValue',[color=blue]
      > 'Year' functions etc............ Ahhhhhh.
      >
      > A suggestion in the right direction would be appreciated.
      >
      > Thanks,
      >
      > Gord
      >[/color]

      You should be able to use the "IsDate" function to check both time and date.
      For example:
      ? isdate("25:78")
      False
      ? isdate("11:50am ")
      True
      ? isDate("4/5/04")
      True
      ? isdate ("2/31/04")
      False

      I believe Rick addressed how to check the separator to see if it is a time
      entry.


      Comment

      • Gord

        #4
        Re: Date/Time recognition

        Rick,

        Thanks. You're right, I am trying to determine if an entry is a date or
        time. Formatting comes later. Your solution works, although it seems to be
        necessary to copy the entry into a 'Date' variable in order for the 'InStr'
        function to work. Even if 'IsDate' says a textbox entry is a valid date,
        using that textbox (or a string variable) in the 'InStr' function won't
        work. In your example 'YourEntry' needs to be a date variable.

        I'm using the Microsoft reference library and Programmers Guide to (attempt)
        to teach myself some basic programming. I'm wondering what the 'Format$(0,
        "/")' or 'Format$(0, ":")' is all about? What expression does the '0'
        represent?

        Thanks

        Gord


        "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
        news:Eeydnb9ZrP YUyF7dRVn-uQ@comcast.com. ..[color=blue][color=green]
        > > VB6 accepts Date and Time values as 'Date'. I'm trying to verify[/color]
        > entry into[color=green]
        > > a database I'm creating by verifying that an appropriate Date or Time[/color]
        > value[color=green]
        > > has been entered. Using built-in VB functions/properties etc., I[/color]
        > can't seem[color=green]
        > > to unambiguously confirm that an entry is a valid date or valid time[/color]
        > value?[color=green]
        > > (I would like to try and do this without having to write some huge[/color]
        > procedure[color=green]
        > > that processes an entry character by character, especialy with all the
        > > permutations that are possible) 'IsDate' recognizes date or time as[/color]
        > 'Date',[color=green]
        > > and I can't seem to interogate the variables to confirm it's one or[/color]
        > the[color=green]
        > > other. I've been playing around with 'DateValue', 'DatePart',[/color]
        > 'TimeValue',[color=green]
        > > 'Year' functions etc............ Ahhhhhh.
        > >
        > > A suggestion in the right direction would be appreciated.[/color]
        >
        > It is not completely clear to me whether you are trying to verify that
        > the entries are in the correct format for Dates and Times or simply if
        > the entry is a Time entry or a Date entry. I kind of think you are
        > looking for the latter. If so, you should be able to use these two
        > statements (don't change the colon or slash, using them makes the Format
        > statement return the value that is specified in the Regional settings,
        > whatever it is)...
        >
        > DateSeparator = Format$(0, "/")
        > TimeSeparator = Format$(0, ":")
        >
        > coupled with the InStr function to determine if the entry (assuming it
        > is properly formatted) is a Date entry, a Time entry or neither.
        >
        > If InStr(YourEntry , DateSeparator) Then
        > MsgBox "Your entry is a Date."
        > ElseIf InStr(YourEntry , TimeSeparator) Then
        > MsgBox "Your entry is a Time."
        > Else
        > MsgBox "Your entry is neither!"
        > End If
        >
        > Rick - MVP
        >[/color]


        Comment

        • Steve Gerrard

          #5
          Re: Date/Time recognition


          "Gord" <x1gord1x@telus .net> wrote in message
          news:tZryc.2877 8$%i1.24850@edt nps89...[color=blue]
          > Rick,
          >
          > Thanks. You're right, I am trying to determine if an entry is a date[/color]
          or[color=blue]
          > time. Formatting comes later. Your solution works, although it seems[/color]
          to be[color=blue]
          > necessary to copy the entry into a 'Date' variable in order for the[/color]
          'InStr'[color=blue]
          > function to work. Even if 'IsDate' says a textbox entry is a valid[/color]
          date,[color=blue]
          > using that textbox (or a string variable) in the 'InStr' function[/color]
          won't[color=blue]
          > work. In your example 'YourEntry' needs to be a date variable.
          >[/color]

          Like IsNumeric(X), which answers the question, "can I call CDbl(X)
          without getting an error?", IsDate(X) answers the question "can I call
          CDate(X) without getting an error?" Rick can post for you a fine example
          of why IsNumeric is otherwise useless for checking for valid numeric
          input.

          I would do this something like:
          Dim dtTest As Date

          If IsDate(Text1.Te xt) Then
          dtTest = CDate(Text1.Tex t)
          If dtTest > 0 And dtTest < 1 Then
          Debug.Print "its just a time"
          ElseIf dtTest >= 1 And Fix(dtTest) = dtTest Then
          Debug.Print "its just a date"
          ElseIf dtTest >= 1 Then
          Debug.Print "its a date and time"
          End If
          End If





          Comment

          Working...