Previous Page
Next Page

Hack 19. Help Users Navigate Through Long Forms

Use Page Break controls and command buttons so that users won't have to scroll through long data-entry forms.

Information is wonderful. The more you know, the more you can do and plan forunless you are the one stuck entering the data. Then, all you can plan on is a lot of typing and mousing around.

Figure 3-2shows an entry form, and a rather long one at that. This entry form contains more entry fields than can reasonably fit onscreen, as evidenced by the scrollbar on the right of the form. Anyone using this form will need to scroll or tab through to the fields on the bottom.

Figure 3-1. A form that takes a lot of entries

A tab control is great for managing a lot of controls on a form. However, this hack is based on a real-life situation. I once worked on a project in which data-entry operators were entering information from legal-size forms. The entry screen had to match the layout of the form.

The Page Up and Page Down keys on the keyboard make it easy to scroll up and down through the form, but you can't control how much scrolling will occur. The odds that pressing Page Up or Page Down will leave the form right where you need it are indeed small.

Luckily, you can get this to work properly. All you need to do is add Page Break controls to the form. What are Page Break controls, you ask? Figure 3-2shows the Toolbox with the Page Break control pointed out. By placing page breaks strategically in the form design, you can get the Page Up and Page Down keys to scroll the form right to where you need it.

Figure 3-2. The Page Break control on the Toolbox

Figure 3-3 shows the form in Design mode. A Page Break control has been placed directly above the Personal Information section. This is a rather unobtrusive control. It simply appears as a string of dots in Design mode, and in View mode, you don't even see the control.

Figure 3-3. Adding Page Break controls

Now, when you use the Page Down key you will scroll the form directly to where the Page Break control resides. Figure 3-4 shows how the form easily scrolls to the Personal Information section.

Figure 3-4. Smart scrolling

3.2.1. Smart Navigation

Using the Page Up and Page Down keys is an adequate way to scroll a form, but we can do better. Imagine this: a form has several segregated data areas, and you need to access them in random order. Having to press Page Up or Page Down several times to get the form to where you need it is a lot of work.

To avoid this problem, you can place a series of command buttons in the form header (or form footer). The header and footer are always visible because these sections are exempt from scrolling. Figure 3-5 shows the form in Design mode, with command buttons in the header that will let users scroll to where they want.

Figure 3-5. Command buttons to facilitate form navigation

3.2.2. The Code

The trick is for each button to simulate Page Up and Page Down keystrokes. The following code uses the SendKeys statement to accomplish this. The four new buttons on the form each initiate a series of SendKeys statements:

	Private Sub cmdWorkInfo_Click()
	' 4 SendKeys up, 1 SendKeys down
	  navigate_form 4, 1
	End Sub

	Private Sub cmdPersonalInfo_Click()
	' 4 SendKeys up, 2 SendKeys down
      navigate_form 4, 2
	End Sub
	Private Sub cmdContactDetails_Click( )
	' 4 SendKeys up, 3 SendKeys down
      navigate_form 4, 3
    End Sub

	Private Sub cmdOrders_Click( )
	' 4 SendKeys up, 4 SendKeys down
	  navigate_form 4, 4
	End Sub

	Sub navigate_form(u As Integer, d As Integer)
      For form_up = 1 To u
	    SendKeys "{PGUP}"
      Next form_up
	  For form_down = 1 To d
	    SendKeys "{PGDN}"
	  Next form_down
	End Sub

Each Click event sends the number of Page Up and Page Down keystrokes that are required to have the form scroll to the desired area. These simulated keystrokes access the Page Break controls placed earlier on the form. When a button is clicked, the routine immediately sends four Page Up keystrokes. The number of keystrokes is based on this particular example. The point is to get to the top of the form. Four Page Up keystrokes are needed only if the form is currently at the bottom page break, but using four regardless of the form's location doesn't present a problem.

The code then simulates the correct number of Page Down keystrokes. The number differs for each buttonfor each area being scrolled to. In other words, the Work Info button has one Page Down, the Personal Info button has two Page Downs, and so forth.

With this approach, any area of a long form is just a single click away, and it isn't necessary for users to go through the entire form.

    Previous Page
    Next Page