Limit password textbox to 4 letter

M

magicdds

I have a text box on a form where the user can enter their password. I have
the following code to not allow the user to continue typing more than 4
letters into the text box:

Private Sub Password_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyReturn Then
If Len(Password.Text) = 0 Then KeyCode = 0
If Len(Password.Text) <> 4 Then KeyCode = 0
End If

End Sub

---------------------------------------------------------------
Private Sub Password_KeyPress(KeyAscii As Integer)

If Len(Password.Text) >= 4 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
Beep
End If
End If

End Sub


This all worked fine. But, now that it works, I changed the Input Mask of
the PASSWORD text box to Password, and I get an error message:

"Microsoft Access can't retrieve the value of the property Len(Password.Text)"

Does anyone have an idea of how to solve this problem?

Thanks
Mark
 
K

Ken Snell \(MVP\)

Put a hidden textbox on the form. Name it txtCharacters.

Use the Change event of the Password control to write the value of the .Text
property into that textbox:

Private Sub Password_Change()
Me.txtCharacters.Value = Me.Password.Text
End Sub

Then change your code to test the length of the Me.txtCharacters.Value
property intstead of Me.Password.Text property.
 
M

magicdds

I just changed

Me.txtCharacters.Value

to

Me!txtCharacters.Value

and this worked great.

Thank you very much for your help.
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top