only numbers function (1 Viewer)

amir0914

Registered User.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
53
Hi guys, I have many text boxes and they should accept only numbers, it means no one can type letters on text box. can someone give me a function to put on text boxes event??

Thanks in advanced.
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 14:56
Joined
Apr 27, 2015
Messages
2,892
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.
 

amir0914

Registered User.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
53
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.
I knew this before, is there a way to do this without any massage?? i'm sure is a way with vba code.
 

jdraw

Super Moderator
Staff member
Local time
Today, 08:56
Joined
Jan 23, 2006
Messages
13,194
You could check the textbox with IsNumeric() and take a selected path based on the result of the check. Perhaps the textbox has no value?? You could check that as well.
God luck.
 

theDBguy

I’m here to help
Local time
Today, 05:56
Joined
Oct 29, 2018
Messages
10,692
Hi. You could try using the KeyPress event to silently reject/ignore non-numeric characters.
 

missinglinq

AWF VIP
Local time
Today, 08:56
Joined
Jun 20, 2003
Messages
6,370
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.)
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
Be warned that without a message, some users may think that there keyboard/computer is broken!

Linq ;0)>
 

theDBguy

I’m here to help
Local time
Today, 05:56
Joined
Oct 29, 2018
Messages
10,692
Hi Linq. Thanks for the assist. Perhaps use a Beep rather than do nothing? Just a thought...
 

amir0914

Registered User.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
53
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.)
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
Be warned that without a message, some users may think that there keyboard/computer is broken!

Linq ;0)>
Thank you buddy, the code was great.
 

isladogs

CID Moderator
Staff member
Local time
Today, 13:56
Joined
Jan 14, 2017
Messages
13,379
In this code, do nothing means those keys are allowed so you definitely don't want a beep or message.
Where that would be helpful is the Case Else where the key code is remapped to nothing i.e.blocked

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
 

theDBguy

I’m here to help
Local time
Today, 05:56
Joined
Oct 29, 2018
Messages
10,692
Hi Colin. Thanks for correcting me. I meant to say a Beep might be an option when the key is ignored/discarded. Cheers!
 

Micron

AWF VIP
Local time
Today, 08:56
Joined
Oct 20, 2018
Messages
3,102
I suppose you could just use numeric values rather than vb constants...
Code:
Select Case KeyCode
    Case 48 To 57
    Case 96 To 105
    Case Else
    Beep
    keycode = 0
End Select
EDIT - nope, that doesn't allow you to tab off. But

Case 9

would.
Just figured out that most/all numerics can go on one line
Code:
Select Case KeyCode
  Case 8, 9, 13, 48 To 57, 96 To 105
  Case Else
  Beep
  KeyCode = 0
End Select
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 08:56
Joined
Jun 20, 2003
Messages
6,370
...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
...
That's true...but the provided code uses the KeyDown event.

Glad we could help, amir0914!

Good luck with your project!

Linq ;0)>
 

missinglinq

AWF VIP
Local time
Today, 08:56
Joined
Jun 20, 2003
Messages
6,370
...Just figured out that most/all numerics can go on one line
That's true...but I'm getting old and forgetful...and I like making things as self explanatory as possible!

Linq ;0)>
 

Micron

AWF VIP
Local time
Today, 08:56
Joined
Oct 20, 2018
Messages
3,102
That's true...but I'm getting old and forgetful...and I like making things as self explanatory as possible!
Me too; but I'm lazy also...
Besides, there are only so many characters in a keyboard bucket. You don't want to run out, do you?
 

theDBguy

I’m here to help
Local time
Today, 05:56
Joined
Oct 29, 2018
Messages
10,692
It wasn't specified, but I wonder if decimal point is a valid input. Just curious...
 

Micron

AWF VIP
Local time
Today, 08:56
Joined
Oct 20, 2018
Messages
3,102
It wasn't specified, but I wonder if decimal point is a valid input. Just curious...
Good point! Add 110 and 190 to the numbers?
Depends on the situation, I guess.
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom