|
-
Feb 1st, 2002, 12:09 PM
#1
Thread Starter
Member
Validation in keypress
I have 10 regular text boxes in one form and have following code in Keypress event. User can enter only numeric values and a decimal point. The maxlength is set to 5. I don't want user to enter 2 decimal points and also user can enter only one digit after decimal point.
Valid entries Example: .1, 0.1, 15.3, 100.0, 5.3
Invalid entries example: 1..1, 0.11, 0.25, 1.1.1 etc
VB Code:
Private Sub txtNoBarCDMax_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> 46 Then
KeyAscii = 0
Beep
Else
If KeyAscii = 46 And count = 1 Then
KeyAscii = 0
Beep
ElseIf KeyAscii = 46 Then
count = 1
End If
End If
End Sub
I had declared global variable count as variant in module. But it gives me error as
"Compile Error:
Function or Interface marked as restricted, or the function uses an Automation type not supported in VB"
Please advise. What would be the ideal way to validate?
Thanx.
When you TELL THE TRUTH, you don't have to REMEMBER WHAT YOU SAID !!!
-
Feb 1st, 2002, 12:11 PM
#2
Why is count a variant? Shouldn't it be either an Integer or a Long?
-
Feb 1st, 2002, 12:14 PM
#3
Thread Starter
Member
i tried declaring as integer but it didn't work........ and received the similar error.....
When you TELL THE TRUTH, you don't have to REMEMBER WHAT YOU SAID !!!
-
Feb 1st, 2002, 12:30 PM
#4
I think it has some heartburn with the word Count. Change your declaration to an Integer and call it iCount.
-
Feb 1st, 2002, 12:36 PM
#5
Frenzied Member
I've never seen that kind of error associated with a textbox procedure. I'd try commenting out that block of coding and see what happens...
-
Feb 1st, 2002, 12:58 PM
#6
Thread Starter
Member
Hack... u r right...my code worked after changing global variable from count to icount.....how can i restrict user (in keypress) to enter only one digit after decimal point....eg 1.22
Thanx
When you TELL THE TRUTH, you don't have to REMEMBER WHAT YOU SAID !!!
-
Feb 1st, 2002, 01:10 PM
#7
Ok. I just whipped this together. See what you think.
VB Code:
Private Sub txtNoBarCDMax_LostFocus()
If IsDecimal(txtNoBarCDMax.Text) Then
txtNoBarCDMax.Text = Format(txtNoBarCDMax.Text, "00000.00")
txtNoBarCDMax.Text = CDec(txtNoBarCDMax.Text)
MsgBox txtNoBarCDMax.Text
End If
End Sub
Private Function IsDecimal(ByVal MyValue As Variant) As Boolean
Dim ConvertValue As String
ConvertValue = CStr(MyValue)
IsDecimal = False
If IsNumeric(ConvertValue) Then
If InStr(1, MyValue, ".") Then
IsDecimal = True
End If
End If
End Function
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
|