WORKING WITH THE NATIVE ACCESS TAB CONTROL

Now it's time to check out a great feature that was new in Access 97—the native Tab control. Past versions of Access, from version 2 on, have had Tab ActiveX controls to use. Tabbed forms are very attractive and add a new dimension to your application. They allow you to display multiple pages of information without using either multiple forms or command buttons to switch between pages.

Some solutions have been provided by ActiveX control solutions, one of which, the TabStrip control, is mentioned in Chapter 14, “Programming for Power with ActiveX Controls.” The problems with the ActiveX solutions provided thus far are that

  • They need to be included with the application when distributing them.

  • They take up memory.

  • They can cause performance degradation.

  • Registration problems can occur.

  • Depending on where you get them, they might cost you money.

  • Code is needed to control paging.

Some alternative utilities have been provided, but sometimes can be cumbersome to modify and maintain. Starting with Access 97, a native Access Tab control takes care of all these situations.

An Overview of the Native Tab Control

The native Tab control includes most features of the TabStrip control, which was included in the Access 95 Developer's Toolkit. These features include the following:

  • Displaying multiple rows of tabs

  • Including bitmap images for each tab

  • Programmatically accessing the tab pages collection

The native Tab control is very quick and isn't hard to use after it's figured out. The example in this discussion is the same basic customer form as in Chapter 9. Figure 10.18 shows this form in Design view. You can find this form, TabControlExample1, in the Chap10.mdb database file. In Figure 10.18, you can see that the Tab control, rather than the individual pages, is highlighted. This is shown in two ways:

  • The most obvious is the property sheet displaying the words Tab Control on its title bar, along with the name of this Tab control. If a page instead is highlighted, the property sheet says the word Page and the name that you gave the page.

  • Look at the handles for controlling the height of the control. They're above the tabs. If a page is highlighted, the sizing handles appear below the tabs, as in Figure 10.19.

    Figure 10.18. This Tab control is included in the toolbox and is now native to Access.

    Figure 10.19. Watch the sizing handles and property sheet name to see which page you're on in the Tab control.

Watching the sizing handles is very important for keeping straight which page you're on. Also, make sure that the tab you want to place the controls on is in the foreground, again seen in Figure 10.18 with the Rental History tab.

Placing controls on the tab pages is easy—you do it in the same way as on the main form, only on the individual pages. If you click the various tabs found on the TabControlExample1 form, you can see how they work.

Creating and Editing a New Tab Control

To start working with the Tab control, create a form that's similar tab-wise to the TabControlExample1 form. You even borrow the subforms. The first set of steps creates a Tab control, inserts a new tab, and then tells you how to set the right caption on the tabs:

1.
Create a new blank form.

2.
In the New Form dialog, leave Design View as the choice to create the form with and pick the Customers table to base the form on. Click OK. You now have a blank form in Design view.

3.
Click the Tab control in the toolbox. Figure 10.20 shows a Tab control already placed on the form.

Figure 10.20. The Tab control begins with two tab pages when you first place it on a form.


Tip

Notice that the property sheet says Default Tab Control. By displaying the property sheet and then clicking the specific control, you can set default values for that control type. This way, you can set up defaults for the different control types for this form. After you set them here, the properties reflect the settings whenever a new control of this type is placed on the form.


Note

The four properties used by the Tab control, as you can see in the property sheet, include Multi Row, used for specifying using multiple row tabs, and Style, which lets you use tabs, commands, or nothing to switch pages on the Tab control. The other two properties deal with the size and height of the tabs.


4.
Right-click the Tab control. The shortcut menu includes a section for the Tab control itself: Insert Page, Delete Page, and Page Order. The last choice grouped with these commands, Tab Order, is actually for the whole form.

5.
Choose Insert Page. You now have three pages on the tab form.

If you're on the first tab page, Access inserts the new page in the first position, and the tabs display Page3, Page1, and Page2. But don't worry—if you click the Page3 tab and then view the All page of the property sheet, notice that the Page Index property is 0 (see Figure 10.21). That means that you just need to change the Caption property of the page. The other way to rearrange is to right-click the Tab control and choose Page Order. You then can rearrange to your heart's content.

Figure 10.21. When a tab page is inserted, its Page Index property is set to be between the pages where you place it, although its caption is set to be the last page.


6.
Change the name of the tabs by changing the Caption property of each and then changing the Name property. Set the Caption property for the first tab (to the left) to Main Information and the Name property to tpg.Main Information.

7.
Click the second (middle) tab. Type Relations in the Caption property for this tab and make the Name property tpgRelations.

8.
Click the third tab (to the right). Type Rental History for the Caption property and make the Name property tpgRentalHistory.

You've now set all the tabs to reflect the pages of information you want them to hold.

Moving Pages in the Tab Control

If you decide that you need the Relations page last and the Rental History page second, you can at this point simply change the Caption property for each page. However, if you have data on the pages, you would have to cut and paste the controls on each page. Rather than deal with this hassle, however, you can have Access take care of it for you.

Highlight the Tab control or one of its pages to show the resize handles. Then bring up the shortcut menu and choose Page Order to display the Page Order dialog. Highlight the Relations page in the Page Order dialog (see Figure 10.22) and click Move Down. Click OK to show the page change in the Tab control and to update the Page Index property for each page. All controls are carried with the moved page, too.

Figure 10.22. Using the Page Order dialog makes moving tab pages a snap.


Tip

The way these tab pages are named, it doesn't matter that they're reordered. If the Name properties of the pages are left as is (Page3, Page2, and so on), they would be totally out of order and confusing. By giving the pages more topical names, you don't care what the order is when you address them directly by their names.


Adding Controls to the Tab Pages

Now it's time to add controls to the Tab control pages you've created. To get ready for this, choose Field List from the View menu. Then click the Main Information tab so that the Name property reflects tpgMainInformation in the property sheet.

From the field list, drag LastName, FirstName, and Address onto the tpgMainInformation page of the Tab control. When you have the fields over the tab page, Access reflects which page has the focus by shading the page (see Figure 10.23). This is like what happens when you add a radio, toggle, or check box to an Option Group control.

Figure 10.23. The selected fields are placed on the first page of the Tab control.


Now click the Relations tab. Notice that the Tab control looks cleared—because a new, empty page is displayed. (If you don't believe me, click the Main Information tab and the fields you just placed reappear. Then click the Relations tab again.)

It's time to place a subform on the Relations page of the Tab control. Follow these steps:

1.
Make sure that the Wizard button is turned off in the toolbox.

2.
Click the Subform control and draw out a subform. Notice that the tab page area is shaded again. You now have an unbound Subform/Subreport control on the Relations page.

3.
Use the property sheet's Data page to set the Source Object property to the TabControlExampleSubTabPage2 form. The Link Child Fields and Link Master Fields properties are set automatically to CustomerID.

You've set up two out of three tab pages. To set up the Rental History page, follow the steps just given, but set the Source Object property of the subform to the TabControlExampleSubTabPage3 form.

Using Code with the Access Tab Control

Where code is necessary to switch pages with the various alternatives to the Tab control, none is necessary with the Tab control itself. However, sometimes you might want to change properties of the Tab control and individual pages on-the-fly.

The following code listings are on command buttons located on the TabControlExample2 form, which is a copy of the TabControlExample1 form, with a couple of new command buttons. This first code example merely goes through the Pages collection, which is included in the Tab control, and prints the caption to the Immediate window. You can see the code in Listing 10.2.

Listing 10.2. Chap10.mdb: Printing the Caption Property for Each Page in the Tab Control
Private Sub cmdPrintCaptions_Click()

   Dim tabMyControl As TabControl
   Dim pgCurrTabPage As Page

   Set tabMyControl = Me!TabCtl0

   For Each pgCurrTabPage In tabMyControl.Pages

      Debug.Print pgCurrTabPage.Caption

   Next pgCurrTabPage

End Sub
						

This code iterates through the Pages collection by using the For...Each VBA command. You must have the Immediate window open to see the values.

The last piece of code to show how to manipulate the Tab control actually switches the Relations and Rental History tab pages (see Listing 10.3).

Listing 10.3. Chap10.mdb: Switching the Order of Pages in the Tab Control
   Private Sub cmdSwitchTabOrder_Click()

   Dim tabMyControl As TabControl
   Dim pgCurrTabPage As Page

   Set tabMyControl = Me!TabCtl0

   For Each pgCurrTabPage In tabMyControl.Pages
      If pgCurrTabPage.Name = "tpgRelations" Then

         If pgCurrTabPage.PageIndex = 1 Then
            pgCurrTabPage.PageIndex = 2
         Else
            pgCurrTabPage.PageIndex = 1
         End If

         Exit For

      End If


   Next pgCurrTabPage

End Sub

As with Listing 10.2, the code in Listing 10.3 iterates through the Pages collection of the Tab control. However, rather than simply print the Caption property, this code checks to see whether the Name property contains the tpgRelations string. If it does, it toggles the Page Index property to either 1 or 2, thus switching the Relations and Rental History tabs.

Note

As with other Access collections, the Pages collection is based at 0, so page 1 is 0, page 2 is 1, and so on.


This should get you started creating tabbed forms using the Access Tab control. After you play with it for a while, I'm sure you'll find it as easy to use as most of the other controls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.130.201