×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

prevent multiple decimal points during textbox input

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

Tobin Sparks
www.nov.com

RE: prevent multiple decimal points during textbox input

Look up MaskedEdit control

RE: prevent multiple decimal points during textbox input

You can test with a single line of code. Use the Split function with a "." delimiter, then use UBound to find how many strings were returned.  If more than one decimal point is found, the array will have more than 2 elements. Since UBound is zero-based, compare the result to 1.

CODE

If UBound(Split(myTextBox.Value, ".", -1, vbTextCompare)) > 1 Then
    MsgBox "Too many decimals!"
Else
    MsgBox "Input OK!"
End If

-handleman, CSWP (The new, easy test)

RE: prevent multiple decimal points during textbox input

Use the "Validation" event on your control to double-check inputs.

RE: prevent multiple decimal points during textbox input

(OP)
Howdy,

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

Tick,
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

(OP)
handleman,

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

Sorry for the less-than-useful non-advice.  Validation events are not present in VBA.

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

Textbox1.Text = CStr(CDbl(Textbox1.Text))
Use "On Error" statement to direct validation to error handling.

RE: prevent multiple decimal points during textbox input

(OP)
handleman, FrancisL and TheTick,

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 smile .
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 smile .
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

A quick warning: putting code in the KeyPress event is not sufficient to prevent the user from inputting non-numerical data.  Ctrl-V doesn't fire the KeyPress event.   

-handleman, CSWP (The new, easy test)

RE: prevent multiple decimal points during textbox input

(OP)
handleman,

Well - that's good to know.
Do you know how to send an electrical shock to the keyboard? smile Not - high voltage or anything - just a little shock. smile
Just enough to change behavior smile .

Thanks Again

Tobin Sparks
www.nov.com

RE: prevent multiple decimal points during textbox input

Try out the 'IsNumeric()' function. I would suggest using 'IsNumeric()' in the lost focus event and if the input is not numeric, pop up a message and give the focus back to the textbox.

RE: prevent multiple decimal points during textbox input

(OP)
cowski

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

"Exit" ~= "Lost Focus" for TextBox.

RE: prevent multiple decimal points during textbox input

(OP)
TheTick,

OOHH! - They hide it :-) .

Thanks

Tobin Sparks
www.nov.com

RE: prevent multiple decimal points during textbox input

You'll never be able to totally prevent any user from possibly getting invalid text into that box.  Instead of trying, do your final validation in code immediately before it is actually used in your program.   

-handleman, CSWP (The new, easy test)

RE: prevent multiple decimal points during textbox input

(OP)
handleman,

Sounds like good advice, I'll do what I can.

Thanks

Tobin Sparks
www.nov.com

RE: prevent multiple decimal points during textbox input

Here's a little snippet that makes use of the keypress event

CODE

Dim LastKeyAscii As Variant
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
When two decimal points are entered you get a beep and the orignal text is restored.  To the user it looks as if nothing happens if a second consequtive decimal point is attempted.

For training purposes OSHA may find a beep more acceptable than a tiny electric shock.bigsmile

RE: prevent multiple decimal points during textbox input

(OP)
cowski,

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

(OP)
cummings54,

That's very interesting. I really appreciate your input.
Sometimes OSHA can spoil my fun smile .

Thanks Again

Tobin Sparks
www.nov.com

RE: prevent multiple decimal points during textbox input

Cummings,

What happens if the user enters 15.51.54?

-handleman, CSWP (The new, easy test)

RE: prevent multiple decimal points during textbox input

(OP)
handleman,

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close