Prevent 2 Decimal Points Entry in a Textbox

B

Bob Barnes

Does anyone have code to allow entry of ONLY one Decimal Point (KeyAscii
Character 46) in a text box???

I was playing w/ code like this, but NOT getting the desired result.

Public Sub DecZeroNine(KeyAscii)
Dim B, Ctl As Control
Set Ctl = Screen.ActiveControl
MsgBox Screen.ActiveControl
If ((KeyAscii = 8) Or _
(KeyAscii >= 48 And KeyAscii <= 57)) Then
' it's a 0-9 or a Backspace (8)
ElseIf (KeyAscii = 46) Then
MsgBox KeyAscii
' it's ar Decimal Pt (46)
B = InStr(1, Ctl, ".")
MsgBox B
If IsNull(B) Then
MsgBox "Null"
' OK - 1st "."
Else
MsgBox "No DecPt Allowed"
KeyAscii = 0
End If
Else
' eat it
KeyAscii = 0
End If
End Sub

TIA - Bob
 
B

Brian

f the text box is always numeric and you can live with preventing the udpate
of the box (instead of preventing the user from even typing the second
decimal point), how about formatting the text box with, for example, General
Number? Then, if the user enters a second decimal point, it will generate an
error that you can trap and/or pass on to the user.

If you really must prevent entry in the first place, try this in the
KeyPress event:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 Then
If Str(Text1.Text) Like "*.*" Then
MsgBox "No second decimal allowed"
SendKeys "{BACKSPACE 1}"
End If
End If
End Sub
 
B

Brian

Hmmm...on further thought..the code in my prior post doesn't work if the
decimal points are consecutive key strokes or at the beginning of the entry.
Try this instead:

If KeyAscii = 46 Then
If Len(Text1.Text) = 0 Then Exit Sub 'bypass check if this is the first
character
If Not IsNumeric(Text1.Text & Chr(KeyAscii)) Then
MsgBox "No second decimal allowed"
SendKeys "{BACKSPACE 1}"
End If
End If
End Sub
 
M

Marshall Barton

Bob said:
Does anyone have code to allow entry of ONLY one Decimal Point (KeyAscii
Character 46) in a text box???
[snip]

Private Sub Text14_Change()
Dim intPos As Integer
Dim intLen As Integer

intPos = InStr(Text14.Text, ".")
intLen = Len(Text14.Text) - 1
If 0 < intPos And intPos < intLen Then
Beep
Text14.Text = Left(Text14.Text, intLen)
Text14.SelStart = intLen
End If
End Sub
 
B

Bob Barnes

Will try your 2nd Post - I figure if the User makes an Entry error, it's
likely to be 2 decimal points back-to-back.

Thank you - Bob
 
B

Bob Barnes

Brian - thank you. I'll try this using Ctl
as Screen-ActiveControl. I have MANY
textboxes & want to use a single Subroutine.

Bob
 
B

Bob Barnes

Marsh - I'll try it - Thank you - Bob

Marshall Barton said:
Bob said:
Does anyone have code to allow entry of ONLY one Decimal Point (KeyAscii
Character 46) in a text box???
[snip]

Private Sub Text14_Change()
Dim intPos As Integer
Dim intLen As Integer

intPos = InStr(Text14.Text, ".")
intLen = Len(Text14.Text) - 1
If 0 < intPos And intPos < intLen Then
Beep
Text14.Text = Left(Text14.Text, intLen)
Text14.SelStart = intLen
End If
End Sub
 
B

Bob Barnes

Brian - I skightly modified your code so I can use the same Sub on ALL
textboxes.
(Code is below). It WORKS on 2 Decimal places entered consecutively.
However,if I enter 1.3. ---> the {BACKSPACE} deletes the 3 because the 2nd
Decimal Point won't enter (which is what we want).

Is there a way to "examine" if a "non-decimal pt" - (like a 3) which is to
the immediate left of what would be the 2nd decimal pt entered???

My code..
Public Sub DecZeroNine(KeyAscii)
Dim Ctl As Control
Set Ctl = Screen.ActiveControl
If ((KeyAscii = 8) Or _
(KeyAscii >= 48 And KeyAscii <= 57)) Then
' it's a 0-9 or a Backspace (8)
ElseIf (KeyAscii = 46) Then
If Len(Ctl.Text) = 0 Then Exit Sub 'bypass check if this is the first
character
If Not IsNumeric(Ctl.Text & Chr(KeyAscii)) Then
MsgBox "No second decimal allowed"
SendKeys "{BACKSPACE}"
End If
Else
' eat it
KeyAscii = 0
End If
End Sub

TIA - Bob
 
T

Tim Ferguson

Does anyone have code to allow entry of ONLY one Decimal Point (KeyAscii
Character 46) in a text box???

if not isnumeric(MyString) Then
cancel = True
msgbox "You have to enter a real number"

else
' it's a valid entry

endif


.... let the platform do the work, not yourself!

HTH


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top