prevent multiple decimal points during textbox input
prevent multiple decimal points during textbox input
(OP)
Howdy All,
I'm looking for a good way to detect if the user has entered more than one decimal point into a TextBox. I'm trying to prevent a error due to multiple decimal points.
This seems like it should be easy and there's probably a bunch of ways to do this, but I've been looking and can't seem to find even one.
I was thinking of trying to get the decimal point (.) count in a string.
Can anyone help?
Thanks
I'm looking for a good way to detect if the user has entered more than one decimal point into a TextBox. I'm trying to prevent a error due to multiple decimal points.
This seems like it should be easy and there's probably a bunch of ways to do this, but I've been looking and can't seem to find even one.
I was thinking of trying to get the decimal point (.) count in a string.
Can anyone help?
Thanks
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
RE: prevent multiple decimal points during textbox input
CODE
MsgBox "Too many decimals!"
Else
MsgBox "Input OK!"
End If
-handleman, CSWP (The new, easy test)
RE: prevent multiple decimal points during textbox input
RE: prevent multiple decimal points during textbox input
Thank you ALL. This seems so simple ans basic it should be easier to find. I'll check it out later today.
Thanks Again
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
I don't see the Validation event available for a TextBox control in VBA. Is it only in VB6/.net, or am I just looking in the wrong spot?
-handleman, CSWP (The new, easy test)
RE: prevent multiple decimal points during textbox input
I was thinking the same thing, but, I did't get to look again just to make sure. I'm currently using a Keyup (or down) event for input. I could use a change event also.
Thanks
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
When trying to make a user-proof app, you can easily spend more than half your programming time on grooming user input.
Other ways to trigger validation:
- Use KeyDown, KeyUp, or KeyPress events to detect when Tab or Enter keys are pressed.
- Use textbox's Enter & Exit events which are triggered when the object gets/loses focus.
A quick way to validate is to just assign the textbox contents to a variable and return it to the textbox.CODE
RE: prevent multiple decimal points during textbox input
Thanks for your efforts in answering my question.
handleman - Thanks for the above code. It works perfect in a Change event. The text entry event I'm using is Keypress .
I have to tell you though, I'm not familiar with "Split function" so I'm only saying that it works but I can't say why .
I'd have never come up with it myself or even thought to search for it that way.
Thanks Again
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim ch As String
' Allow only numbers and "." to be entered in the box
ch = Chr$(KeyAscii)
If Not (ch >= "0" And ch <= "9" Or ch = ".") Then KeyAscii = 0
End Sub
Private Sub QTBallTxtSpace_Change()
' Limit the number of decimal points to avoid an error
If UBound(Split(QTBallTxtSpace.Value, ".", -1, vbTextCompare)) > 1 Then MsgBox "Too many decimals points "
End Sub
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
-handleman, CSWP (The new, easy test)
RE: prevent multiple decimal points during textbox input
Well - that's good to know.
Do you know how to send an electrical shock to the keyboard? Not - high voltage or anything - just a little shock.
Just enough to change behavior .
Thanks Again
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
RE: prevent multiple decimal points during textbox input
Thanks for your response. Apparently a "lost focus" event is not available in SW API for a TextBox.
It appears the best way to disable Ctrl+V is to disable this Windows function at the start up of the form, then enable at exit.
Not real happy with that though.
Thanks Again
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
RE: prevent multiple decimal points during textbox input
OOHH! - They hide it :-) .
Thanks
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
-handleman, CSWP (The new, easy test)
RE: prevent multiple decimal points during textbox input
Sounds like good advice, I'll do what I can.
Thanks
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
CODE
Dim LastTextBox1 As String
Private Sub TextBox1_GotFocus()
LastKeyAscii = 0
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 46 And (LastKeyAscii = KeyAscii) Then
Beep
TextBox1.Text = LastTextBox1
End If
LastKeyAscii = KeyAscii
LastTextBox1 = TextBox1.Text
End Sub
For training purposes OSHA may find a beep more acceptable than a tiny electric shock.
RE: prevent multiple decimal points during textbox input
Thanks for the 'IsNumeric()' clue. I found a good way to use it to validate input. Very helpful.
Thanks
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
That's very interesting. I really appreciate your input.
Sometimes OSHA can spoil my fun .
Thanks Again
Tobin Sparks
www.nov.com
RE: prevent multiple decimal points during textbox input
What happens if the user enters 15.51.54?
-handleman, CSWP (The new, easy test)
RE: prevent multiple decimal points during textbox input
I'll still catch the erroneous input when the code validates the input after the user pulls the trigger.
I'm still open to a little electrical shock :-) .
Thanks
Tobin Sparks
www.nov.com