[RESOLVED] Again with Decimal...........
hi,
i am able to validate decimal input with TextBox. I used the following codings so that only 2 numbers are allowed aftr decimal.also, only 1 decimal is allowed in a textbox.
VB Code:
Option Explicit
Dim k, s, c
Private Sub Text1_Change()
If Len(Text1.Text) = s Then
Command1.SetFocus
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
k = Chr(KeyAscii) Like "[0-9]"
If KeyAscii = 46 And c >= 1 Then
KeyAscii = 0
End If
If KeyAscii = 46 Then
s = Len(Text1.Text) + 3
c = c + 1
End If
End Sub
the problem arises when i use backspace.i am unable to type Decimal point again.
hw shud i track whether decimal point is being erased or not.so that aftr typing decimal point for second time{(ie)aftr erasing first one using backspace},only 2 digits are allowed aftr it. :rolleyes:
plzzzz help me solve this.
Re: Again with Decimal...........
Forgot who made a numerical textbox control... try doing a search for it.
Re: Again with Decimal...........
Try something like
VB Code:
If KeyAscii = 46 Then
If InStr(1, Text1.Text, ".") = 0 Then 'there is no decimal point
'do whatever you need to do if there is no decimal point
Else
'there is a decimal point, so whatever is appopriate here
End If
End If
Re: Again with Decimal...........
this does it.. just whipped it up.. im sure it could be better
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 And KeyAscii <> 46 Then
KeyAscii = 0
Exit Sub
End If
If InStr(Text1, ".") And KeyAscii = 46 Then
KeyAscii = 0
Exit Sub
End If
If InStr(Text1, ".") And KeyAscii <> 8 Then
If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
KeyAscii = 0
Exit Sub
End If
End If
End Sub
Re: [RESOLVED] Again with Decimal...........
Hi Static , it is a beautiful code but I found a bug.
When you run this code do the following
Enter 123456 in the textbox and then using the mouse place the cursor between 3 and 4.
Now if you insert a decimal, it will let you do it so the idea of having two decimal places fails...
Re: [RESOLVED] Again with Decimal...........
ok.. its not perfect i know.. i did it quickly.
here.. happy now?
VB Code:
Private Function Nums(KA As Integer) As Integer
Nums = KA
If Not IsNumeric(Chr(KA)) And KA <> 8 And KA <> 46 Then
Nums = 0
Exit Function
End If
If InStr(Text1, ".") And KA = 46 Then
Nums = 0
Exit Function
End If
If InStr(Text1, ".") And KA <> 8 Then
If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
Nums = 0
Exit Function
End If
ElseIf InStr(Text1, ".") = 0 And KA = 46 Then
If Text1.SelStart < Len(Text1) - 2 Then
Nums = 0
End If
End If
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Nums(KeyAscii)
End Sub
Re: [RESOLVED] Again with Decimal...........
Quote:
ok.. its not perfect i know.. i did it quickly.
here.. happy now?
Awesome is the word :D