Online Book Reader

Home Category

Access Cookbook - Ken Getz [40]

By Root 1858 0
whether or not the Shift key was depressed when the key designated by KeyCode was pressed. You want Access to ignore the keystroke if you press the PgUp or PgDn key. To make that happen, you can modify the value of the KeyCode parameter, setting it to 0. This tells Access that you want the keystroke to be ignored. Step 3 includes the code that performs this transformation. (Think what fun you could have intercepting each keystroke and converting it to something else behind the scenes, just to amuse your users!)

The sample form uses the following code, in reaction to the check box's AfterUpdate event, to control how the form reacts to keystrokes:

Private Sub chkMovement_AfterUpdate( )

If Me.chkMovement Then

Me.Cycle = acbcCycleCurrentPage

Me.OnKeyDown = "[Event Procedure]"

Else

Me.Cycle = acbcCycleAllRecords

Me.OnKeyDown = vbNullString

End If

Me.Prefix.SetFocus

End Sub

If you're going to use the techniques presented in this solution, you'll probably want to provide some method of navigating through the rows on your form. You could use the built-in navigation buttons, but you probably wouldn't have gone to this much effort if you didn't want a bit more control. The Solution in Recipe 2.6 provides a method you can use for placing your own navigation buttons on a form, giving you complete control over the look and placement of the controls. Using those controls, you can ensure that users can't move to a different row until they've satisfied your needs in the current one.

See Also


For more information on handling keystrokes, see Recipe 11.3 in Chapter 11.

2.4. Use an Option Group to Collect and Display Textual Information


Problem


Option groups are great for collecting and displaying numeric values, but sometimes you need to use an option group bound to a column of values that isn't numeric. For instance, in each row you have a field that contains just one of four different alphabetic codes. You want some way to let the user choose from those four codes on a form.

Solution


When you want a control on a form bound to a column in a table that contains a few alphabetic items, you usually can use a list or combo box to display and collect the information. Sometimes, though, you want to be able to use an option group, where you can have option buttons or even toggle buttons containing pictures. But option groups, as Access implements them, can be bound only to numeric columns.

The solution is to use an unbound option group. Rather than moving the data directly from the form to the underlying data, you'll make a pit stop along the way.

Open and run frmOptionExample in 02-04.MDB. This form, shown in Figure 2-7, pulls in two columns from the underlying table, tblShipments. Each row contains a Contents field and a Shipper field. The Shipper field can be just one of four values: UPS, Fed Ex, US Mail, or Airborne. The form displays the Contents field in a text box and the Shipper field in an option group. It also shows another text-box control: the pit stop mentioned earlier. This (normally hidden) text box is the bound control, not the option group.

Figure 2-7. Example form using an option group to store character data

To create a minimal sample form that works with the same data, follow these steps:

In 02-04.MDB, create a new form. Choose tblShipments for the form's RecordSource property.

Create controls on your new form, as shown in Table 2-2. Make sure that you've created the option group before you attempt to place any option buttons inside it. The option group should turn dark when you attempt to place an option button in it.

Table 2-2. Control properties for the new sample form

Control type

Property

Value

Option group

Name

grpCode

Option button (UPS)

Name

optUPS

OptionValue

1

Option button (Fed Ex)

Name

optFedEx

OptionValue

2

Option button (US Mail)

Name

optUSMail

OptionValue

3

Option button (Airborne)

Name

optAirborne

OptionValue

4

Text box

Name

txtShipper

ControlSource

Shipper

Create the following event procedure in the

Return Main Page Previous Page Next Page

®Online Book Reader