|
-
Jul 18th, 2005, 06:26 AM
#1
Thread Starter
Fanatic Member
VB - Numeric Textbox input only
As you type the text
VB Code:
Private Sub Textbox1_KeyPress(index As Integer, KeyAscii As Integer)
'Accepts only numeric input
Select Case KeyAscii
Case vbKey0 To vbKey9
Case vbKeyBack, vbKeyClear, vbKeyDelete
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Clicking a command button to validate
VB Code:
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "Please enter numbers only.", vbInformation
'you may also consider erasing it
Text1.Text = ""
End If
End Sub
Last edited by x-ice; Dec 14th, 2006 at 01:18 PM.
-
Jul 19th, 2005, 03:31 PM
#2
Re: VB - Numeric Textbox input only
You know, every textbox has a "Validate" event...
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) Then
MsgBox "Please enter numbers only.", vbInformation
'you may also consider erasing it
Text1.Text = ""
Cancel = True
End If
End Sub
Much better than checking in multiple places...
-
Jul 19th, 2005, 04:13 PM
#3
Thread Starter
Fanatic Member
Re: VB - Numeric Textbox input only
My code checks as each character is typed, therefore literally stopping you typing text
-
Oct 30th, 2012, 06:45 PM
#4
Lively Member
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
-
Aug 8th, 2013, 09:47 AM
#5
Member
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.
-
Aug 13th, 2013, 02:47 PM
#6
New Member
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.
-
Aug 16th, 2013, 10:20 PM
#7
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.
-
Aug 16th, 2013, 10:21 PM
#8
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.
-
Sep 15th, 2013, 05:01 PM
#9
New Member
good job
-
Oct 18th, 2013, 09:01 AM
#10
New Member
Re: VB - Numeric Textbox input only
 Originally Posted by x-ice
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|