Right click on the Text Box and choose properties. On the format tab, change the entry to General Number.
This will give you the default error message. If you want something a little more specific, you will need to add some code.
Private Sub [COLOR="Red"]YourTextBoxName[/COLOR]_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 48 To 57
'Do nothing: Numerical characters (digits) are allowed
Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab
'Do nothing: Allow these keys to be used
Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9
'Do nothing: Allow input from Numbers Keypad
Case Else
'Don't allow anything but digits to be used
KeyCode = 0
End Select
End Sub
Here's a hack from the vaults that's the kind of thing that theDBguy suggested...(Replaceing YourTextBoxName with the actual name of your Control.)
Be warned that without a message, some users may think that there keyboard/computer is broken!Code:Private Sub [COLOR="Red"]YourTextBoxName[/COLOR]_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case 48 To 57 'Do nothing: Numerical characters (digits) are allowed Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab 'Do nothing: Allow these keys to be used Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9 'Do nothing: Allow input from Numbers Keypad Case Else 'Don't allow anything but digits to be used KeyCode = 0 End Select End Sub
Linq ;0)>
Select Case KeyCode
Case 48 To 57
Case 96 To 105
Case Else
Beep
keycode = 0
End Select
Select Case KeyCode
Case 8, 9, 13, 48 To 57, 96 To 105
Case Else
Beep
KeyCode = 0
End Select
...Note for others who read this thread in the future.
For that type of key press procedure to work, you must also set KeyPreview =Yes in the form properties...
...Just figured out that most/all numerics can go on one line
Me too; but I'm lazy also...That's true...but I'm getting old and forgetful...and I like making things as self explanatory as possible!
Good point! Add 110 and 190 to the numbers?It wasn't specified, but I wonder if decimal point is a valid input. Just curious...