SENSING TEXT BOX DATA TYPE?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J. C. O'Connell

    SENSING TEXT BOX DATA TYPE?

    hi,

    I HAVE A TEXT BOX THAT I WANT THE USER TO ENTER A NUMBER IN.
    HOW DO I PREVENT RUN TIME ERROR IF THEY ENTER A STRING INSTEAD?
    I GUESS I NEED SOME TYPE OF DATA TYPE SENSING COMMAND BUT I
    DONT KNOW WHAT IT IS.
    jco
  • Kris M

    #2
    Re: SENSING TEXT BOX DATA TYPE?

    Private Sub Text1_KeyPress( KeyAscii As Integer) If (KeyAscii < 48 Or
    KeyAscii > 57) And KeyAscii <> 46 Then KeyAscii = 0 End IfEnd
    Sub"J. C. O'Connell" <hifisapi@gate. net> wrote in message
    news:3f07211a.1 552457@news.gat e.net...> hi,[color=blue]
    >
    > I HAVE A TEXT BOX THAT I WANT THE USER TO ENTER A NUMBER IN.
    > HOW DO I PREVENT RUN TIME ERROR IF THEY ENTER A STRING INSTEAD?
    > I GUESS I NEED SOME TYPE OF DATA TYPE SENSING COMMAND BUT I
    > DONT KNOW WHAT IT IS.
    > jco[/color]


    Comment

    • Bruce W. Roeser

      #3
      Re: SENSING TEXT BOX DATA TYPE?

      Kris,

      Whoops ... I left off a conversion (chr$(KeyAscii) ). Use this instead.

      It's a few more keystrokes than the other example (which is also valid
      except for the backspace) but it is good programming practice to write your
      code so if someone else comes along they can understand the intent. While
      this is an overly simple section of code, developing that habit will pay
      off. The extra time making code clear can pay off big-time later when
      trying to find bugs.

      Private Sub Text1_KeyPress( KeyAscii As Integer)
      '
      ' Invalidate keystroke if not a digit, decimal point or backspace.
      '
      If (Not IsNumeric(chr$( KeyAscii)) And (chr$(KeyAscii) <> "." And
      KeyAscii <> vbBack)) Then
      KeyAscii = 0
      Endif

      End Sub

      HTH,

      -bruce

      "Kris M" <maccxoph@yahoo .com> wrote in message
      news:bYqcnc3Vu5 TTO5qiXTWJjg@co mcast.com...[color=blue]
      > Private Sub Text1_KeyPress( KeyAscii As Integer) If (KeyAscii < 48 Or
      > KeyAscii > 57) And KeyAscii <> 46 Then KeyAscii = 0 End IfEnd
      > Sub"J. C. O'Connell" <hifisapi@gate. net> wrote in message
      > news:3f07211a.1 552457@news.gat e.net...> hi,[color=green]
      > >
      > > I HAVE A TEXT BOX THAT I WANT THE USER TO ENTER A NUMBER IN.
      > > HOW DO I PREVENT RUN TIME ERROR IF THEY ENTER A STRING INSTEAD?
      > > I GUESS I NEED SOME TYPE OF DATA TYPE SENSING COMMAND BUT I
      > > DONT KNOW WHAT IT IS.
      > > jco[/color]
      >
      >
      >[/color]


      Comment

      • Bruce W. Roeser

        #4
        Re: SENSING TEXT BOX DATA TYPE?

        Close ... the user needs to be able to backspace as well. Also - write the
        code so someone else can read it.

        Private Sub Text1_KeyPress( KeyAscii As Integer)
        '
        ' Invalidate keystroke if not a digit, decimal point or backspace.
        '
        If (Not IsNumeric(chr$( KeyAscii)) And (KeyAscii <> "." And KeyAscii <>
        vbBack)) Then
        KeyAscii = 0
        Endif

        End Sub

        "Kris M" <maccxoph@yahoo .com> wrote in message
        news:bYqcnc3Vu5 TTO5qiXTWJjg@co mcast.com...[color=blue]
        > Private Sub Text1_KeyPress( KeyAscii As Integer) If (KeyAscii < 48 Or
        > KeyAscii > 57) And KeyAscii <> 46 Then KeyAscii = 0 End IfEnd
        > Sub"J. C. O'Connell" <hifisapi@gate. net> wrote in message
        > news:3f07211a.1 552457@news.gat e.net...> hi,[color=green]
        > >
        > > I HAVE A TEXT BOX THAT I WANT THE USER TO ENTER A NUMBER IN.
        > > HOW DO I PREVENT RUN TIME ERROR IF THEY ENTER A STRING INSTEAD?
        > > I GUESS I NEED SOME TYPE OF DATA TYPE SENSING COMMAND BUT I
        > > DONT KNOW WHAT IT IS.
        > > jco[/color]
        >
        >
        >[/color]


        Comment

        Working...