Tab Controls

H

hotplate

When using a tab control on a form, is it possible to change which
page displays when a combo box selection is made?

Also, if this is possible, how would I make it so that tab's page
displays when navigating through the records based on the prior combo
box selection?
 
D

Dale Fye

It's simpler to just call the AfterUpdate event of the combo box from the
Current event:

Private Sub Form_Current()

Call yourComboName_AfterUpdate

End sub
--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
D

David W. Fenton

To select the tabbed page based on the combobox selection:

Private Sub YourComboName_AfterUpdate()
Select Case YourComboName
Case "PageOne"
Me.YourTabControl = 0
Case "PageTwo"
Me.YourTabControl = 1
Case "PageThree"
Me.YourTabControl = 2
Case Else
Me.YourTabControl = 0
End Select
End Sub

Notice that the Tabbed Page index is Zero-based. The first page's
value is 0, the second is 1, etc.

I have recently run into a situation where I had to add pages to a
tab and the logical order of the pages changed the page indexes.
Because of that, I'm moving away from using indexes for working with
tabs. For instance, in the OnChange event of the tab control, I use:

Select Case Me!ctlTab.Pages(Me!ctlTab).Name
Case "PageOne"
...
Case "PageTwo"
...
End Select

and so forth.

Unfortunately, I can't figure out a way to set the value of the tab
based on the name. The only thing I can think of is a Byzantine
routine that walks through the Pages collection and checks the name,
and then returns the value.

Public Function GetTabValueFromPageName(ctl As Control, _
strPageName As String) As Integer
' returns -1 if strPageName does not exist
' otherwise returns value for strPageName
Dim i As Integer
Dim intReturn As Integer

intReturn = -1
For i = 0 To ctl.Pages.Count - 1
If ctl.Pages(i).Name = strPageName Then
intReturn = i
Exit For
End If
Next i
GetTabValueFromPageName = intReturn
End Function

Am I just missing something that would be much easier?

In any case, with the function above, and if your listbox returns
the name of the tab page, then you could replace the original code
quoted at the top with this:

Private Sub YourComboName_AfterUpdate()
Me!YourTabControl = GetTabValueFromPageName (Me!YourTabControl, _
Me!YourComboName)
End Sub

I'd be delighted to hear that there's an easier way and that I'm
just being obtuse.
 
D

David W. Fenton

It's simpler to just call the AfterUpdate event of the combo box
from the Current event:

Private Sub Form_Current()>
Call yourComboName_AfterUpdate
End sub

Meh. I find that it's often unreliable attempting to do that, so if
I need to call the same code in more than one event, I'll write a
private subroutine that both events can call. Seems much cleaner to
me.
 
D

Dale Fye

David,

I had the same problem, but am using the tabs caption, instead of the name.

Dale
 
D

Dale Fye

David,

Any particular circumstances where you have had problems with this? I
cannot recall ever having a problem doing it this way. Although I agree that
your way is probably cleaner.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
D

David W. Fenton

Any particular circumstances where you have had problems with
this? I cannot recall ever having a problem doing it this way.
Although I agree that your way is probably cleaner.

I can't recall the exact circumstances, since it was so long ago
that I started avoiding doing it! The problem was the for whatever
reason, the code compiled but at runtime, couldn't find to sub. Or
maybe it was the other way around, that it threw a compile error but
worked? Also, as I seem to recall, the problem seemed to usually
happen if I was referring to an event in a subform or parent from
from the other parent or child (respectively). Using the PUBLIC
keyword didn't always resolve it.

But I honestly can't remember the specifics. It was confusing to me
and I couldn't figure out the pattern, so I coded around it in a way
that, in my opinion, was cleaner in the long run.
 

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