Online Book Reader

Home Category

Access Cookbook - Ken Getz [188]

By Root 2153 0
select which controls should carry forward values. The first part of the problem can be solved with a little VBA code to change the value of a control's DefaultValue property at runtime, squirreling away the original DefaultValue, if one exists, in the control's Tag property. The second part of the problem can be handled in a variety of ways; in this solution, we suggest using a small toggle button for each bound control that will offer the carry-forward feature.

To see an example, load the 09-05.MDB database and open the frmCustomer form in form view. Note that many of the text box controls have a small, captionless toggle button located just to their right. Navigate to the record of your choice and depress one or more of the toggle buttons to indicate that you wish to carry forward that text box's value to newly added records (see Figure 9-16). Now jump to the end of the recordset and add a new record. (A quick way to accomplish this is to click on the rightmost navigation button at the bottom of the form.) The values for the "toggled" text boxes carry forward onto the new record (see Figure 9-17). To turn off this feature for a control, click again on its toggle button to reset it to the unselected state.

Figure 9-16. The toggle buttons to the right of several text boxes have been depressed

Figure 9-17. The values of the "toggled" text boxes have been carried forward

To add this functionality to your own forms, follow these steps:

Open your form in design view. Add a small toggle button control to the right of each bound control for which you wish to add a carry-forward feature. On the frmCustomer sample form, we added toggle controls to the right of the Company, Address, City, State, Zip, Phone, and Fax text boxes. Because you can't duplicate an AutoNumber field and you're unlikely to want to carry forward a customer's first or last name, we did not add toggle buttons for these controls.

Adjust the toggle buttons' control properties to match those in Table 9-2.

Replace Phone with the label of the bound control to the left of the toggle button; replace txtPhone with the name of the bound control. Replace the Width and Height values with anything that works well on your form without unnecessarily cluttering it. We've found that a width of 0.1" works nicely with a height that matches the height of the bound control (on the sample form, the height of both the text box and the toggle button controls is 0.1667").

Table 9-2. Property settings for tglPhone on frmCustomer

Property

Value

Width

0.1"

Height

0.1667"

ControlTip

Carry forward Phone value to new records

Tag

txtPhone

OnClick

=acbCarry([Form], [Screen].[ActiveControl])

Add the following function to a global module (or import basCarryForward from 09-05.MDB):

Public Function acbCarry(frm As Form, ctlToggle As Control)

Dim ctlData As Control

Const acbcQuote = """"

' The name of the data control this toggle control serves

' is stored in the toggle control's Tag property.

Set ctlData = frm(ctlToggle.Tag)

If ctlToggle.Value Then

' If the toggle button is depressed, place the current

' carry field control into the control's DefaultValue

' property. But first, store the existing DefaultValue,

' if any, in the control's Tag property.

If Len(ctlData.DefaultValue) > 0 Then

ctlData.Tag = ctlData.DefaultValue

End If

ctlData.DefaultValue = acbcQuote & ctlData.Value & acbcQuote

Else

' The toggle button is unpressed, so restore the text box's

' DefaultValue if there is a nonempty Tag property.

If Len(ctlData.Tag) > 0 Then

ctlData.DefaultValue = ctlData.Tag

ctlData.Tag = ""

Else

ctlData.DefaultValue = ""

End If

End If

End Function

Discussion


Although there are other ways to offer this functionality to users, the toggle button control works best because it stays depressed to indicate its special state. If we had instead used a menu item or code attached to the bound control's double-click event to indicate that a control should be carried forward, users might find it difficult to remember which

Return Main Page Previous Page Next Page

®Online Book Reader