tags:

views:

297

answers:

2

I have a UserForm with some textbox entry fields on it that are enabled/disabled by a checkbox. When a checkbox is clicked to check it, I'd like to move the focus into the now-enabled textbox.

The textbox is the next control after the checkbox in the tab order, so it seems like using the tab order to find the appropriate textbox would be a good idea.

But... how can I find the next control in the tab order after a given control? Is there a method to do that, or do I have to enumerate all the controls and figure it out for myself?

+1  A: 

As a different way of looking at this.

Can you not rather use the textbox you want focussed, and set that name in the checkbox.tag

then in you vba code use

DoCmd.GoToControl CheckBox.Tag

Where the CheckBox.Tag is the Textbox.Name?

EDIT:

OK, I found this

SendKeys "{Enter}", True

at VBA code for moving to next control? It must be eeeasy

astander
Thanks. I could do, but I wanted to avoid putting the name of the control anywhere, because I want to be able to copy-and-paste these controls over and over - and I don't want to have to constantly update the name in the tag (or anywhere else).
Gary McGill
OK, i updated the answer.
astander
No offence, but there is a more robust option than using send keys, send keys quite brittle in an environment where user can switch focus to another app before sendkeys fires.
Binary Worrier
Yeah, if I was going to go down that route, I'd at least use SendMessage to make sure that the message went to the right place. But, while this would no doubt work, I'll hang fire for a bit in the hope of getting a "better" answer. Actually, as things stand, I still lean towards enumerating all the controls and figuring out the next one myself.
Gary McGill
+1  A: 

I appreciate that this comes under the heading of "enumerating all the controls" but it's pretty simple and I attach the code for completeness:

Private Sub CheckBox1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.TabIndex = Me.ActiveControl.TabIndex + 1 Then
        ctl.SetFocus
        Exit For
    End If
Next

End Sub

mikemay
Yes, I ended up doing something pretty much like this - although note that not every type of control has a TabIndex property (e.g. Picture), so you need to guard against that.
Gary McGill