Integers/Strings

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

    #1

    Integers/Strings

    When asking a user to input a number it's easy to make sure it's higher,
    lower etc then a specified amount. What I'm struggling with is if you ask
    the user for a number and they enter a letter. How do you code this kind of
    thing to make sure that a string is entered as a string or an integer is
    entered as an integer?
    I'm new to VB so I apologise for asking a question that is probably very
    easy.



  • Roy Riddex

    #2
    Re: Integers/Strings

    Sorry for the double post, it seemed the first post hadn't been sent so a
    phone call to blueyonder sorted it out.


    Comment

    • J French

      #3
      Re: Integers/Strings

      On Thu, 16 Oct 2003 11:49:35 +0100, "Roy Riddex"
      <roy_riddexNOSP AM@blueyonder.c o.uk> wrote:
      [color=blue]
      >When asking a user to input a number it's easy to make sure it's higher,
      >lower etc then a specified amount. What I'm struggling with is if you ask
      >the user for a number and they enter a letter. How do you code this kind of
      >thing to make sure that a string is entered as a string or an integer is
      >entered as an integer?
      >I'm new to VB so I apologise for asking a question that is probably very
      >easy.[/color]

      Look at Val() and Str|()

      If Val( Text1.Text ) > 123 Then Call Barf

      Personally I like creating things back into a String and testing them
      again - it is ... belt and braces


      Comment

      • Roy Riddex

        #4
        Re: Integers/Strings

        "J French" <erewhon@nowher e.com> wrote in message
        news:3f8e9865.2 7380963@news.bt click.com...[color=blue]
        > On Thu, 16 Oct 2003 11:49:35 +0100, "Roy Riddex"
        > <roy_riddexNOSP AM@blueyonder.c o.uk> wrote:
        >[color=green]
        > >When asking a user to input a number it's easy to make sure it's higher,
        > >lower etc then a specified amount. What I'm struggling with is if you ask
        > >the user for a number and they enter a letter. How do you code this kind[/color][/color]
        of[color=blue][color=green]
        > >thing to make sure that a string is entered as a string or an integer is
        > >entered as an integer?
        > >I'm new to VB so I apologise for asking a question that is probably very
        > >easy.[/color]
        >
        > Look at Val() and Str|()
        >
        > If Val( Text1.Text ) > 123 Then Call Barf
        >
        > Personally I like creating things back into a String and testing them
        > again - it is ... belt and braces
        >
        >[/color]
        Are you saying that Val(Text1.Text) will always be greater than an Integer
        no matter what the letters entered are?
        What is 'Call Barf'?


        Comment

        • Roy Riddex

          #5
          Re: Integers/Strings

          [color=blue][color=green]
          > > Look at Val() and Str|()
          > >
          > > If Val( Text1.Text ) > 123 Then Call Barf
          > >
          > > Personally I like creating things back into a String and testing them
          > > again - it is ... belt and braces
          > >
          > >[/color]
          > Are you saying that Val(Text1.Text) will always be greater than an Integer
          > no matter what the letters entered are?
          > What is 'Call Barf'?
          >
          >[/color]
          I tried your method in a simple form using an Input box and an Ok Command
          button. However, it still didn't work. Below is the code, if possible can
          someone please check it over and tell me where I'm going wrong. All I want
          to do is to make sure that a number is entered and not a letter.

          Private Sub cmdOK_Click()
          Dim Number As Integer
          If Val(Text1.Text) > 123 Then
          Text1.Text = ""
          Text1.SetFocus
          Else
          Number = Text1.Text
          Print Number
          End If
          End Sub


          Comment

          • Stephane Richard

            #6
            Re: Integers/Strings

            In the KeyDown event of your textbox you could test

            if (KeyCode < 48 or Keycode > 57) And Keycode <> 45 And Keycode <> 46 Then
            KeyCode = 0
            End if

            This will simply dissallow the users from entering letters in the first
            place which will save you validation :-)....48 to 57 is 0 through 9
            respectively... 45 is the - sign and 46 is the . (dot if you wnat to allow
            decimal values).

            --
            Stéphane Richard
            "Ada World" Webmaster



            "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
            news:anxjb.1070 $Ux1.217@news-binary.blueyond er.co.uk...[color=blue]
            >[color=green][color=darkred]
            > > > Look at Val() and Str|()
            > > >
            > > > If Val( Text1.Text ) > 123 Then Call Barf
            > > >
            > > > Personally I like creating things back into a String and testing them
            > > > again - it is ... belt and braces
            > > >
            > > >[/color]
            > > Are you saying that Val(Text1.Text) will always be greater than an[/color][/color]
            Integer[color=blue][color=green]
            > > no matter what the letters entered are?
            > > What is 'Call Barf'?
            > >
            > >[/color]
            > I tried your method in a simple form using an Input box and an Ok Command
            > button. However, it still didn't work. Below is the code, if possible can
            > someone please check it over and tell me where I'm going wrong. All I want
            > to do is to make sure that a number is entered and not a letter.
            >
            > Private Sub cmdOK_Click()
            > Dim Number As Integer
            > If Val(Text1.Text) > 123 Then
            > Text1.Text = ""
            > Text1.SetFocus
            > Else
            > Number = Text1.Text
            > Print Number
            > End If
            > End Sub
            >
            >[/color]


            Comment

            • Rick Rothstein

              #7
              Re: Integers/Strings

              > In the KeyDown event of your textbox you could test[color=blue]
              >
              > if (KeyCode < 48 or Keycode > 57) And Keycode <> 45 And Keycode <> 46[/color]
              Then[color=blue]
              > KeyCode = 0
              > End if
              >
              > This will simply dissallow the users from entering letters in the first
              > place which will save you validation :-)....48 to 57 is 0 through 9
              > respectively... 45 is the - sign and 46 is the . (dot if you wnat to allow
              > decimal values).[/color]

              But it won't protect against the user pasting in bad data.

              Rick - MVP


              Comment

              • Rick Rothstein

                #8
                Re: Integers/Strings

                > ....<snip>... All I want to do is to make sure that a number[color=blue]
                > is entered and not a letter.[/color]

                To decide 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

                • Karl S

                  #9
                  Re: Integers/Strings

                  Just use the IsNumeric() function to check the value before doing anything
                  with it. Try the code sample below with "123" and "123b" and see if that is
                  what you are looking for.

                  Dim strMyString As String
                  strMyString = "123b"
                  'strMyString = "123"

                  If IsNumeric(strMy String) Then
                  MsgBox strMyString & " is numeric."
                  Else
                  MsgBox strMyString & " is not numeric."
                  End If

                  HTH,
                  K


                  "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
                  news:CIujb.124$ Ux1.101@news-binary.blueyond er.co.uk...[color=blue]
                  > When asking a user to input a number it's easy to make sure it's higher,
                  > lower etc then a specified amount. What I'm struggling with is if you ask
                  > the user for a number and they enter a letter. How do you code this kind[/color]
                  of[color=blue]
                  > thing to make sure that a string is entered as a string or an integer is
                  > entered as an integer?
                  > I'm new to VB so I apologise for asking a question that is probably very
                  > easy.
                  >
                  >
                  >[/color]


                  Comment

                  • Rick Rothstein

                    #10
                    Re: Integers/Strings

                    > Just use the IsNumeric() function to check the value before doing anything[color=blue]
                    > with it. Try the code sample below with "123" and "123b" and see if that[/color]
                    is[color=blue]
                    > what you are looking for.
                    >
                    > Dim strMyString As String
                    > strMyString = "123b"
                    > 'strMyString = "123"
                    >
                    > If IsNumeric(strMy String) Then
                    > MsgBox strMyString & " is numeric."
                    > Else
                    > MsgBox strMyString & " is not numeric."
                    > End If
                    >
                    > HTH,
                    > K[/color]

                    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

                    Working...