Using the Toolbox to Add Label and Text Controls

Using the Form Wizard or the AutoForm feature of Access 2003 simplifies the generation of standard forms for displaying and updating data in tables. Creating forms from scratch in Form Design view by adding controls from the Toolbox provides much greater design flexibility than automated form generation. The examples in this chapter use the HRActions table that you created in Chapter 5, and a query, qryHRActions, which you create in the next section.

For more information on creating the data source for this chapter and establishing the correct relationships, see "Creating the HRActions Table," p. 182.


Tip

graphics/power_tools.gif

If you haven't created the HRActions table, you can import it from the Forms15.mdb database in your \Program Files\Seua11\Chaptr15 folder or from the \Seua10\Chaptr15 folder of the accompanying CD-ROM.


Creating the Query Data Source for the Main Form

The HRActions table identifies employees uniquely by their sequential ID numbers, located in the EmployeeID field. As before, you need to display the employee's name and title on the form to avoid entering records for the wrong person. The form design example in this chapter uses a one-to-many query to provide a single source of data for the new, custom HRActions form.

To create the HRActions query that serves as the data source for your main form, follow these steps:

  1. graphics/table_wizard.gif Close any open Northwind forms, click the Tables shortcut in the Database window, and select HRActions in the table list.

  2. graphics/query_design_window.gif Click the New Object: Query toolbar button to open the New Query dialog, and click OK with Design View selected to open Query1 with the HRActions table added. (Don't worry if your query's name contains a different number.)

  3. graphics/show_table.gif Click the Show Table toolbar button to open the Show Table dialog, and add the Employees table to your query. Click the Close button to close the Show Table dialog.

  4. If you defined relationships for the HRActions table as described in Chapter 5, the upper pane of the query window appears as shown in Figure 15.3. The line connecting the two tables indicates that a many-to-one relationship exists between the EmployeeID field in the HRActions table and the EmployeeID field of the Employees table.

    Figure 15.3. The upper pane of the Query Design window displays the many-to-one relationship between the Employees and HRActions tables.

    graphics/15fig03.gif

    Tip

    If you didn't define any relationships, the join line doesn't appear. In this case, you need to drag the EmployeeID field from the HRActions field list to the EmployeeID field of the Employees field list to create a join between these two fields.

  5. Click the * field of the HRActions table, and then drag and drop it in the first column of the Query Design grid. This adds all the fields of the HRActions table to your query.

  6. From the Employees table, click and drag the LastName, FirstName, Title, HireDate, Extension, ReportsTo, and Notes fields to columns 2 through 8 of the Query grid, respectively, as shown in Figure 15.4.

  7. To simplify finding an employee, click the Sort row of the LastName column and select an Ascending sort.

  8. graphics/run_toolbar.gif Click the Run toolbar button to check your work, and then close the new query. Click Yes when the message box asks if you want to save the query.

  9. In the Save As dialog, name the query qryHRActions and click OK.

Figure 15.4. The query includes all fields (*) from the HRActions table and seven fields from the Employees table.

graphics/15fig04.gif

Now that you've created the query that provides a unified data source for the main form, you're ready to begin creating your custom multitable form.

Creating a Blank Form with a Header and Footer

When you create a form without using the Form Wizard, Access opens a default blank form to which you add controls that you select from the Toolbox. To open a blank form to begin duplicating the form you created with the Form Wizard in Chapter 14, do the following:

  1. graphics/form_wizard.gif In the Database window, click the Forms shortcut, and click the New button to open the New Form dialog.

  2. With the default Design view selected in the upper list of the New Form dialog, select qryHRActions in the lower drop-down list (see Figure 15.5). Click OK.

    Figure 15.5. Select qryHRActions as the data source for the new form in the New Form dialog.

    graphics/15fig05.gif

  3. Access creates a new blank form with the default title Form1. By default, the Toolbox and the field list for the qryHRActions query open. Click the Maximize button of the Form Design window to expand the form to fill the document window.

    Tip

    graphics/toolbox.gif

    graphics/field_list.gif If the Toolbox or field list isn't visible, click the appropriate button on the Form Design (top) toolbar. If the grid doesn't appear on the form, choose View, Grid.

  4. Choose View, Form Header/Footer to add a header and footer to the form.

The default width of blank forms is 5 inches. The default height of the Form Header and Footer sections is 0.25 inch, and the height of the Detail section is 2 inches. To adjust the height of the form's Detail section and the width of the form, do this:

  1. Place the mouse pointer on the top line of the Form Footer bar. The mouse pointer becomes a double-headed arrow with a line between the heads. Hold down the left mouse button and drag the bar to create a Detail section height of about 3.0 inches, measured by the left vertical ruler. The active surface of the form, which is gray with the default 24 x 24 grid dots, expands vertically as you move the Form Footer bar downward, as shown in Figure 15.6.

    Figure 15.6. When you drag a section header or margin, the mouse pointer changes to a line with a double-headed arrow.

    graphics/15fig06.gif

  2. Minimize the Form Footer section by dragging the bottom margin of the form to the bottom of the Form Footer bar.

  3. Drag the right margin of the form to 6 inches as measured by the horizontal ruler at the top of the form.

Adding a Label to the Form Header

The label is the simplest control in the Toolbox to use. By default, labels are unbound and static, and they display only the text you enter. Static means that the label retains the value you originally assigned as long as the form is displayed, unless you change the Caption value with VBA code. To add a label to the Form Header section, complete the following steps:

  1. graphics/label.gif Click the Toolbox's Label button. When you move the mouse pointer to the form's active area, the pointer becomes the symbol for the Label button, combined with a crosshair. The center point of the crosshair defines the position of the control's upper-left corner.

  2. Locate the crosshair at the upper-left of the Form Header section. Press and hold down the left mouse button while you drag the crosshair to the position for the lower-right corner of the label (see Figure 15.7).

    Figure 15.7. Drag the symbol for the control, a label in this example, from the upper left to the lower right to define a rectangle that represents the size of the control.

    graphics/15fig07.gif

    Note

    As you drag the crosshair, the outline of the container for the label follows your movement. The number of lines and characters that the text box can display in the currently selected font appears in the status bar.

  3. If you move the crosshair beyond the bottom of the Form Header section, the Form Header bar moves to accommodate the size of the label after you release the left mouse button. When the label is the size you want, release the mouse button.

  4. The mouse pointer becomes the text-editing cursor inside the outline of the label. Type Human Resources Action Entry as the text for the label, and click anywhere outside the label to finish its creation. If you don't type at least one text character in a label after creating it, the box disappears the next time you click the mouse.

  5. Choose File, Save, and type the name frmHRActionEntry in the Form Name text box of the Save As dialog. Click OK.

For tips on manipulating elements of a form, see "Selecting, Moving, and Sizing a Single Control," p. 546.


You use the basic process described in the preceding steps to add most of the other types of controls to a form. (Some Toolbox buttons, such as the graph and command buttons, launch a Control Wizard to help you create the control if the Control Wizards button is activated.)

After you add the control, you use the anchor and sizing handles described in Chapter 14 to move the control to the desired position and to size the control to accommodate the content. The location of the anchor handle determines the Left (horizontal) and Top (vertical) properties of the control. The sizing handles establish the control's Width and Height property values.

Formatting Text and Adjusting Text Control Sizes

graphics/font.gif graphics/font_size.gif When you select a control that accepts text as the value, the typeface and font size combo boxes appear on the toolbar (refer to Figure 15.7). To format the text that appears in a label or text box, do the following:

  1. graphics/properties_window.gif Click the Human Resources Action Entry label you created in the preceding section to select the label. If the Properties window isn't open, click the Properties toolbar button. Alternatively, double-click the unselected label.

    graphics/access_2002_new_icon.gif

    Access 2002 added a drop-down list to the Properties window that lets you select any object on the form or report. Selecting objects from the Properties window's list is faster than selecting from the toolbar's Object list.


  2. graphics/font.gif Open the Font list on the Formatting toolbar and select the typeface family you want. Tahoma is Access 2003's default font. (MS Sans Serif was the default typeface in Access 97 and earlier.)

  3. graphics/font_size.gif Open the Font Size list and select 14 points.

  4. graphics/bold.gif Click the Bold attribute button on the toolbar.

  5. graphics/size_to_fit.gif The size of the label you created probably isn't large enough to display the larger font. To adjust the size of the label to accommodate the content of the label, click the Size to Fit button if you added it to the Toolbox or choose Format, Size, To Fit. Access resizes the label's text box to display the entire label; if necessary, Access also increases the size of the Form Header section.

Tip

graphics/control_alignment.gif

graphics/size_to_fit.gif The two Format commands Size, To Grid and Size, To Fit work slightly differently, depending on whether one or more controls are selected. If one or more controls are selected when you execute one of the sizing commands, the command is applied to the selected control(s). If no controls are selected, the chosen sizing command applies as the default to all objects you subsequently create, move, or resize.


When you change the properties of a control, the new values are reflected in the Properties window for the control, as shown in Figure 15.8. If you move or resize the label, you see the label's Left, Top, Width, and Height property values change in the Properties window. You usually use the Properties window to change the property values of a control only if a toolbar button or menu choice isn't available.

Figure 15.8. The Properties window reflects changes you make to the property values of a control with toolbar controls.

graphics/15fig08.gif

Note

You can select different fonts and the Bold, Italic, and Underline attributes (or a combination) for any label or caption for a control. Good design practices dictate use of a single font family, such as Tahoma, for all controls on a form. If the PC running your Access application doesn't have the font family you specified, Windows selects the closest available match usually Arial for sans serif fonts. Changes you make to the formatting of data in controls doesn't affect the data's display in Datasheet view.


Tip

Change the default format of a control, such as a label, by doing this: Select the control in the toolbox and click the Properties button or press F4 to open the Default ControlName window. Click the Format tab and change the property values such as Font Name, Font Size, and Font Weight to apply a standard format to the new labels or other controls you add.


Creating Bound and Calculated Text Boxes

Following are the most common attributes of Access text boxes:

  • Single-line text boxes usually are bound to controls on the form or to fields in a table or query.

  • Multiline text boxes usually are bound to Memo field types and include a vertical scroll bar to allow access to text that doesn't fit within the box's dimensions.

  • Calculated text boxes obtain values from expressions that begin with = (equal sign) and are usually a single line. Most calculated text boxes get their values from expressions that manipulate table field or query column values; but the =Now expression to supply the current date and time also is common. Calculated text boxes are unbound and read-only. You can't edit the value displayed by a calculated text box.

  • Unbound text boxes can be used to supply values such as limiting dates to Access VBA procedures. As a rule, an unbound text box that doesn't contain a calculation expression can be edited.(An unbound text box control can be set to inhibit editing, but doing so negates the control's purpose in most cases.)

The following sections show you how to create the first three types of text boxes.

Adding Bound Text Boxes

The most common text box used in Access forms is the single-line bound text box that makes up the majority of the controls for the frmHRActions form of Chapter 14. To add a bound text box do the following:

  1. graphics/field_list.gif If necessary, click the Field List button on the toolbar to redisplay the field list.

  2. Click and drag the EmployeeID field in the field list window to the upper-left corner of the form's Detail section. When you move the mouse pointer to the active area of the form, the pointer becomes a field symbol, but no crosshair appears. (Notice that Access creates two controls with this action: the text box and its label control.) The position of the field symbol indicates the upper-left corner of the text box, not the label, so drop the symbol in the approximate position of the text box anchor handle, as shown in Figure 15.9.

  3. Drag the text box by the anchor handle closer to the ID label, and decrease the box's width.

  4. graphics/bold.gif Small type sizes outside a field text box are more readable when you turn the Bold attribute on. Select the ID: label and click the Bold button.

    Figure 15.9. Add a bound text box control by dragging the field name to the position where you want the text box to appear.

    graphics/15fig09.gif

    Note

    When Access creates a label for a text box that's associated with a form control, the bound object's name is the value for the text label. If the form control is bound to a table object, such as a field, that has a Caption property (and the Caption property isn't empty), Access combines the value of the Caption property with a colon suffix (the colon is a default you can inhibit) as the default value for the text label of the bound form control. When you created the HRActions table in Chapter 5, you set the Caption property for each field name. The EmployeeID field has a Caption property set to ID, so the label for the text box bound to the EmployeeID field is also ID plus a colon.

  5. Drag the HRComments field from the list box to the form about 0.75 inch below the ID label, delete the label, and resize the text box to about 1 x 2 inches, as shown later in Figure 15.10. When you add a text box bound to a memo field, Access automatically sets the Scrollbars property to Vertical, and they appear when the memo is longer than the text box space or you place the cursor in the text box.

  6. Press Ctrl+S to save your work.

When you drag fields from the Field list in this manner, you automatically create a bound control. By default, however, all controls you add with the Toolbox are unbound controls. You can bind a control to a field by creating an unbound control with a tool and and selecting a field in the Control Source property drop-down list (reach the Control Source list by clicking the Data tab in the Properties window for the control).

Adding a Calculated Text Box and Formatting Date/Time Values

You can display the result of all valid Access expressions in a calculated text box. An expression must begin with = and can use VBA functions to return values. To create a calculated text box that displays the current date and time, do the following:

  1. graphics/text_box.gif Close the Properties window, click the Text Box tool in the Toolbox, and draw an unbound text box at the right of the Form Header section of the form.

  2. graphics/bold.gif Edit the label of the new text box to read Date/Time:, and relocate the label so that it is adjacent to the text box. Apply the Bold attribute to the label.

  3. Type =Now in the text box to display the current date and time from your computer's system clock; Access adds a trailing parentheses pair for you. Adjust the width of the label and the text box to accommodate the approximate length of the text.

  4. graphics/subform.gif Change to Form view and inspect the default date format, MM/DD/YYYY HH:MM:SS AM/PM for North America.

  5. graphics/properties_window.gif To delete the seconds value, open the Properties window for the text box, and click the Format tab. Select the Format property and type mm/dd/yyyy hh:nn ampm in the text box.

Your reformatted Date/Time text box appears as shown in Figure 15.10. Access lets you alter properties of text boxes and other controls in Form and Form Design views. When you change the focus to another control, the format string, mm/dd/yyyy hh:nn ampm for this example, properly reformats the text box.

Figure 15.10. The format string you specify for a text box doesn't apply when the Properties window for the text box is open in Form or Form Design view until you change the focus.

graphics/15fig10.gif

graphics/new.gif

When you return to Design view, the Human Resources Action Entry label you added to the form header has an error correction flag. An error correction flag is a small green triangle in the top left corner of the control. The "Accepting or Declining Control Error Correction" section, which follows shortly, describes dealing with error correction on forms.


Using the Clipboard with Controls

You can use the Windows Clipboard to make copies of controls and their properties. As an example, create a copy of the Date/Time control using the Clipboard by performing the following steps:

  1. graphics/design_view.gif Return to Form Design mode and select the unbound Date/Time control and its label by clicking the field text box. Both the label and the text box are selected, as indicated by the selection handles on both controls.

  2. graphics/copy.gif Copy the selected control to the Clipboard by pressing Ctrl+C.

  3. graphics/paste.gif Click the Detail bar to select the Detail section, and paste the copy of the control below the original version by pressing Ctrl+V.(Access pastes the control into the top-left corner of the section, so you'll need to reposition it.)

  4. Click the Format property in the Properties window for the copied control, and select Long Date from the drop-down list.

  5. graphics/subform.gif To check the appearance of the controls you've created, click the View button on the toolbar.

  6. graphics/design_view.gif Return to Design view and delete the added Date/Time text box and label. To do so, enclose both with a selection boundary created by dragging the mouse pointer across the text boxes from the upper-left to the lower-right corner. Press Delete. (You need the Date/Time text box only in the Form Header section for this form.)

Accepting or Declining Control Error Correction

graphics/new.gif

graphics/form_error_autocorrection.gif After you add a text box control to the form header section, Access 2003's new control error correction feature becomes evident in Form Design view. The Human Resources Action Entry label sports a green flag in its upper left corner. When you select a control with an error flag, the error checking smart tag icon a diamond-shaped sign with an exclamation point appears to the right of the control.

When you pass the mouse pointer over the icon, an error message screen tip describing the problem appears in this case: "This is a new label and is not associated with a control." Clicking the icon's arrow opens the following list (see Figure 15.11):

  • Error description New Unassociated Label for this example.

    Figure 15.11. When you add an unbound label control that's not associated with a text box, Access 2003 flags the control for error correction and offers these selections to ignore or correct the purported error.

    graphics/15fig11.gif

  • Corrective action(s) Associate Label with Control for this example, which opens a list of text box(es) in the section to which the label can be associated.

  • Help on This Item opens a Microsoft Access Help window with the topic relating to the error.

  • Ignore Error removes the flag from the selected control.

  • Error Checking Options lets you specify the errors to be flagged or turn off error checking (see Figure 15.12). Clearing the New Unassociated Labels check box and clicking Apply removes the flag from the selected control. Clicking OK with a check box cleared prevents further error checking for the selection.

Figure 15.12. The error checking Options dialog lets you specify the types of errors to be flagged by this new feature.

graphics/15fig12.gif

Adding new unassociated labels is a common task, so removing this error check is a good form and report design practice. Changes you make in the error checking Options dialog apply to all databases.

Changing the Default View and Obtaining Help for Properties

A form that fills Access's Design window might not necessarily fill the window in Run mode. Run mode might allow the beginning of a second copy of the form to appear. A second copy appears if the Default View property has a value of Continuous Forms. Forms have the following five Default View property values from which you can choose:

  • Single Form displays one record at a time in one form.

  • Continuous Forms displays multiple records, each record having a copy of the form's Detail section. You can use the vertical scroll bar or the record selection buttons to select which record to display. Continuous Forms view is the default value for subforms created by the Form Wizard.

  • Datasheet displays the form fields arranged in rows and columns.

  • graphics/access_2002_new_icon.gif PivotTable displays an empty PivotTable design form, unless you've previously designed the PivotTable.

  • graphics/access_2002_new_icon.gif PivotChart displays an empty PivotChart design form, unless you've previously designed the PivotChart.

Note

PivotTable and PivotChart views of the data source for a form seldom are useful. These views require aggregate values, which are uncommon except in decision-support forms. Rather than use a PivotTable or PivotChart view of the data, add these controls to a form. Chapter 18 describes how to add PivotTable and PivotChart controls to forms.


To change the form's Default View property, do the following:

  1. graphics/properties_window.gif Click the View button on the toolbar to return to Form Design view, if necessary.

  2. graphics/form_select.gif Press Ctrl+R or choose Edit, Select Form.

  3. graphics/properties_window.gif Click the Properties button if the Properties window isn't visible. Click the Format tab.

  4. Click the Default View property and open the list.

  5. Select the value you want for this property for the current form. For this exercise, select Single Form (the default) from the list.

  6. While Default view is selected, press F1 to open the Help window for the Default View property. This Help window also explains how the Default View and Views Allowed properties relate to each another.

Note

The vertical scroll bar disappears from the form in Run mode if a single form fits within its multiple document interface (MDI) child window.


graphics/subform.gif You can verify your changes, if any, to the Default View property by clicking the View button to review the form's appearance.



Special Edition Using Microsoft Office Access 2003
Special Edition Using Microsoft Office Access 2003
ISBN: 0789729520
EAN: 2147483647
Year: 2005
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net