I have ... a combo-box with 4 answers: AnswerA, AnswerB, AnswerC & AnswerD.
I also have 4 tabs on it: TabA, TabB, TabC & TabD
1. For changing the active tab of the TabControl we use:
Where "<TabControlName>" is the name of the TabControl and "<TabName>" is the name of the tab we want to select.
2. The result of the expression we need to use then is something like (here for selecting "TabB" from a TabControl named "TabCtl0"):
3. The common feature that relates each possible value of the ComboBox ("AnswerA", "AnswerB", "AnswerC" and "AnswerD") and the name of each Tab ("TabA", "TabB", "TabC" and "TabD") is the last letter which is the same for both ("A", "B", "C" and "D").
4. We must then build the name of the Tab we want to select by adding the last letter of the value in the ComboBox to the common part in the name of each Tab: "Tab"
5. The VBA function that returns the last letter of a string expression or value is: Right(Value, CharacterCount). So, to extract the last letter from the value in the ComboBox, we use the expression (if the name of the ComboBox is "Combo_SelectTab"):
Which (from 3. hereabove) will return "A", "B", "C", or "D".
6. From 4. hereabove, the returned letter must be added to the litteral string "Tab" which is the beginning of the name of each Tab in the TabControl. So the name of the desired Tab can be obtained from this expression:
"Tab" & Right(Me.Combo_SelectTab.Value, 1)
7. From 1. and 2. hereabove, we can then write (again for a TabControl named "TabCtl0" and a ComboBox named "Combo_SelectTab"):
8. As the Tab must change when the value of the ComboBox changes, we shall use this statement in response to the AfterUpdate event for the ComboBox, as this event occurs immediately after the selected item in the ComboBox (which is represented by its Value property) is being changed:
Private Sub Combo_SelectTab_AfterUpdate()
Me.TabCtl0.Pages("Tab" & Right(Me.Combo_SelectTab.Value, 1)).SetFocus