Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetFocus not working

10 views
Skip to first unread message

JMMach

unread,
Mar 11, 2003, 9:13:43 AM3/11/03
to
I have a user form which has text boxes for First Name and Last Name. Upon
EXIT of the First Name text box, we confirm that the First Name is correct.
If it is then the Last Name text box becomes active; if it is not then the
First Name text box is to remain active. Problem is that the SetFocus does
not set the focus on the First Name text box; it goes to the Last Name text
box. I can't figure out why my code is failing. Can anyone help?

This is the troubled bit of code:

Private Sub txtFName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim intAns As Integer
Dim strFirstName As String

strFirstName = txtFName
intAns = MsgBox("Is This name:" & vbCr & _
strFirstName & vbCr & _
" correct?", vbQuestion + vbYesNo, "Final Answer?")

If intAns = vbYes Then
txtLName.SetFocus 'make the next (Last Name) text box the active
text box
Else
txtFName.SetFocus 'make the First Name text box active again
End If
End Sub


Thanks for your help.

JMMach


Chip Pearson

unread,
Mar 11, 2003, 9:27:29 AM3/11/03
to
Set the Cancel parameter to True to keep focus in the text box.
E.g.,


If intAns = vbYes Then
txtLName.SetFocus

Cancel = False
Else
txtFName.SetFocus
Cancel = True
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com ch...@cpearson.com

"JMMach" <jmm...@gto.net> wrote in message
news:Ow5sTh95...@TK2MSFTNGP11.phx.gbl...

JMMach

unread,
Mar 11, 2003, 10:05:16 AM3/11/03
to
Thanks Chip that worked.
But would you explain the Cancel = False and Cancel = True a bit more.
If intAns = vbYes then
what does Cancel = False not cancel; and if
else
what does Cancel = True cancel?
These details are not clear to me. Thanks again.
JMMach


"Chip Pearson" <ch...@cpearson.com> wrote in message
news:O9ckap95...@TK2MSFTNGP09.phx.gbl...

Chip Pearson

unread,
Mar 11, 2003, 10:30:11 AM3/11/03
to
The Exit event provides a parameter called Cancel that allows you
to cancel the actual Exit of the control. If you leave this alone,
or set Cancel = False, the form will move to the next control in
the tab order. SetFocus doesn't work here because the Exit code
hasn't finished. When it does finish, VBA automatically changes the
focus to the next control.

The Cancel parameter allows you to override VBA's default behavior.
When you set Cancel = True, you are telling VBA to cancel the exit
of the control and leave focus on that control.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com ch...@cpearson.com


"JMMach" <jmm...@gto.net> wrote in message

news:Oq23G#95CHA...@TK2MSFTNGP12.phx.gbl...

JMMach

unread,
Mar 11, 2003, 10:36:56 AM3/11/03
to
Thanks Chip, that clears the fog.
JMMach

"Chip Pearson" <ch...@cpearson.com> wrote in message

news:O4VpcM#5CHA...@TK2MSFTNGP12.phx.gbl...

0 new messages