Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox accepts numeric and decimal only (How to lead with decimal??) 1

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
I am having a hard time finding what I need.

I have the code below that only allows entry of numerics and one decimal and shows in 0.00 format.

I would like the user to be able to enter a decimal as the first character when inputting data. (e.g. ".25")

Currently with this code, a zero would need to be entered first (e.g. "0.25")

Any thoughts on how to take my current code and allow a decimal to be the first thing entered by the user?

Code:
Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    txtTime.Text = Format(Round(CDec(txtTime.Text) / 0.25) * 0.25, "0.00")

End Sub

Code:
Private Sub txtTime_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc(".")
        If InStr(1, Me.txtTime.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select

End Sub
 


Hi,

I cannot duplicate your problem.

I added a textbox to a userform and then pasted your code in the forms's code window and ran.

I can lead the entry in the textbox with a decimal.

Could it be your keyboard mapping?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
No! But you got me to the solution!

The one thing I didn't mention (and the culprit) was that I was initializing that textbox = "0.00"

Therefore, the code validating to allow only one decimal was disallowing reentery of a decimal even though "0.00" was highlighted and would be written over.

Think I may just initialize with "0" and call it a day!


Thanks Skip!
 



isn't this what you want
Code:
Private Sub txtTime_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
    Case Asc("0") To Asc("9"), Asc(".")
    Case Else
        KeyAscii = 0
End Select

End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


You might need to do something like this to prevent multiple decimal points...
Code:
Private Sub txtTime_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc(".")[b]
        If Len(txtTime) = 0 Then Exit Sub
        If Split(txtTime.Text, ".")(UBound(Split(txtTime.Text, "."))) > 0 Then KeyAscii = 0[/b]
    Case Else
        KeyAscii = 0
End Select

End Sub
This assumes that the textbox is cleared before a new number is entered.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

Don’t forget to allow deleting the whole or a part of an entry, i.e. the use ot the Backspace key

Have fun.

---- Andy
 
I suggested to use the IsNumeric function to test entry (thread707-1650491). It worked for me in case of starting with empty textbox - it accepts all valid non-negative numbers. Have you tested this approach?

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top