|
-
Nov 15th, 2001, 11:28 AM
#1
Thread Starter
New Member
Restrict # of Decimal Positions in a Text Box
Does anyone have sample code on how I can restrict a user from
entering no more than 1 digit following a decimal point in a text
box? For example:
89.9 is valid
91 is valid
93.87 is invalid
I can't seem to find a slick way to do this.
Thanks,
Ron
-
Nov 15th, 2001, 11:49 AM
#2
The following code only allows digits and a period (and only one period) with only one decimal after that period.
Be aware that this code only consider a period as a decimal sign so it's not localized for countries that use other signs (like commas in Scandinavia).
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim nPos As Long
Select Case KeyAscii
Case vbKeyBack
'We allow this so we do nothing
Case vbKey0 To vbKey9, Asc(".")
nPos = InStr(Text1, ".")
If nPos Then
If Text1.SelStart > nPos Or KeyAscii = Asc(".") Then
KeyAscii = 0
ElseIf Text1.SelStart = nPos And Len(Text1.Text) > nPos Then
KeyAscii = 0
End If
End If
Case Else
'we dont allow any other characters
KeyAscii = 0
End Select
End Sub
Best regards
-
Nov 15th, 2001, 12:11 PM
#3
Thread Starter
New Member
Thanks Joacim. Your code works like a charm.
Ron
-
Nov 15th, 2001, 12:26 PM
#4
Yes, as you might have noticed is that even if you typed one decimal you can still type in new digits before the decimal point.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|