Results 1 to 3 of 3

Thread: How to stop users typing in anything but numbers in txtbox.

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    13

    How to stop users typing in anything but numbers in txtbox.

    This is a simple one.

    How do I make sure I don't get a type miss match error. For example if I have 2 text boxes which I only want integers typed in and not letters how would I code this.

    Once I have got the code going, would it be better to make say letters off limits or should I chuck up a msgbox and tell the user that only numbers can be typed?

    Is there a way of stopping all but numbers being typed into a textbox?

    Thanks

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    There is a way to only allow numbers without alerting the user that he/she types a wrong character.
    Try this code:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     'ensure we only accept numeric input
    3.     KeyAscii = NumericOnly(KeyAscii)
    4.    
    5. End Sub
    6.  
    7. Public Function NumericOnly(KeyAscii As Integer) As Integer
    8.     'if the key passed to this function is not a number
    9.     If Not IsNumeric(Chr$(KeyAscii)) And Not KeyAscii = vbKeyBack Then
    10.         'return 0
    11.         NumericOnly = 0
    12.     Else
    13.         'return the same key
    14.         NumericOnly = KeyAscii
    15.     End If
    16. End Function
    Baaaaaaaaah

  3. #3
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Here is a code I am using which only allows type numeric numbers positive or negative and also use . or , for decimal separation depending on Windows configuration
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Dim texte As String, decimalpoint As String
    3. decimalpoint = Mid$(Format(0, "0.0"), 2, 1)
    4. texte = Text1.Text
    5. If Text1.SelStart = 0 And Text1.SelLength = 0 And InStr(texte, "-") Then
    6.     KeyAscii = 0
    7.     Exit Sub
    8. End If
    9. If KeyAscii > 31 And (KeyAscii < 44 Or KeyAscii > 57 Or KeyAscii = 47) Then KeyAscii = 0
    10. If (KeyAscii = 46 Or KeyAscii = 44) And (InStr(texte, ".") Or InStr(texte, ",")) Then
    11.     KeyAscii = 0
    12. ElseIf (KeyAscii = 46 Or KeyAscii = 44) Then KeyAscii = Asc(decimalpoint)
    13. End If
    14. If KeyAscii = 45 Then
    15.     If Left$(texte, 1) <> "-" And Len(texte) > 0 Then
    16.         KeyAscii = 0
    17.         Text1.Text = "-" & texte
    18.         Text1.SelStart = Len(Text1.Text)
    19.     ElseIf Len(texte) > 0 Then
    20.         KeyAscii = 0
    21.     End If
    22. End If
    23. End Sub
    Josep Mª

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width