Results 1 to 7 of 7

Thread: Validation in keypress

  1. #1

    Thread Starter
    Member AnsMe's Avatar
    Join Date
    Sep 2001
    Posts
    63

    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:
    1. Private Sub txtNoBarCDMax_KeyPress(KeyAscii As Integer)
    2.  
    3. If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> 46 Then
    4.     KeyAscii = 0
    5.     Beep
    6. Else
    7.     If KeyAscii = 46 And count = 1 Then
    8.         KeyAscii = 0
    9.         Beep
    10.     ElseIf KeyAscii = 46 Then
    11.         count = 1
    12.     End If
    13. End If
    14. 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 !!!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Why is count a variant? Shouldn't it be either an Integer or a Long?

  3. #3

    Thread Starter
    Member AnsMe's Avatar
    Join Date
    Sep 2001
    Posts
    63
    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 !!!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I think it has some heartburn with the word Count. Change your declaration to an Integer and call it iCount.

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    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...
    Please rate my post.

  6. #6

    Thread Starter
    Member AnsMe's Avatar
    Join Date
    Sep 2001
    Posts
    63
    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 !!!

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Ok. I just whipped this together. See what you think.
    VB Code:
    1. Private Sub txtNoBarCDMax_LostFocus()
    2. If IsDecimal(txtNoBarCDMax.Text) Then
    3.    txtNoBarCDMax.Text = Format(txtNoBarCDMax.Text, "00000.00")
    4.    txtNoBarCDMax.Text = CDec(txtNoBarCDMax.Text)
    5.    MsgBox txtNoBarCDMax.Text
    6. End If
    7. End Sub
    8.  
    9. Private Function IsDecimal(ByVal MyValue As Variant) As Boolean
    10.     Dim ConvertValue As String
    11.     ConvertValue = CStr(MyValue)
    12.     IsDecimal = False
    13.     If IsNumeric(ConvertValue) Then
    14.         If InStr(1, MyValue, ".") Then
    15.             IsDecimal = True
    16.         End If
    17.     End If
    18. 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
  •  



Click Here to Expand Forum to Full Width