How to have two Input masks?

[nateobot]

Registered User.
Local time
Today, 01:15
Joined
Jul 15, 2002
Messages
64
Hey I have a change password validation form. Standard setup with
OldPassword, NewPassword, ConfirmNewPassword.

In the underlying table the password field is limited to 10 characters. In the form I of course have the textboxes marked as password input mask. I would also like to limit the textbox to only accept 10 characters. I have code on the KeyPress event that seems to work however I was wondering if there was an easier way to do this?

my code for the keypress:

Code:
Private Sub txtNewPass_KeyPress(KeyAscii As Integer)
If Not IsNull(txtNewPass.Text) Then
    If Len(txtNewPass.Text) >= 9 Then
        txtNewPass.Text = Left(txtNewPass.Value, 10)
    End If
End If
End Sub
 
Upon closer inspection of your code, I see that it has a problem.

User enters the textbox and begins typing. When user hits the tenth character they will get a Run-time error '94' because txtNewPass.Value = Null until user leaves the textbox, at which time it is updated.

I have a suggestion for you. Not a simpler one, but a better one:

As regards unbound forms, I believe that any code for On Key Press should serve primarily to limit what a user enters.
Code:
Private Sub txtNewPass_KeyPress(KeyAscii As Integer)

If KeyAscii = 32 Then
    KeyAscii = 0 'disallows entry of a space
End If

If Len(Me.txtNewPass.Text) = 10 Then
    If KeyAscii <> 8 Then 'allows use of the backspace key
        KeyAscii = 0
    End If
End If

End Sub
This prevents users from entering any more than ten characters and does not assail them with annoying messages. If users cannot enter any further keystrokes, it's a clear message that they've reached the limits of the thing.

However, this does not take into account the possibility that a user pastes in a long line of text. Never think that they won't do it, because they will. Someday, just when you've grown comfortable, somebody will do it. A textbox will accept multi-line input, as well. This is very bad. When this happens, the user deserves an annoying message.

Assuming you have a command button for submitting the user's password, I further recommend a bit more complexity. In the On Click Event of a command button named cmdSubmit:
Code:
Private Sub cmdSubmit_Click()

With Me
    If Not IsNull(.txtNewPass) Then
        If Len(.txtNewPass) > 10 Or InStr(.txtNewPass, " ") Then 'checks length and checks for use of spaces
            MsgBox "Only 10 characters allowed. No spaces allowed." & vbCr & "Please re-enter password."
            .txtNewPass.SetFocus
            .txtNewPass = Empty
        End If
    Else
        .txtNewPass.SetFocus
    End If
End With

End Sub

Your Confirm Password textbox could complicate the use of the above. Have fun with it.

Shep
 
Yeah my darn code worked fine the first time after that kaput just like you said. :)

So I decided to make an administrative decision and say screw em. Put a nice label and told them the limits of password and all the business rules. If they go over 10 chars it pops up telling them they are stupid (not really) and then clears out the boxes making them type it all over again.

Thanks
-nate
 

Users who are viewing this thread

Back
Top Bottom