Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3253
  • Last Modified:

Programmatically Tab to Next Control

How do you tab to the next control on your form using VBA code in an event subroutine?

I am using Access 2003 on an XP machine.
0
dastrw
Asked:
dastrw
  • 7
  • 5
  • 4
  • +1
1 Solution
 
Rey Obrero (Capricorn1)Commented:

me.controlname.setfocus

give more info on what you are trying to do
0
 
dastrwAuthor Commented:
I have a bunch of forms each of which has some controls/fields that are enabled/disabled, visible/hidden, etc.

I just want to go to the next valid field without having to know what it is.

If I use the SetFocus method then I have to know which field is visible and enabled and not locked which is too complicated at this time.

Thanks.
0
 
Rey Obrero (Capricorn1)Commented:
you may need to refer to the tab index of the controls.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dastrwAuthor Commented:
I want a single generic command that will work with any form.  Something like:

Me.TabNext

or

Me.Controls.MoveNext

I haven't found anything like that yet.
0
 
Rey Obrero (Capricorn1)Commented:
because those animals are not born yet...
0
 
Leigh PurvisDatabase DeveloperCommented:
Indeed the control's TabIndex is key (a SendKeys alternative may seem simple by comparison - but it's not worth it).
 

Function fMoveNext()
On Error Resume Next
 
    Dim ctl As Control
    Dim intInd As Integer
    Dim intTest As Integer
    Dim blnEnabled As Boolean
    Dim blnDone As Boolean
    
    intInd = Nz(Me.ActiveControl.TabIndex) + 1
    
    Do Until blnDone
        intInd = intInd Mod Me.Controls.Count
        For Each ctl In Me.Controls
            intTest = ctl.TabIndex
            If intTest = intInd Then
                blnEnabled = ctl.Enabled And ctl.TabStop = True
                If blnEnabled Then
                    ctl.SetFocus
                    blnDone = True
                    Exit For
                End If
            End If
        Next
        intInd = intInd + 1
    Loop
    
    Set ctl = Nothing
    
End Function

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
dastrw,
just curious as to how/why/where you are planning to apply the function?

when you said, you just want to go to the next field, why do you need to do it in codes?
0
 
dastrwAuthor Commented:
So here's the long sad story of why I want to do this:
1) Users are easily confused
2) Access doesn't let you attach menu bars directly to forms.  If you create a menu bar it gets stuck up with all the other general Access menu bars
3) I would like to simulate a menu bar that is attached directly to my forms rather than hiding it up with all the other Access menu bars so the easily confused users can find them.
4) I simulated a menu bar by using a combo box.  The combo box has the text "Select Action..." in it by default.  When the users click on the combo box's drop down arrow all of the possible actions are revealed.  They can then pick one and the form will do what they want.
5) After they pick an action, I replace their selection with "Select Action..." so it looks just like before.

Everything is OK, except after this whole process, the text "Select Action" is 'selected', i.e. it is in reverse-text where the text is white and the background is black.  This looks confusing to the user.

The only solution I have found is to move focus off of the control so that the text is unselected.

So instead of a mechanism to programatically move off the text, I would need something that could either:

1) Attach menu bars directly to the form
2) Unselect the text so it isn't in reverse mode
3) Somehow set the forms so that when users tabbed from field to field the cursor was set to be at the beginning or end of the text rather than selecting the entire text.  I know you can do this for Access as a whole when navigating through tables, but I don't know how you do this for form fields.

I thought about using a button or a graphic that users could click on with an invisible combo box or list box lurking behind it that would pop-up when the button or graphic was clicked on, but that was getting to be a little too convoluted.  What I have works now except for the selected text in the combo box window.
0
 
Joe OvermanEngineerCommented:
I might be missing something:
but why not just create a hidden text box and set the focus on it.  After the user selects what he/she wants from your combo box and you reset it the combo box value move the focus to a hidden text box and your text is no longer selected the user is none the wiser.
0
 
dastrwAuthor Commented:
I didn't think you could set focus to something that wasn't visible and enabled.  I may be wrong.
0
 
Joe OvermanEngineerCommented:
You are right dastrw, guess I lost my head.  But the idea is the same, set the focus one some other object other than combo box.
0
 
Rey Obrero (Capricorn1)Commented:
that was the very first suggestion...
0
 
Joe OvermanEngineerCommented:
Capricorn is right, after rereading we have the same idea and path.
0
 
dastrwAuthor Commented:
I could create a "virtually" hidden field.  This would be a very very small transparent field up in one corner of the form.  The only downside would be that if someone tabbed through all the fields they would end up there and wonder what it was.

It turns out I have Help buttons on all of the forms, and my temporary solution is just to set focus to the help buttons which will always be enabled.
0
 
Leigh PurvisDatabase DeveloperCommented:
Was there something wrong with the function I suggested?
I believe it's tested... :-s
I personally dislike small / hidden controls - I find them to be hackish.
Your Help button is an option - but I don't see why when the next control in sequence is a available...
0
 
Joe OvermanEngineerCommented:
You can also turn the tab stop off on that virtual field.
0
 
dastrwAuthor Commented:
LPurvis,

Don't worry.  You're gonna get all the points as you answered my question.

I'm just keeping this open in case someone has some more good ideas.
0
 
Leigh PurvisDatabase DeveloperCommented:
lol if it was the points I was worried about I dare say I'd strive for a bit more participation :-)
But until now it looked like you'd entirely overlooked the possibility!
And I don't really see what the alternative is given the requirement to "go to the next valid field without having to know what it is."
All your forms might have a Help button - and that's all well and good.  But assuming as much then *requires* that your forms do.  The only danger with the function as it stands is if you had a form with *no* enabled or Tabstopped controls (but that can easily be trapped for too).
The user's experience (and an understandable progression of events for them) is always important to keep in mind.  That's why we set tab orders in the first place.
But by all means - consider alternatives.  Indeed I'd always encourage you to do so.
(I still don't like hidden controls for focus setting though ;-)
0
 
dastrwAuthor Commented:
I agree with you on the focus setting and hidden objects.  I wish I could write more straightforward code.

It seems that on a number of occasions Microsoft lets you drag the horse almost to where the water trough is, but not quite close enough for him to get a drink.

Then you have to somehow teach the horse how to use a straw.
0
 
Leigh PurvisDatabase DeveloperCommented:
There's some truth to that.
But to consider this instance by way of example - the function could (and really should) be made into a generic one - such that you insert it once into a standard module in your application and then use it from any form you choose as if it were a built in piece of functionality.
You then just call
    fTabNextControl Me
from any form you choose to navigate to the next control.  
It's the ability to do things like this which extends an environment like Access beyond some end user tool for creating basic utilities.  
The requirements that are achievable with a little bit of code implemented as a standard function I'd rather see stay that way and MS concentrate on implementing new and more exciting options (or otherwise unachievable functionality).
Though if it's not much effort for us - it could be argued that it probably wouldn't be for them either ;-)

Function fTabNextControl(Optional pfrm As Access.Form)
On Error Resume Next
 
    Dim ctl As Control
    Dim intInd As Integer
    Dim intTest As Integer
    Dim blnEnabled As Boolean
    Dim blnDone As Boolean
    
    If pfrm Is Nothing Then
        Set pfrm = Screen.ActiveForm
    End If
    
    intInd = Nz(pfrm.ActiveControl.TabIndex) + 1
    
    Do Until blnDone
        intInd = intInd Mod pfrm.Controls.Count
        For Each ctl In pfrm.Controls
            intTest = ctl.TabIndex
            If intTest = intInd Then
                blnEnabled = ctl.Enabled And ctl.TabStop = True
                If blnEnabled Then
                    ctl.SetFocus
                    blnDone = True
                    Exit For
                End If
            End If
        Next
        intInd = intInd + 1
    Loop
    
    Set ctl = Nothing
    
End Function

Open in new window

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now