Another Len function not working

  • Thread starter Afrosheen via AccessMonster.com
  • Start date
A

Afrosheen via AccessMonster.com

Thanks for your time

I have this code:

Private Sub txtUser1_AfterUpdate()
On Error GoTo txtUser1_AfterUpdate_Error

If Len(Nz(Me!txtUser1, "")) = 0 Then
MsgBox "does it work"
Me.txtUser1.SetFocus
End If

On Error GoTo 0
Exit Sub

txtUser1_AfterUpdate_Error:
Err.Description = Err.Description & " In Procedure " &
"txtUser1_AfterUpdate of VBA Document Form_LoginTest"
Call LogError(Err.Number, Err.Description, "txtUser1_AfterUpdate")

End Sub

It will work if there is a space entered in the text box. If I just press
enter it will go to the next field. Even when I put in a break, if I press
enter it just goes to the next field and the afterupdate will not fire.


Thanks for your help.
 
B

BlairH

Can you try your code under OnExit instead of AfterUpdate? An update doesn't
occur if nothing was changed.

Blair
 
M

Marshall Barton

Afrosheen said:
I have this code:

Private Sub txtUser1_AfterUpdate()
On Error GoTo txtUser1_AfterUpdate_Error

If Len(Nz(Me!txtUser1, "")) = 0 Then
MsgBox "does it work"
Me.txtUser1.SetFocus
End If

On Error GoTo 0
Exit Sub

txtUser1_AfterUpdate_Error:
Err.Description = Err.Description & " In Procedure " &
"txtUser1_AfterUpdate of VBA Document Form_LoginTest"
Call LogError(Err.Number, Err.Description, "txtUser1_AfterUpdate")

End Sub

It will work if there is a space entered in the text box. If I just press
enter it will go to the next field. Even when I put in a break, if I press
enter it just goes to the next field and the afterupdate will not fire.

You can't use SetFocus that way. Instead, try using the
BeforeUpdate event:
Cancel = (Len(Nz(Me!txtUser1, "")) = 0)

Or, if the msgbox is required:
If Len(Nz(Me!txtUser1, "")) = 0 Then
MsgBox "does it work"
Cancel = True
End If
 
A

Afrosheen via AccessMonster.com

Thanks for getting back so fast. The statement will work is I press the space
bar then enter. If I just press enter the beforeupdate will not fire. It just
goes to the next field


Marshall said:
I have this code:
[quoted text clipped - 19 lines]
enter it will go to the next field. Even when I put in a break, if I press
enter it just goes to the next field and the afterupdate will not fire.

You can't use SetFocus that way. Instead, try using the
BeforeUpdate event:
Cancel = (Len(Nz(Me!txtUser1, "")) = 0)

Or, if the msgbox is required:
If Len(Nz(Me!txtUser1, "")) = 0 Then
MsgBox "does it work"
Cancel = True
End If
 
J

John W. Vinson

Thanks for getting back so fast. The statement will work is I press the space
bar then enter. If I just press enter the beforeupdate will not fire. It just
goes to the next field

That's correct. BeforeUpdate fires only when there is an update - i.e. when
something (anything other than just a <tab> or <enter>) has been typed into
the control.

If you want the code to run even if the user sets focus to the control and
leaves it without doing anything at all, you'll need to use the LostFocus
event; if you want it to run even if the user just LOOKS at the control and
does nothing with it, I don't think you can!
 
A

Afrosheen via AccessMonster.com

Hello John, we meet again.

I probably then should put it in the LostFocus control.

Thanks
 
M

Marshall Barton

But, you can't cancel the LostFocus or Exit events. You
also can't use Set Focus in the LostFocus event. so you may
have to use the Exit event.

However, users (certainly most that I know) can get
frustrated with that kind of UI. Most recommendations
strongly favor using the FORM's BeforeUpdate event to check
the validity of all fields before saving the record.
 

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