Results 1 to 10 of 10

Thread: VB - Numeric Textbox input only

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved VB - Numeric Textbox input only

    As you type the text

    VB Code:
    1. Private Sub Textbox1_KeyPress(index As Integer, KeyAscii As Integer)
    2. 'Accepts only numeric input
    3. Select Case KeyAscii
    4.   Case vbKey0 To vbKey9
    5.   Case vbKeyBack, vbKeyClear, vbKeyDelete
    6.   Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
    7.   Case Else
    8.     KeyAscii = 0
    9.     Beep
    10. End Select
    11. End Sub
    Clicking a command button to validate

    VB Code:
    1. Private Sub Command1_Click()
    2.     If Not IsNumeric(Text1.Text) Then
    3.         MsgBox "Please enter numbers only.", vbInformation
    4.         'you may also consider erasing it
    5.         Text1.Text = ""
    6.     End If
    7. End Sub
    Last edited by x-ice; Dec 14th, 2006 at 01:18 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: VB - Numeric Textbox input only

    You know, every textbox has a "Validate" event...
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     If Not IsNumeric(Text1.Text) Then
    3.         MsgBox "Please enter numbers only.", vbInformation
    4.         'you may also consider erasing it
    5.         Text1.Text = ""
    6.        
    7.         Cancel = True
    8.     End If
    9. End Sub
    Much better than checking in multiple places...

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: VB - Numeric Textbox input only

    My code checks as each character is typed, therefore literally stopping you typing text

  4. #4
    Lively Member
    Join Date
    Oct 2012
    Location
    Caracas, Venezuela
    Posts
    121

    Re: VB - Numeric Textbox input only

    Hi x-ice,

    I'm pretty new working with VB. Actually I'm usign your code to have a numeric textbox. Thank you and thank VB Forums. I have another problem: What do I need to to allow negative numbers and limit to specific number of decimals?

    Yor help will be highly appreciated.

    Necalabria

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    47

    Re: VB - Numeric Textbox input only

    Private Sub txtCTC_KeyPress(KeyAscii As Integer)
    If Chr(KeyAscii) Like "[a-z]" And KeyAscii <> 8 Then KeyAscii = 0
    End Sub

    This works good.

  6. #6
    New Member
    Join Date
    Aug 2013
    Posts
    1

    Re: VB - Numeric Textbox input only

    I think it is also good to mention for newer programmers that you can add as many validations as you want with "If Else" statements to clean up code using your second set of code
    ex. from personal project for a POS system for a coffee shop
    If Not IsNumeric(CappacinoTextBox.Text) Then
    MsgBox("Please enter numbers only.", vbInformation)
    'Validate CappacinoTextBox
    CappacinoTextBox.Text = ""
    ElseIf Not IsNumeric(LatteTextBox.Text) Then
    MsgBox("Please enter numbers only.", vbInformation)
    'Validate LatteTextBox
    LatteTextBox.Text = ""
    ElseIf Not IsNumeric(ChaiTeaTextBox.Text) Then
    MsgBox("Please enter numbers only", vbInformation)
    'Validate ChaiTeaTextBox
    ChaiTeaTextBox.Text = ""
    End If

    Will validate every one of those text boxes in one block of code so anyone who picks up your code later can get all your validations in one place.

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: VB - Numeric Textbox input only

    Text1_Validate(Cancel As Boolean) doesn't work


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: VB - Numeric Textbox input only

    laudeniold has a good idea to start with but it doesn't stop typing in special characters
    Last edited by jmsrickland; Aug 16th, 2013 at 10:43 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    New Member
    Join Date
    Sep 2013
    Posts
    1

    Thumbs up good job

    this works really well ♥

  10. #10
    New Member
    Join Date
    Oct 2013
    Posts
    2

    Re: VB - Numeric Textbox input only

    Quote Originally Posted by x-ice View Post
    My code checks as each character is typed, therefore literally stopping you typing text
    But it doesn't consider pasting characters, for instance, when using the edit box's context menu.

    Therefore, you will need additional code in the Change or Validate events.

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