SetFocus doesn't work on modeless dialog (not an acFocus issue)

SetFocus doesn't work on modeless dialog (not an acFocus issue)

BMcAnney
Advocate Advocate
2,426 Views
7 Replies
Message 1 of 8

SetFocus doesn't work on modeless dialog (not an acFocus issue)

BMcAnney
Advocate
Advocate
The last line in my UserForm_Initialize sub is:
TextBox1.SetFocus

For some reason, it doesn't work when the UserForm is modeless. Focus does not set and I have to manually click in the text box. If I turn the dialog back to modal, it works fine.

Anyone know what is going on here?

Thanks,
Brent McAnney
0 Likes
2,427 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
hmmm... never noticed that one before. if I was to guess, id say the acFocus control is intercepting the Initialize event - because i couldnt get *any* controls to take focus on the Form.Initialize event - but was able to get any/all controls to take focus on events *after* initialize .. DoubleClick, activate, etc... so it looks like a kludge is in order...
0 Likes
Message 3 of 8

BMcAnney
Advocate
Advocate
Any suggestions on how to go about said kludge? This is actually my first attempt at using VBA and I can't think of a way to set the focus after the form initializes (without user interaction).

Brent McAnney
0 Likes
Message 4 of 8

Anonymous
Not applicable

Try setting the tab order of the textbox you want
to have focus to be first.
0 Likes
Message 5 of 8

BMcAnney
Advocate
Advocate
It actually is first (set to 0). The funny part is that the first time tab is pressed, the second textbox becomes active, even though the first one was never active.
0 Likes
Message 6 of 8

Anonymous
Not applicable
Hi Brent,

When you open a form with VBA it runs the Initialize event (if it
exists), then it runs the Activate event (if it exists). At the time of
running the Activate event, the form is visible, so a set focus may work.

Each time you hide the form, then show it, the Activate event is run
again, where as the Initialize event only runs on the first
creation/opening of the form.

Regards


Laurie Comerford


BMcAnney wrote:
> Any suggestions on how to go about said kludge? This is actually my
> first attempt at using VBA and I can't think of a way to set the focus
> after the form initializes (without user interaction). Brent McAnney
0 Likes
Message 7 of 8

BMcAnney
Advocate
Advocate
Thanks, Laurie. Unfortunately, the SetFocus method doesn't seem to work in the Activate sub either (with a modeless dialog). I know the event is firing, since I tested it by putting a msgbox in the sub. But the SetFocus just doesn't work.

Brent McAnney
0 Likes
Message 8 of 8

Anonymous
Not applicable

There are several discussions in google that
state issues with VBA, modeless forms, and setfocus.  Micro$oft
recommends using modal forms, or VB or C# which do support setfocus on modeless
forms.  One thing I forgot to mention with tab order is to not use
setfocus.  When the form activates the focus should be on the control first
in the tab order.  As a test you could select all the controls on the form,
set tab stop to false, then set the textbox to true.  If you do try this,
don't use setfocus.

 

I remember struggling with this issue before and
using msgbox caused more problems.


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Thanks,
Laurie. Unfortunately, the SetFocus method doesn't seem to work in the
Activate sub either (with a modeless dialog). I know the event is firing,
since I tested it by putting a msgbox in the sub. But the SetFocus just
doesn't work. Brent McAnney
0 Likes