non numeric textbox manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kpomeru
    New Member
    • May 2010
    • 4

    non numeric textbox manipulation

    ok i have a textbox say textbox1.text and i want to make sure when a non numeric value is entered it exits sub or does nothing.. thanks
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Did you search the Forum or Google..?

    Comment

    • Guido Geurs
      Recognized Expert Contributor
      • Oct 2009
      • 767

      #3
      This is the code from a call in Bytes:
      Only integers can be entered (can be optional for your project)
      The first lines lets only numbers be entered !

      Code:
      Private Sub Text1_Change()
          With Text1
      '§ check for letters
              If Not IsNumeric(.Text) And (.Text) <> "" Then
                  MsgBox ("Enter a NUMBER !")
      '§ clear the last character
                  .Text = Left(.Text, Len(.Text) - 1)
      '§ set the cursor at the end of the string
                  .SelStart = Len(.Text)
              Else
      '§ check for DOT or COMMA
                  If InStr(.Text, ".") Or InStr(.Text, ",") Then
                      MsgBox ("Enter an INTEGER value !")
                  Else
      '§ enter your code....
      '            MsgBox ("This is OK")
                  End If
              End If
          End With
      End Sub

      Comment

      • kpomeru
        New Member
        • May 2010
        • 4

        #4
        thanks i used the first line of code for the textbox

        if not isnumeric(textb ox1.text) then
        'do nothing
        textbox1.text = "0"
        end if

        thanks so much, i used that for the textfield for a button click calculator

        Comment

        • vb5prgrmr
          Recognized Expert Contributor
          • Oct 2009
          • 305

          #5
          Just exclude alpha characters from being entered...
          Code:
          Option Explicit
          
          Private Sub Text1_KeyPress(KeyAscii As Integer)
          
          Select Case KeyAscii
          Case vbKeyBack
          Case vbKey0 To vbKey9
          Case Else
            KeyAscii = 0
          End Select
          End Sub

          Good Luck

          Comment

          Working...