Set formfield focus on Multipage

J

jacobk

Is it possible to set the focus on a formfield when selecting a Multipage tab?
If so, how do I do that?

Thanks in advance.

Jacob.
 
G

Gordon Bentley-Mix

Jacob,

In my experience this cannot be done. According to the VBA help, there are
no Events associated with the Page object. Therefore, there is nothing to
write code against, so you cannot set focus to a particular control on a page
when the page is selected. The best you can do is set focus to a particular
control when the UserForm is initialised - and then only if the Page that the
control is located on is selected (thus making the control visible) when the
UserForm is initially displayed.

However, I do note that there is a "Change" Event associated with the
MultiPage control itself, which is triggered by "electing a different page
on a MultiPage." Perhaps you could look into this further and find something
that might work there - altho I don't hold much hope, as it may be difficult
to tell exactly which page is being selected...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
L

Lasse

Hi

I have created a Userform with a Multipage containing 5 sheets/tabs. When
browsing between the different sheets it automatically selects a textbox
where the user can enter a number. I do this by setting the TabIndex to 0 on
the textbox in each sheet.
I use the Multipage_Change which Gordon refers to and it works fine. I use
it to clear the content of the textbox when changing between the sheets.

Hope this is what you are looking for.

/Lasse
 
G

Gordon Bentley-Mix

Lasse / Jacob,

I thought about this overnight and, together with Jonathan West's response
in a related post, think I've hit on a solution that doesn't rely on setting
the TabIndex of a particular control to 0 - which is actually a workable
solution and probably perfectly acceptable. However, if you want a bit more
control (and don't we all!) then something like this would probably work:

Assume a UserForm with a 3-page MultiPage control called "MultiPage1". On
Page1 there is (among other things) a TextBox called "TextBoxA" that is our
'target' TextBox, and similarly "TextBoxB" on Page2 and "TextBoxC" on Page3.

Sub MultiPage1_Change()
Select Case MultiPage1.Value
Case 1
If TextBoxA.Visible = True Then TextBoxA.SetFocus
Case 2
If TextBoxB.Visible = True Then TextBoxB.SetFocus
Case 3
If TextBoxC.Visible = True Then TextBoxC.SetFocus
End Select
End Sub

The check to see if the TextBox is visible is probably not absolutely
necessary since the only way the .Value property of the MultiPage control
could be a particular value is if the page containing the TextBox is
selected, which would (barring some other unforeseen circumstances)
automatically make the TextBox visible, but I never like to leave things to
chance and thus introduce an opportunity for error.

BTW, you could probably use a "sloppy" approach and eliminate the Select
Case statement entirely and just rely on checking the visibility of a
particular control in the MultiPage Change event, thusly:

Sub MultiPage1_Change()
If TextBoxA.Visible = True Then TextBoxA.SetFocus
If TextBoxB.Visible = True Then TextBoxB.SetFocus
If TextBoxC.Visible = True Then TextBoxC.SetFocus
End Sub

Or you could be slightly more elegant (but still just as "sloppy") and
retain the Select Case statement but modify what is being evaluated in it -
something like this:

Sub MultiPage1_Change()
Select Case True
Case TextBoxA.Visible
TextBoxA.SetFocus
Case TextBoxB.Visible
TextBoxB.SetFocus
Case TextBoxC.Visible
TextBoxC.SetFocus
End Select
End Sub

Many ways to skin this cat, but all rely on the MultiPage Change event. My
apologies for dismissing it so quickly before.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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