Adding controls to the dialog box

The form properties that you change to control the appearance and behavior of a form don't affect how you add controls to the form. You can still use all the standard techniques that you'd use in Access to create a form for scrolling through records. For example, to add a bound control to the form, click a control type in the Toolbox. Then drag the underlying field's name from the Field List to the form. To add an unbound control to the form, click a control type in the Toolbox and then click the form's Design grid.

If the Control Wizard opens after you drop a control on the form, you can go through the wizard as you normally would. If you're planning to attach custom code to the control later and don't want the wizard to create the control, just click the wizard's Cancel button. Then you can assign a name, caption, and events to the control through the control's Properties sheet.

For example, the top half of Figure 9-5 shows in Design view a sample dialog box with four main controls: ReportName, LabelsToSkip, CancelBttn, and PrintBttn. In that example, the controls ReportName and LabelsToSkip are bound to fields in the SettingsTable described earlier in this section. Thus, the dialog box remembers the settings in those controls from one session to the next. The lower half of Figure 9-5 shows that same form open in Form view.










Figure 9-5:

Controls on a form (dialog box).

SkipLabels m

R-intwhich label report?: IfttfigH?:g4gi2'Labels: I Skip how.iftfrty labels? | 1Q



In the sample form shown in Figure 9-5, the CancelBttn and PrintBttn controls aren't bound to any table field. Instead, each just has some custom code tied to its On Click event. For example, the On Click event for CancelBttn is DoCmd.Close acForm, Me.Name, acSaveNo, which closes the form without saving any changes or printing.

The On Click event for PrintBttn could execute any VBA code or macro. For instance, to call the SkipLabels procedure described in Chapter 8, have that procedure execute the statement

Call SkipLabels ([ReportName].Value,LabelsToSkip].Value.

Doing so will print whatever report name appears in the ReportName control, skipping the number of labels specified in the LabelsToSkip control. The procedure also closes the dialog box. The following code shows the On Click event procedure for both controls in the class module for the sample form.

Private Sub CancelBttn_Click()

'Close the SkipLabels form without doing anything. DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

Private Sub PrintBttn_Click()

'Print the specified labels, skipping specified blanks. Call SkipLabels([ReportName].Value, [LabelsToSkip].Value)

'Then close the SkipLabels form, saving current choices. DoCmd.Close acForm, Me.Name, acSaveYes

End Sub

I help you create a much fancier SkipLabels dialog box in the sections that follow. But for now, you should be able to see how it works. The controls named ReportName and LabelsToSkip on the form serve as data to pass to the SkipLabels() Sub procedure. Clicking the Print button on the form calls the SkipLabels routine using the syntax

Call SkipLabels([ReportName].Value,[LabelsToSkip].Value)

When SkipLabels run, it prints whatever report name appears in the Report Name control on the form and also skips however many labels are specified in the LabelsToSkip control on the form.

0 0

Post a comment