Befor Update Len() VBA Not Working

xyba

Registered User.
Local time
Today, 22:25
Joined
Jan 28, 2016
Messages
189
Hi

Can anyone tell me why I may be getting a runtime error when executing this code (the code in red is what's causing the error apparently):

Code:
Private Sub Pwdtxt0_BeforeUpdate(Cancel As Integer)
If Len(Pwdtxt0) < 8 Then
    MsgBox "Please check your password and try again.  Passwords must be more than 8 characters!"
[COLOR="Red"]      Me.Pwdtxt0 = Null
      Me.Pwdtxt = Null
        Me.Pwdtxt0.SetFocus[/COLOR]

End If
End Sub

I have the below similar code which works perfectly on another control:

Code:
Private Sub Pwdtxt_AfterUpdate()
If Me.Pwdtxt.Value <> Me.Pwdtxt0.Value Then
      MsgBox "Passwords Don't Match", vbOKOnly
      DoCmd.RunCommand acCmdUndo
      Me.Pwdtxt0 = Null
      Me.Pwdtxt = Null
        Me.Pwdtxt0.SetFocus
Else
 
Hi. What was the error description?


Edit: Nevermind, I see the problem. You can't modify the same control in its BeforeUpdate event (it's like an infinite loop).
 
Shouldn't you be invoking the cancel argument?
Code:
If Len(Pwdtxt0) < 8 Then
Cancel = true
...
 
Agree with Pat. I like to see what I typed wrong so I dont repeat it.

Code:
        Me.Pwdtxt0.SetFocus
        Me.Pwdtxt0.SelStart = 0
        Me.Pwdtxt0.SelLength = len(me.Pwdtxt0)
This will select the text so you can see it and then replaces it as you start to type.
 

Users who are viewing this thread

Back
Top Bottom