Tab control and subforms

D

Debbiedo

I have a main form that I reference for populating a corresponding
subform. Both forms use tab controls. TabClt1.Tab1 (Page 1) on the
main form corresponds to TabClt2.Tab3 (Page 1) on the subform, and
MainForm.TabClt1.Tab2 (Page 2) corresponds to Subform.TabClt1.Tab4
(Page 2). When the user clicks on the Tab of either form, I want the
corresponding Tab on the other form to show. For Tab2 I tried the
following code but it did not work.

Private Sub Tab2_Click()
Forms!MainForm!Subform.Form!TabClt2.Value = 1
End Sub

That didn't work.

I also tried just TabCtl2. Value = 1 and Me!TabClt2.Value =1 to no
avail.

So I guess I need to learn how to reference tab controls using code.
Help anyone?

Thanks

Deb
 
D

Douglas J. Steele

What does "did not work" mean? Did you get an error? If so, what was the
error? If there was no error, what did you get, and what did you want to
get?
 
G

George Nicholson

Don't use the Click event. Use the Change event.

Private Sub TabClt1_Change()

Select Case TabClt1
Case 1
' Tab one on main form was just selected...
Me.NameOfSubformControl.Form.TabClt2= 1
Case 2
Me.NameOfSubformControl.Form.TabClt2 = 1
Case Else
MsgBox "Unexpected value"
End Select

End Sub


HTH,
 
D

Debbiedo

Don't use the Click event. Use the Change event.

Private Sub TabClt1_Change()

Select Case TabClt1
Case 1
' Tab one on main form was just selected...
Me.NameOfSubformControl.Form.TabClt2= 1
Case 2
Me.NameOfSubformControl.Form.TabClt2 = 1
Case Else
MsgBox "Unexpected value"
End Select

End Sub

HTH,










- Show quoted text -

This code did not work. I was able to switch between tabs on the first
form (Form1) but nothing was switched on the second form (Form2). The
message box appeared however.

Private Sub TabCtl1_Change()
Select Case TabClt1
Case 1
' Tab one on main form was just selected...
Me.Form2.Form.TabClt2 = 1
Case 2
Me.Form2.Form.TabClt2 = 1
Case Else
MsgBox "Unexpected value"
End Select


End Sub

I also tried this:

Private Sub TabCtl1_Change()
Select Case TabClt1
Case 1
' Tab one on main form was just selected...
Me.Tab3.Form.TabClt2 = 1
Case 2
Me.Tab3.Form.TabClt2 = 1
Case Else
MsgBox "Unexpected value"
End Select


End Sub

And got the error message

Compile error.
Method or data memeber not found. ".

Tab3" was highlighted. Tab3 is the name of the first tab (Page 1) on
the subform

Also, Case 1 and 2 look the same in your code. Is this correct?

Thanks
 
G

George Nicholson

(Revised):
Private Sub TabClt1_Change()
Select Case TabClt1
Case 0
' First tab (PageIndex =0) on main form was just
selected...
' Select same index # on subform
Me.NameOfSubformControl.Form.TabClt2= 0
Case 1
Me.NameOfSubformControl.Form.TabClt2 = 1
Case Else
MsgBox "Unexpected value: " & TabClt1
End Select
End Sub

1)
The message box appeared however.
Then the PageIndex of the page being selected must be something other than 1
or 2. PageIndex numbers start with 0 so what you refer to as "page1"
probably has an index number of 0 (my bad). PageIndex numbers appear in the
property sheet for the Tab control. The code has been revised to reflect
this. Message box will also display the "unexpected" index number now.

2)
...Case 1 and 2 look the same in your code. Is this correct?
Nope. Corrected

3)
I also tried this:
Me.Tab3.Form.TabClt2 = 1
....Tab3 is the name of the first tab (Page 1) on the subform
And got the error message: Compile error. Method or data memeber not
found. ".

Well, if Me is the main form, and the main form doesn't have a control named
Tab3 **, an error message would be expected.

** The main form doesn't have a control named Tab3 because 1) its on the
subform and therefore part of the subform's Control collection and 2) a tab
control page/tab isn't considered a control anyway (see #3 below).

A couple other things worth mentioning:
1) Controls on a Tab control are referenced as if they are simply part
of the underlying form. The fact that they are on a tab control has no
effect on coding. There is no extra complexity involved.

2) The same is *not* true for subforms. To refer to a control on a
subform, you have to reference the subform and you can't refer to subforms
by their name, you have to use the name of the control that contains the
subform.
"How to Refer to Form and Subform properties and controls"
http://mvps.org/access/forms/frm0031.htm

3) Tabs/Pages on a Tab Control can be referenced in 2 ways: by PageIndex
or by the Name (not Caption) of the page.
- The "Value" property of a TabControl (the default property) reflects
the PageIndex value of the currently selected tab.
- It might help to think of the TabControl's Change event as an
AfterUpdate event since the TabControl's value already reflects the new
value, not the page you've just left.
- The first, leftmost tab will always have a PageIndex of 0. If you
physically move/add/delete tabs, the PageIndex values will be automatically
renumbered from 0 to (Pages.Count - 1), left-to-right.

Both of these would display the PageIndex value of the selected tab:
Msgbox = Me.TabCtl1
Msgbox = Me.TabCtl1.Value

Both of these would set the Tab control to the "first" page,
regardless of its name:
Me.TabCtl1 = 0
Me.TabCtl1.Value = 0

The following would display the Name of the currently selected tab:
MsgBox Me.TabCtl1.Pages(Me.TabCtl1.).Name
The following would set the Tab control to the tab named "PageOne":
Me.TabCtl1 = Me.TabCtl1.Pages("PageOne").PageIndex

Note: these last 2 examples would be the only time the Name property
of a Tab/Page would be of any practical use.

HTH,
 

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