In Chapter 13, you learned how to create a multiple-page form as one way to handle displaying more data than will fit on one page of a form on your computer screen. You also learned how to control simple tabbing on the form by setting the form’s Cycle property to Current Page. One disadvantage of this approach is you can no longer use Tab or Shift+Tab to move to other pages or other records. You must use the Page Up and Page Down keys or the record selector buttons to do that. You can set Cycle to All Records to restore this capability, but some strange things happen if you don’t add code to handle page alignment.
To see what happens, open the frmXmplContactsPages form in the Conrad Systems Contacts sample database (Contacts.accdb) from the Navigation Pane. Press Page Down to move to the Home Address field for the first contact. Next press Shift+Tab once (back tab). Your screen should look something like Figure 20–11.
Figure 20–11: The form page doesn’t align correctly when you back-tab from the Home Address field in frmXmplContactsPages.
If you leave the Cycle property set to All Records or Current Record, tabbing across page boundaries causes misalignment unless you add some code to fix it. What happens is that Access moves the form display only far enough to show the control you just tabbed to. (In this example, you’re tabbing to the Notes text box control.) Open the sample frmContactsPages form that has the code to fix this problem and try the same exercise. You should discover that Shift+Tab places you in the Notes field, but the form scrolls up to show you the entire first page.
To allow tabbing across a page boundary while providing correct page alignment, you need event procedures in the Enter event for the first and last controls that can receive the focus on each page. If you examine the code behind the frmContactsPages form, you’ll find these four procedures:
Private Sub ContactID_Enter() ' If tabbing forward into this field from previous record ' align page 1 Me.GoToPage 1 End Sub Private Sub HomeAddress_Enter() ' If tabbing forward into this field, align page 2 Me.GoToPage 2 End Sub Private Sub Notes_Enter() On Error Resume Next ' If tabbing backward into the last control on page 1, align it Me.GoToPage 1 End Sub Private Sub Photo_Enter() On Error Resume Next ' If tabbing backward into the last control on page 2, align it Me.GoToPage 2 End Sub
This is arguably some of the simplest example code in any of the sample databases, but this attention to detail will make the users of your application very happy.
The code also executes when you tab backward into the ContactID and HomeAddress controls or forward into the Notes or Photo controls, or you can click in any of the controls. Access realizes that the form is already on the page requested in each case, so it does nothing.