Set Focus not working after MsgBox

Í

ͯÏþÓñ

Douglas J. Steele said:
Not only that, but the fact that no error is raised by the code being in
the AfterUpdate event implies that Access has not been set to require
declaration of all variables, since there is no Cancel variable declared
in the AfterUpdate event (only in the BeforeUpdate event)

VBA should ALWAYS be set up to require declaration (for the life of me, I
can't understand why it's not the default!) Ensure that each module has
the line Option Explicit at the top (either the first or second line). To
have that line automatically added to all future modules, go into Tools |
Options while in the VB Editor, look on the Editor tab and ensure that the
"Require Variable Declaration" option is selected.
 
D

dhstein

The user enters data in a text box. If the data is not 2 characters I
display a MsgBox, clear out the field ( txbMyField.Value = "") then Set Focus
(txbMyField.SetFocus). For some reason after the Msgbox is displayed, the
cursor moves to a combobox on the form. Any ideas what's going on here?
 
T

tina

you don't say what event the code is running from. suggest you try the
following in the control's BeforeUpdate event, as

If Len(Me!txbMyField) < 2 Then
Cancel = True
MsgBox "your message here"
Me!!txbMyField.Undo
End If

because the update is cancelled, the focus never leaves the control, so you
don't have to set focus back to it.

hth
 
D

dhstein

Tina,

Thanks for the response. The event is After Update. I tried your code,
but the same thing happens. If I enter 1 character and get the error
message, focus moves to a different control.
 
K

Ken Snell [MVP]

Post all the code from your form's module. It would appear that you have
some other code running that is interfering with the code that tina
suggested (her code is right).
 
T

tina

well, if you tried the code in the AfterUpdate event, it definitely will
*not* work, because you can't cancel AfterUpdate. try running the code in
the BeforeUpdate event, as i posted previously, and post back if you have
any problem with it.

hth
 
J

Jeanette Cunningham

Using setfocus is not a straight forward thing. Access has many rules about
when it can set the focus to a control - however the rules are not written
down anywhere and even trial and error takes a long time to teach all the
do's and don't's about using set focus.
Set focus is best avoided if there is an easier alternative.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
D

Douglas J. Steele

Not only that, but the fact that no error is raised by the code being in the
AfterUpdate event implies that Access has not been set to require
declaration of all variables, since there is no Cancel variable declared in
the AfterUpdate event (only in the BeforeUpdate event)

VBA should ALWAYS be set up to require declaration (for the life of me, I
can't understand why it's not the default!) Ensure that each module has the
line Option Explicit at the top (either the first or second line). To have
that line automatically added to all future modules, go into Tools | Options
while in the VB Editor, look on the Editor tab and ensure that the "Require
Variable Declaration" option is selected.
 
J

Jeanette Cunningham

My post was intended as information for the original poster, as he had used
setfocus in the code he put on the after update event.
Pointing him in the direction of looking for other ways to do things - as a
former beginner I can remember that using setfocus seemed an easy way to get
access to do what you want. Although setfocus looks like it is your friend
any time, access doesn't quite work like that.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
T

tina

i agree, there are many ways to do things. i use SetFocus when i need it,
and don't have any problems with it. diff'rent strokes... :)
 
D

dhstein

Thanks Tina and everyone. I had the code in after update event - moved it to
before update and it works fine. Sorry I didn't see all the responses
earlier.
 
T

tina

you're welcome :)


dhstein said:
Thanks Tina and everyone. I had the code in after update event - moved it to
before update and it works fine. Sorry I didn't see all the responses
earlier.
 

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