Trappable Errors

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

    #1

    Trappable Errors

    Is there an error that can be raised to replace IsNumeric?

    Thanks!
  • Rick Rothstein

    #2
    Re: Trappable Errors

    > Is there an error that can be raised to replace IsNumeric?

    I'm not sure what that question is supposed to mean. What is it you are
    trying to do?

    Rick - MVP


    Comment

    • martim07

      #3
      Re: Trappable Errors

      I want to validate textbox input. I need to make sure that there is
      something entered and that the entered value is a number. I don't want
      to use IsNumeric or Len(...). I want to get into trapping and handling
      errors. Thanks.










      "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message news:<d4CcnfOAY MEpSheiU-KYvg@comcast.co m>...[color=blue][color=green]
      > > Is there an error that can be raised to replace IsNumeric?[/color]
      >
      > I'm not sure what that question is supposed to mean. What is it you are
      > trying to do?
      >
      > Rick - MVP[/color]

      Comment

      • Steve Gdula

        #4
        Re: Trappable Errors

        martim07@hotmai l.com (martim07) wrote in message news:<85e11858. 0310131306.7743 3334@posting.go ogle.com>...[color=blue]
        > I want to validate textbox input. I need to make sure that there is
        > something entered and that the entered value is a number. I don't want
        > to use IsNumeric or Len(...). I want to get into trapping and handling
        > errors. Thanks.
        >[/color]
        It sounds like you are making things harder on yourself than
        necessary.

        Use the 'VAL' function: dblValue=VAL(tx tBox.text)
        Right out of the MSDN Library---------------------------------------
        The Val function stops reading the string at the first character it
        can't recognize as part of a number. Symbols and characters that are
        often considered parts of numeric values, such as dollar signs and
        commas, are not recognized. However, the function recognizes the radix
        prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and
        linefeed characters are stripped from the argument.

        ---------------------------------------------------------------------
        You need decision logic to test for the returned value at some event:
        if dblValue<1 then
        err.raise (someErrorNum) 'Explicitly raise an error

        or alternately you can:
        dblResult=1/dblValue 'This will also raise an error if dblValue is
        zero.

        It it up to you to catch the errors.

        --Steve.

        Comment

        • Rick Rothstein

          #5
          Re: Trappable Errors

          > I want to validate textbox input. I need to make sure that there is[color=blue]
          > something entered and that the entered value is a number. I don't want
          > to use IsNumeric or Len(...). I want to get into trapping and handling
          > errors. Thanks.[/color]

          There is no (practical) way to get VB to recognize a non-numeric input to a
          TextBox as being an error without checking to see that it is not a number
          and using Err.Raise to force the error. Of course, that would be silly to do
          since as soon as you know it is not what you want, you can react to it then
          and there without wasting VB's time forcing it to generate an error.

          Checking with Len to see if anything was typed is the standard way to check
          if the TextBox is empty. You might want to use Trim$ inside of the Len
          function to catch instances where the **only** thing the user typed was one
          or more blank spaces. As for deciding if the user's entry is a number or
          not, here are two functions that I have posted in the past for similar
          questions..... one is for digits only and the other is for "regular"
          numbers:

          Function IsDigitsOnly(Va lue As String) As Boolean
          IsDigitsOnly = Not Value Like "*[!0-9]*"
          End Function

          Function IsNumber(ByVal Value As String) As Boolean
          ' Leave the next statement out if you don't
          ' want to provide for plus/minus signs
          If Value Like "[+-]*" Then Value = Mid$(Value, 2)
          IsNumber = Not Value Like "*[!0-9.]*" And _
          Not Value Like "*.*.*" And _
          Len(Value) > 0 And Value <> "." And _
          Value <> vbNullString
          End Function


          Rick - MVP


          Comment

          • Gary W.

            #6
            Re: Trappable Errors


            "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
            news:3E-dnSY0x9dxohGiXT WJhg@comcast.co m...[color=blue][color=green]
            > > I want to validate textbox input. I need to make sure that there is
            > > something entered and that the entered value is a number. I don't want
            > > to use IsNumeric or Len(...). I want to get into trapping and handling
            > > errors. Thanks.[/color]
            >
            > There is no (practical) way to get VB to recognize a non-numeric input to[/color]
            a[color=blue]
            > TextBox as being an error without checking to see that it is not a number
            > and using Err.Raise to force the error. Of course, that would be silly to[/color]
            do[color=blue]
            > since as soon as you know it is not what you want, you can react to it[/color]
            then[color=blue]
            > and there without wasting VB's time forcing it to generate an error.
            >
            > Checking with Len to see if anything was typed is the standard way to[/color]
            check[color=blue]
            > if the TextBox is empty. You might want to use Trim$ inside of the Len
            > function to catch instances where the **only** thing the user typed was[/color]
            one[color=blue]
            > or more blank spaces. As for deciding if the user's entry is a number or
            > not, here are two functions that I have posted in the past for similar
            > questions..... one is for digits only and the other is for "regular"
            > numbers:
            >
            > Function IsDigitsOnly(Va lue As String) As Boolean
            > IsDigitsOnly = Not Value Like "*[!0-9]*"
            > End Function
            >
            > Function IsNumber(ByVal Value As String) As Boolean
            > ' Leave the next statement out if you don't
            > ' want to provide for plus/minus signs
            > If Value Like "[+-]*" Then Value = Mid$(Value, 2)
            > IsNumber = Not Value Like "*[!0-9.]*" And _
            > Not Value Like "*.*.*" And _
            > Len(Value) > 0 And Value <> "." And _
            > Value <> vbNullString
            > End Function
            >
            >
            > Rick - MVP
            >
            >[/color]
            These methods all work- however I use a much simpler method..

            For the text box I use the KeyPress event:
            Text1_KeyPress( KeyAscii as Integer)

            Select Case KeyAscii
            Case 48 to 57, 8
            Case Else
            KeyAscii = 0
            End Select

            Works in that NO non-numeric input *can* be entered. 48 to 57 are the ASCII
            codes for 0 through 9 and 8 is ASCII for backspace (in case user makes a
            mistake? Hey... it's been known to happen!)

            For the textbox event (to see if anything was written) would it not be
            easier just to write:

            If text1.text = "" Then
            MsgBox "Write your message here"
            text1.SetFocus
            End If
            ???

            Would that help you more?

            Gary - MCP


            Comment

            • Rick Rothstein

              #7
              Re: Trappable Errors

              See inline comments...
              [color=blue][color=green]
              > > Function IsDigitsOnly(Va lue As String) As Boolean
              > > IsDigitsOnly = Not Value Like "*[!0-9]*"
              > > End Function
              > >
              > > Function IsNumber(ByVal Value As String) As Boolean
              > > ' Leave the next statement out if you don't
              > > ' want to provide for plus/minus signs
              > > If Value Like "[+-]*" Then Value = Mid$(Value, 2)
              > > IsNumber = Not Value Like "*[!0-9.]*" And _
              > > Not Value Like "*.*.*" And _
              > > Len(Value) > 0 And Value <> "." And _
              > > Value <> vbNullString
              > > End Function
              > >
              > >
              > > Rick - MVP
              > >
              > >[/color]
              > These methods all work- however I use a much simpler method..
              >
              > For the text box I use the KeyPress event:
              > Text1_KeyPress( KeyAscii as Integer)
              >
              > Select Case KeyAscii
              > Case 48 to 57, 8
              > Case Else
              > KeyAscii = 0
              > End Select
              >
              > Works in that NO non-numeric input *can* be entered. 48 to 57 are the[/color]
              ASCII[color=blue]
              > codes for 0 through 9 and 8 is ASCII for backspace (in case user makes a
              > mistake? Hey... it's been known to happen!)[/color]

              If you are trying to restrict entry to only numbers, the above will not
              protect against the user Pasting in non-numeric characters. Two camps have
              formed over data verification... the first is like your proposal, do it at
              entry time; the other is let the user type what they want without bothering
              them during the entry process and proof it later (which is what my functions
              would be used for and, I believe, is what the OP asked for).

              [color=blue]
              > For the textbox event (to see if anything was written) would it not be
              > easier just to write:
              >
              > If text1.text = "" Then
              > MsgBox "Write your message here"
              > text1.SetFocus
              > End If
              > ???[/color]

              While Text1.Text = "" works, believe it or not, Len(Text1.Text) = 0 executes
              faster. However, I would do as I suggested in my first post and use
              Trim$(Text1.Tex t) instead of Text1.Text directly... that way you protect
              against the user accidentally entering a blank space into the TextBox.


              Rick - MVP


              Comment

              Working...