Cancel the action of Tab in KeyPress

E

Ed

I have a two 'page' table (actually it is a series of text boxes on a
multipage UserForm)

I use tab to go from textbox to textbox. When I go from the last text box on
page 1, need to display page two and put the focus in the textbox1 of page
2.

I wrote a Keypress routine to handle that. Problem is, the tab expresses
itself as a 'tab' when it hits the second page textbox1. Can I cancel the
operation of the tab once VBA performs the keypress commands? (I have all
"TabKeyBehaviors" set to false.)

-Ed in (Virginia)
 
J

Jay Freedman

I have a two 'page' table (actually it is a series of text boxes on a
multipage UserForm)

I use tab to go from textbox to textbox. When I go from the last text box on
page 1, need to display page two and put the focus in the textbox1 of page
2.

I wrote a Keypress routine to handle that. Problem is, the tab expresses
itself as a 'tab' when it hits the second page textbox1. Can I cancel the
operation of the tab once VBA performs the keypress commands? (I have all
"TabKeyBehaviors" set to false.)

-Ed in (Virginia)

Hi Ed,

The KeyPress routine is the wrong one to use. For starters, the help says:
~~~~
A KeyPress event does not occur under the following conditions:
- Pressing TAB.
- Pressing ENTER.
- Pressing an arrow key.
- When a keystroke causes the focus to move from one control to another.
~~~~

Instead, write a KeyDown routine for the last box on page 1. Make sure the first
textbox on page 2 has the lowest tab index of any control on that page -- that
will ensure that it gets focus when the code switches pages.

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = Asc(vbTab) Then
MultiPage1.Value = 1
End If
End Sub
 
E

Ed

Jay,

Ooops. I meant to say "KeyDown" instead of "KeyPress". I actually have
had the routine working pretty well, except, as the first post indicated,
the Tab key expresses itself as a Tab when it moves into that first box on
the second page. (Tab 'whitespace' is added before the new insertion point.)

So the essence of my question is "how can I 'read' that a tab was
pressed in that last box on the first page, but stop VBA from expressing
that tab when it reaches the destination text box at the top of the second?"
(I can SendKeys a backspace, and that clears the tab whitespace, but it
seems that there should be a cleaner way.)

Thanks.

Ed (in Virginia)
 
J

Jay Freedman

Hi Ed,

I had a working userform with multipage control and textboxes when I wrote my
reply, and I didn't see any tab character in the destination textbox. The change
of focus 'eats' the tab keypress, just as it does when going from one textbox to
another on the same page. Since that was a 'plain-vanilla' setup, I suspect
you've added something to your userform that you shouldn't.

If you want to send me a copy of your template containing the userform, I'll see
if I can figure out what's happening.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

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