Invalid Data?

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

    Invalid Data?

    I do some error checking on my textbox. The problem is that if I enter
    a valid number, then backspace until the box is empty, my msgbox pops up
    saying "Data Error". Is there a way to avoid this?

    Also, if I enter invalid data and try to backspace through it, my "Data
    Error" msgbox pops up. This is rather annoying. Does anyone have a
    solution to this problem? I posted my code - I hope it formats correctly.

    Thanks.


    Private Sub txtAmtSold_Chan ge()
    If txtAmtSold = "" Then
    mnuEditCalculat e.Enabled = False
    End If

    If IsNumeric(txtAm tSold) Then
    If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
    <= 100 Then
    mnuEditCalculat e.Enabled = True
    Else
    mnuEditCalculat e.Enabled = False
    MsgBox "Error: Numbers must be in the range of 1 - 100",
    vbExclamation, "Range Error"
    End If
    Else
    mnuEditCalculat e.Enabled = False
    MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
    End If
    End Sub

  • Randy Birch

    #2
    Re: Invalid Data?

    If you're not hung up on throwing message boxes every time someone makes an
    error, just use this:

    Private Sub txtAmtSold_Chan ge()

    mnuEditCal.Enab led = Len(txtAmtSold. Text) > 0 And _
    IsNumeric(txtAm tSold) And _
    Val(txtAmtSold. Text) >= 1 And _
    Val(txtAmtSold. Text)

    End Sub


    --

    Randy Birch
    MVP Visual Basic

    Please respond only to the newsgroups so all can benefit.


    "dfg" <dfg@dfg.net> wrote in message news:zl%mb.1934 61$6C4.13496@pd 7tw1no...
    : I do some error checking on my textbox. The problem is that if I enter
    : a valid number, then backspace until the box is empty, my msgbox pops up
    : saying "Data Error". Is there a way to avoid this?
    :
    : Also, if I enter invalid data and try to backspace through it, my "Data
    : Error" msgbox pops up. This is rather annoying. Does anyone have a
    : solution to this problem? I posted my code - I hope it formats correctly.
    :
    : Thanks.
    :
    :
    : Private Sub txtAmtSold_Chan ge()
    : If txtAmtSold = "" Then
    : mnuEditCalculat e.Enabled = False
    : End If
    :
    : If IsNumeric(txtAm tSold) Then
    : If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
    : <= 100 Then
    : mnuEditCalculat e.Enabled = True
    : Else
    : mnuEditCalculat e.Enabled = False
    : MsgBox "Error: Numbers must be in the range of 1 - 100",
    : vbExclamation, "Range Error"
    : End If
    : Else
    : mnuEditCalculat e.Enabled = False
    : MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
    : End If
    : End Sub
    :


    Comment

    • Rick Rothstein

      #3
      Re: Invalid Data?

      > If IsNumeric(txtAm tSold) Then

      I usually try and steer people away from using IsNumeric to "proof"
      supposedly numeric text. Consider this (also see note at end of post):

      ReturnValue = IsNumeric("($1, 23,,3.4,,,5,,E6 7$)")

      Most people would not expect THAT to return True. IsNumeric has some "flaws"
      in what it considers a proper number and what most programmers are looking
      for.

      I had a short tip published by Pinnacle Publishing in their Visual Basic
      Developer magazine that covered some of these flaws. Originally, the tip was
      free to view but is now viewable only by subscribers.. Basically, it said
      that IsNumeric returned True for things like -- currency symbols being
      located in front or in back of the number as shown in my example (also
      applies to plus, minus and blanks too); numbers surrounded by parentheses as
      shown in my example (some people use these to mark negative numbers);
      numbers containing any number of commas before a decimal point as shown in
      my example; numbers in scientific notation (a number followed by an upper or
      lower case "D" or "E", followed by a number equal to or less than 305 -- the
      maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
      Hexadecimal, &O or just & in front of the number for Octal).

      NOTE:
      ======
      In the above example and in the referenced tip, I refer to $ signs and
      commas and dots -- these were meant to refer to your currency, thousands
      separator and decimal point symbols as defined in your local settings --
      substitute your local regional symbols for these if appropriate.

      As for your question about checking numbers, 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

      • J French

        #4
        Re: Invalid Data?

        On Mon, 27 Oct 2003 02:24:31 GMT, dfg <dfg@dfg.net> wrote:
        [color=blue]
        >I do some error checking on my textbox. The problem is that if I enter
        >a valid number, then backspace until the box is empty, my msgbox pops up
        >saying "Data Error". Is there a way to avoid this?
        >
        >Also, if I enter invalid data and try to backspace through it, my "Data
        >Error" msgbox pops up. This is rather annoying. Does anyone have a
        >solution to this problem? I posted my code - I hope it formats correctly.
        >
        >Thanks.[/color]
        Gawd - the 'fix' solution is :-[color=blue]
        >
        >
        >Private Sub txtAmtSold_Chan ge()
        > If txtAmtSold = "" Then
        > mnuEditCalculat e.Enabled = False[/color]
        Exit Sub ' <==== GET OUT ====[color=blue]
        > End If
        >
        > If IsNumeric(txtAm tSold) Then
        > If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
        > <= 100 Then
        > mnuEditCalculat e.Enabled = True
        > Else
        > mnuEditCalculat e.Enabled = False
        > MsgBox "Error: Numbers must be in the range of 1 - 100",
        >vbExclamatio n, "Range Error"
        > End If
        > Else
        > mnuEditCalculat e.Enabled = False
        > MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
        > End If
        >End Sub
        >[/color]

        However, code like that is (IMO) completely unreadable.

        Possibly because I am allergic to the use of 'Else'

        In most cases 'Select Case' provides a clearer solution than messing
        around with 'Else'

        In this case heavy use of 'Exit Sub' would clear up your logic

        Comment

        Working...