Link to home
Start Free TrialLog in
Avatar of ronallard
ronallard

asked on

Move to next tTab on TabCtl using VBA

I know I can programatically move to the next tab on the tabctl using:
[Tab2Name].SetFocus

My problem - My tabs are named Tab0 - Tab5. I am on Tab1, in a subform. The subform has a button called "ContinueButton". In the onclick event I would like to move to Tab2. I tried several renditions of the command the closest, "  Forms![frmProcessReports]![TabCtl0].[Tab2].SetFocus  ", but errors on object does not accept this action. Anyone know how to code that?
Avatar of BrianWren
BrianWren

Let's say your main form is named frmMain, and your sub, "Frm Sub Form", is in a control named frmSub, and your control is TabCtl0.

Private Sub Continue_Click()

  Forms![frmMain]![frmSub].Form![TabCtl0].[Tab2].SetFocus  

End Sub

To refer to a subform, you must follow the control name, (the name of the control with the subform in it, not the name of the subform.  They are usually the same, but not always), with the property '.Form'.

Brian
Howdy from Dallas, ronallard.
Good one, Brian, I struggled with this one for a while myself.  The easiest way I have found to change tabs in a tab control is to set the .Value property of the tab control.  Let's say the tab control, tabCtrlA, is on SubForm1 of the MainForm, and we're starting from some control on the MainForm.  The code would go something like this:

'get to the subform
Me!SubForm1.SetFocus
'increment to the next tab
Me!tabCtrlA.Value = Me!TabCtrlA.Value+1

Note that this doesn't explicitly SetFocus to anything, it just sets the next tab as showing on-screen.  Also you get an error if your setting is '6' when you only have 5 tabs.  So, do your thing about controlling the action (you know).

Reading and Setting the .Value property can let you move between the tabs very efficiently.  Works for me.

Good luck as usual...
Avatar of ronallard

ASKER

I haven't tried any of these cause this looks like you think the Tab control is on the subform. The tab control is actually on the main form. Lets try it this way... frmParent contains TabCtl0 which contains Page0 - Page5. I select Page1 which has a subform control, (ChildWindow). sbfChild, loaded in 'ChildWindow', runs several queries then enables 'ContinueButton'. Still running sbfChild, how do I tell it to go to "Page3 of TabCtl0 on frmParent"?
Good morning, ronallard.

Either of these will do it:
    Me!TabCtl0.Pages(3).SetFocus
    ... or ...
    Me!TabCtl0.Value = 3

If you need to qualify the statement more fully use "frmParent.TabCtrl0" instead of "Me!TabCtrl0".  That should do it.

good luck as usual...
Sorry, I forgot to mention this, but the tab control page numbers are 0-based (0, 1, 2...), so if the page 3 you mentioned is the *third* page of the control, then its number would be 2.

good luck as usual...
ASKER CERTIFIED SOLUTION
Avatar of BrianWren
BrianWren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good one, Brian, I had overlooked that!
Sorry Tom - but when I tried these the only ones that worked are the ones with the Me.Pareent on them. Your syntax gave error "Microsoft Access can't find field TabCtl0 ...." Thanks for the help Brian. Both instructions worked as you had suggested.
Brian knows!  I'm currently working in Access 2000, maybe that why.  Glad you have a solution.

good luck as usual...