Share via

SetFocus method is not working - Help?

Anonymous
2012-03-26T22:00:19+00:00

I have the following code on the lost focus event.  I want to stop a user that just tabs through the field.

Private Sub Sub_LostFocus()

If IsNull(Me.Sub) Then

MsgBox "You must enter a modality"

Me.Sub.SetFocus

End If

End Sub

Issue is that the cursor goes to the next field - it does not go back to the sub.  Can someone tell me why and how to fix this?  If I put the code on the next field using the on focus event it works.  So I think the issue is how do you cancel the tab that was done so you don't just move to the next filed?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-03-30T01:16:54+00:00

I have put it on the exit event as you suggest.  How do I use the cancel argument?  I am self taught so my education here is lacking.  Thanks.

If you look at the event procedure shell that Access builds for you, you'll see that it looks something like this:

'------ start of code ------

Private Sub Sub_Exit(Cancel As Integer)

End Sub

'------ end of code ------

You see the "Cancel" argument?  Although it is typed as Integer, it's really a Boolean, and if you set it to True it will cancel the Exit event and keep the focus from leaving the control.  So for your purposes, you might write:

'------ start of code ------

Private Sub Sub_Exit(Cancel As Integer)

    If IsNull(Me.Sub) Then

        MsgBox "You must enter a modality"

        Cancel = True

    End If

End Sub

'------ end of code ------

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2012-03-27T00:50:53+00:00

I have the following code on the lost focus event.  I want to stop a user that just tabs through the field.

Private Sub Sub_LostFocus()

If IsNull(Me.Sub) Then

MsgBox "You must enter a modality"

Me.Sub.SetFocus

End If

End Sub

Issue is that the cursor goes to the next field - it does not go back to the sub.  Can someone tell me why and how to fix this?  If I put the code on the next field using the on focus event it works.  So I think the issue is how do you cancel the tab that was done so you don't just move to the next filed?

When the LostFocus event fires, the focus hasn't actually moved to the next control yet, it's still on the original control, *about* to move elsewhere.  So setting the focus to that original control accomplishes nothing.

Instead, use the Exit event, which has a Cancel argument that you can set to True to keep the focus from leaving the control.

I should say in passing that I rarely try to keep the user from moving freely around the controls on a form.  Instead, I check in the form's BeforeUpdate event to make sure that all required controls are filled in.  That gives more freedom to the user, while still enforcing my requirements, and also is proof against the possibility that the focus never reaches some required field in the first place.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-03-29T23:35:02+00:00

    I have put it on the exit event as you suggest.  How do I use the cancel argument?  I am self taught so my education here is lacking.  Thanks.

    Was this answer helpful?

    0 comments No comments