Integers

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

    #1

    Integers

    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.



  • Raoul Watson

    #2
    Re: Integers


    "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
    news:XLujb.169$ Ux1.24@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]
    Several ways.. I usually tap into the keypress routine so assuming you have
    a text box, here is how the routine will look:

    Private Sub Text1_KeyPress( KeyAscii As Integer)
    Call num(KeyAscii)
    End Sub

    Finally the routine to check for numeric only (plus I added sample for
    period (in case decimal, and also backspace). Any other gets ignored:

    Private Sub num(KeyAscii As Integer)
    If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
    KeyAscii = IIf(Chr$(KeyAsc ii) Like "[0-9]", KeyAscii, 0)
    End Sub


    Comment

    • Rick Rothstein

      #3
      Re: Integers


      "Raoul Watson" <WatsonR@Intell igenCIA.com> wrote in message
      news:HwFjb.2889 0$Ks5.20010@nwr ddc02.gnilink.n et...[color=blue]
      >
      > "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
      > news:XLujb.169$ Ux1.24@news-binary.blueyond er.co.uk...[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[/color][/color]
      ask[color=blue][color=green]
      > > the user for a number and they enter a letter. How do you code this kind[/color]
      > of[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]
      > Several ways.. I usually tap into the keypress routine so assuming you[/color]
      have[color=blue]
      > a text box, here is how the routine will look:
      >
      > Private Sub Text1_KeyPress( KeyAscii As Integer)
      > Call num(KeyAscii)
      > End Sub
      >
      > Finally the routine to check for numeric only (plus I added sample for
      > period (in case decimal, and also backspace). Any other gets ignored:
      >
      > Private Sub num(KeyAscii As Integer)
      > If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
      > KeyAscii = IIf(Chr$(KeyAsc ii) Like "[0-9]", KeyAscii, 0)
      > End Sub
      >
      >[/color]


      Comment

      • Rick Rothstein

        #4
        Re: Integers

        > Private Sub num(KeyAscii As Integer)[color=blue]
        > If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
        > KeyAscii = IIf(Chr$(KeyAsc ii) Like "[0-9]", KeyAscii, 0)[/color]

        Two points on the above line. First, it could also be written this way...

        KeyAscii = IIf(Chr$(KeyAsc ii) Like "#", KeyAscii, 0)

        where I use the metacharacter # in place of "[0-9]". Second, there is no
        need to reassign KeyAscii back to itself if the statement is True... it
        retains its value unless changed. So, the above line could be replaced with
        this

        If Not Chr$(KeyAscii) Like "#" Then KeyAscii = 0

        or, using your original test pattern

        If Chr$(KeyAscii) Like "[!0-9]" Then KeyAscii = 0

        (Note the exclamation point in the brackets.)

        Rick - MVP


        [color=blue]
        > End Sub[/color]


        Comment

        Working...