Results 1 to 23 of 23

Thread: validation(resolved)

Threaded View

  1. #22
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: validation

    Did you try and figure out why it wasn't working properly? I've amended the code, this should do what you want.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim IsNegative As Boolean
    4.  
    5. Private Sub Text1_Change()
    6.  
    7.     If Left$(Text1.Text, 1) = "-" Then
    8.         Text1.MaxLength = 3
    9.         IsNegative = True
    10.     Else
    11.         Text1.MaxLength = 2
    12.         IsNegative = False
    13.     End If
    14.    
    15. End Sub
    16.  
    17. Private Sub Text1_KeyPress(KeyAscii As Integer)
    18.  
    19.     Select Case KeyAscii
    20.         Case vbKey0 To vbKey9                   ' These keys are OK
    21.             If Len(Text1.Text) = 1 Then
    22.                 If Not IsNegative Then
    23.                     If Val(Text1.Text) > 4 Then
    24.                         KeyAscii = 0
    25.                     ElseIf Val(Text1.Text) = 4 Then
    26.                         If KeyAscii > vbKey0 Then
    27.                             KeyAscii = 0
    28.                         End If
    29.                     End If
    30.                 End If
    31.             Else
    32.                 If Abs(Val(Text1.Text)) > 2 Then
    33.                     KeyAscii = 0
    34.                 ElseIf Abs(Val(Text1.Text)) = 2 Then
    35.                     If KeyAscii > vbKey0 Then
    36.                         KeyAscii = 0
    37.                     End If
    38.                 End If
    39.             End If
    40.         Case 45                                 ' 45 is '-'
    41.             If Len(Text1.Text) > 0 Then
    42.                 KeyAscii = 0
    43.             End If
    44.         Case vbKeyBack, vbKeyDelete             ' Allow backspace & delete
    45.         Case Else
    46.             KeyAscii = 0
    47.     End Select
    48.    
    49. End Sub
    I know the code is sorely lacking in comments, so why don't you spend some time stepping through the code to try and understand how it works. You may surprise yourself and come up with a better way to do it.

    BTW There is still a subtle logic error in the code. See if you can figure out what it is and then fix it.
    Last edited by pnish; Apr 20th, 2005 at 05:53 PM.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

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