1 Attachment(s)
Numeric Value Only in textbox
Hi! I would like to share what I have created. It just a simple could that will limit the textbox entry to numbers only.
The code is in a module so you can reuse it. BTW, it also allows you to use period(.) and negative(-). You are limited to use only one negative sign and can only be put before any number.
I have not included enter key in the module instead it is done in the keypress event of textbox.
Please leave me some reputation if you like this code!
Code edited: Now right click and paste is disabled
Re: Numeric Value Only in textbox
you can still paste in non-numeric characters from the context menu...
Re: Numeric Value Only in textbox
Yes that's true. I have not take cared of it yet and also the formatting of number such as when the user type 12 it will not make it as 12.00.
Re: Numeric Value Only in textbox
Code is already edited. I have disabled right click and paste.
Why is it that the number of views in my attachment was reset to zero?
Re: Numeric Value Only in textbox
because it's a different attachment
Re: Numeric Value Only in textbox
There's still a problem with your code. You used MouseDown event to disable right mouse click. So. Click on any TextBox with right mouse button and hold it, then press ENTER to supress the MsgBox, and release mouse button you're holding :)
Re: Numeric Value Only in textbox
Plus you can still use Shift+Insert to paste. And you could set text by SendMessage if you really want to be picky :D
Re: Numeric Value Only in textbox
'Call the function Ut_IntegerValidation on any textbox or control which has .text property
Private Sub Text1_Change()
Ut_IntegerValidation Text1
End Sub
'Paste the following code in a module
Public Function Ut_IntegerValidation(ByVal MyTextBox As Control)
If Not IsNumeric(MyTextBox.Text) Then
MyTextBox.Text = ""
ElseIf IsNumeric(MyTextBox.Text) Then
If Val(MyTextBox.Text) < 0 Then
MyTextBox.Text = ""
End If
End If
End Function