Saturday, August 12, 2017

Introduction to controls

Introduction to controls

This article describes the different kinds of controls available in Access, and shows how to add controls to forms and reports. Controls are the parts of a form or report that you use to enter, edit, or display data. For example, a text box is a popular control for displaying data on reports, or for entering and displaying data on forms. Other popular controls include command buttons, check boxes, and combo boxes (drop-down lists).

In this article

Understand controls

Understand layouts

Understand controls

Controls let you view and work with data in your database application. The most frequently used control is the text box, but other controls include command buttons, labels, check boxes, and subform/subreport controls.

Controls can be bound, unbound, or calculated:

  • Bound control    A control whose source of data is a field in a table or query is called a bound control. You use bound controls to display values that come from fields in your database. The values can be text, dates, numbers, Yes/No values, pictures, or graphs. For example, a text box that displays an employee's last name might get this information from the Last Name field in the Employees table.

  • Unbound control    A control that doesn't have a source of data (such as a field or expression) is called an unbound control. You use unbound controls to display information, pictures, lines or rectangles. For example, a label that displays the title of a form is an unbound control.

  • Calculated control    A control whose source of data is an expression, rather than a field, is called a calculated control. You specify the value that you want to use as the source of data in the control by defining an expression. An expression can be a combination of operators (such as = and + ), control names, field names, functions that return a single value, and constant values. For example, the following expression calculates the price of an item with a 25 percent discount by multiplying the value in the Unit Price field by a constant value (0.75).

=[Unit Price] * 0.75

An expression can use data from a field in the form or report's underlying table or query, or data from another control on the form or report.

Note: Calculations can also be performed in tables by adding a Calculated field, or in queries by entering an expression in the Field row of the query grid. You can then bind forms and reports to those tables or queries, and the calculations appear on the forms or reports without needing to create a calculated control.

When you create a form or report, it is probably most efficient to add and arrange all the bound controls first, especially if they make up most of the controls on the object. You can then add the unbound and calculated controls that complete the design by using the tools in the Controls group on the Design tab in Layout view or Design view.

You bind a control to a field by identifying the field from which the control gets its data. You can create a control that is bound to the selected field by dragging the field from the Field List pane to the form or report. The Field List pane displays the fields of the form's underlying table or query. To display the Field List pane, open the object in Layout view or Design view, and then on the Design tab, in the Tools group, click Add Existing Fields. When you double-click a field in the Field List pane, Access adds the appropriate type of control for that field to the object.

Alternatively, you can bind a field to a control by typing the field name in the control itself (if the object is open in Design view), or in the Control Source property box in the control's property sheet. The property sheet defines the characteristics of the control, such as its name, the source of its data, and its format. To display or hide the property sheet, press F4.

Using the Field List pane is the best way to create a bound control for two reasons:

  • Access automatically fills in the control's attached label with the name of the field (or the caption defined for that field in the underlying table or query), so you don't have to type the control's label yourself.

  • Access automatically sets many of the control's properties to the appropriate values according to the properties of the field in the underlying table or query (such as the Format, Decimal Places, and Input Mask properties).

If you already created an unbound control and want to bind it to a field, set the value in the control's Control Source property box to the name of the field. For details about the Control Source property, press F1 while the cursor is in the property's drop-down list.

Top of Page

Understand layouts

Layouts are guides that align your controls horizontally and vertically to give your form a uniform appearance. You can think of a layout as a table, where each cell of the table is either empty or contains a single control. The following procedures show how to add, remove, or rearrange controls in layouts.

Note: Layouts are optional if you are building a desktop database that will only be opened by using Access. However, if you plan to publish the database to a SharePoint server and use it in a browser, you must use layouts on all forms and reports that you want to use in the browser.

For more information about Web databases, see the article Build a database to share on the Web.

Layouts can be configured in many different ways, but are usually configured in a tabular or a stacked format. Within these two main formats, you can split or merge cells to customize the layout to better suit your controls. For more information, see the section Split or merge cells in a layout.

  • In tabular control layouts, controls are arranged in rows and columns like a spreadsheet, with labels across the top, as shown in the following illustration:

    A tabular layout on a report.

    Tabular control layouts always span two sections of a form or report. Whichever section the controls are in, the labels are in the section above.

  • In stacked layouts, controls are arranged vertically like you might see on a paper form, with a label to the left of each control, as shown in the following illustration:

    A stacked layout on a form.

    Stacked layouts are always contained within a single section on the form or report.

Access automatically creates tabular layouts in either of the following circumstances:

  • You create a new report by clicking Report in the Reports group on the Create tab.

  • You create a new report by clicking Blank Report in the Reports group on the Create tab, and then dragging a field from the Field List pane to the report.

Access automatically creates stacked layouts in either of the following circumstances:

  • You create a new form by clicking Form in the Forms group on the Create tab.

  • You create a new form by clicking Blank Form in the Forms group on the Create tab, and then dragging a field from the Field List pane to the form.

In this section

Create a new layout

Switch a layout from tabular to stacked, or from stacked to tabular

Split one layout into two layouts

Add rows or columns to a layout

Remove rows or columns from a layout

Split or merge cells in a layout

Rearrange controls in a layout

Add controls to a layout

Remove controls from a layout

Create a new layout

On an existing form or report object, you can create a new control layout by doing the following:

  1. Select a control that you want to add to the layout.

  2. If you want to add other controls to the same layout, hold down the SHIFT key and also select those controls.

  3. On the Arrange tab, in the Table group, click Tabular or Stacked.

Access creates the layout and adds the selected controls to it.

Top of section

Switch a layout from tabular to stacked, or from stacked to tabular

To switch an entire layout from one type of layout to the other:

  • Select a cell in the layout that you want to change.

  • On the Arrange tab, in the Rows & Columns group, click Select Layout.

  • On the Arrange tab, in the Table group, click the layout type you want (Tabular or Stacked).

Access rearranges the controls into the layout type that you want.

Top of section

Split one layout into two layouts

You can split a control layout into two layouts by doing the following:

  1. Hold down the SHIFT key and click the cells that you want to move to the new layout.

  2. On the Arrange tab, in the Table group, click the layout type you want for the new layout (Tabular or Stacked).

Access creates a new control layout and adds the selected controls to it.

Note: The original layout might contain empty rows or columns as a result of splitting the layout. To delete a row or column, right-click a cell in the row or column, and then click Delete Row or Delete Column.

Top of section

Add rows or columns to a layout

  1. Select a cell adjacent to where you want to add the new row or column.

  2. On the Arrange tab, in the Rows & Columns group, click Insert Above or Insert Below to insert a new row above or below the current row. Click Insert Left or Insert Right to insert a new column to the left or right of the current column.

Access adds the new row or column to the layout.

Top of section

Remove rows or columns from a layout

  1. Select a cell in the row or column that you want to remove.

  2. On the Arrange tab, in the Rows & Columns group, click Select Column or Select Row to select the column or row.

  3. Press the DELETE key.

Access deletes the row or column from the layout.

Top of section

Split or merge cells in a layout

Like a table in a word processing document, a layout is made up of cells that are arranged in rows and columns. Each cell can contain one control, such as a text box, a label, or a command button. You can split one cell horizontally or vertically to create two cells, or you can merge multiple cells together to create one large cell that spans multiple rows or columns in the layout. Even if a cell spans multiple rows or columns, it can still contain only one control.

Split cells

When you split a cell vertically, you create a new row in the structure of the layout. If there are other cells on the same row as the one that you split, they remain the same size (they span the two underlying rows that result from the split operation). When you split a cell horizontally, you create a new column in the structure of the layout. If there are other cells in the same column as the one that you split, they remain the same size (they span the two underlying columns that result from the split operation).

Merge cells

Note: You can merge empty cells together, or you can merge one cell that contains a control with other empty cells. However, you cannot merge cells that contain multiple controls, because each cell can contain only one control.

  1. Select the first cell that you want to merge.

  2. Hold down the SHIFT key and select the other cells that you want to merge.

  3. On the Arrange tab, in the Merge / Split group, click Merge.

Note: As you split and merge multiple cells in a layout, the underlying row and column structure can become somewhat complex. As a result, it can be difficult to resize just the cell you want. We recommend that you first adjust row heights and column sizes as much as possible, and then split and merge cells to create the layout you want.

Top of section

Rearrange controls in a layout

You can move a control within a layout, or between layouts, by dragging it to the location that you want. As you drag the control, a horizontal or vertical bar indicates where it will be placed when you release the mouse button. If you drag the control over an empty cell, Access highlights the whole cell to indicate where the control will be placed.

You can also move a control within a layout by selecting it, holding down the ALT key, and then using the arrow keys to move the control.

If you use the ALT+arrow key combination to move a control to the left or right, and you move it outside the boundaries of the layout, Access adds new columns to the layout as needed to accommodate the move. These new columns will remain even if you move the control back to its original location, and can have the unintended consequence of widening the report so that it extends onto another page. On reports, this can cause blank pages to print, due to the extra width of the report object. To delete an empty column in a layout, in Layout view, select a cell in that column, and then press the DELETE key. When the layout no longer crosses the page boundary, Access automatically resizes the report back down to the original page width.

Top of section

Add controls to a layout

To add a new field from the Field List pane to an existing control layout    

  • Drag the field from the Field List pane to the layout. A horizontal or vertical bar indicates where the field will be placed when you release the mouse button. If you drag the field over an empty cell, Access highlights the whole cell to indicate where the field will be placed.

To add existing controls to an existing control layout    

  1. In Layout view, select the first control you want to add to the control layout.

  2. If you want to add other controls to the same layout, hold down the SHIFT key and also select those controls. You can select controls in other control layouts.

  3. Drag the selected fields to the layout. A horizontal or vertical bar indicates where the fields will be placed when you release the mouse button.

Top of section

Remove controls from a layout

Removing controls from a layout enables you to place the controls anywhere on the form or report without affecting the positioning of any other controls.

If you plan to use a form or report as part of a Web application, all of its controls must be contained in layouts.

  1. On the Home tab, in the Views group, click Views, and then click Design view.

  2. Select the cells that you want to remove from the layout, including any labels. To select multiple cells, hold down the SHIFT key and then click the cells you want to remove. To select all of the cells in the layout, on the Arrange tab, in the Rows & Columns group, click Select Layout.

  3. Right-click one of the selected cells, point to Layout, and then click Remove Layout.

    Access removes the selected cells from the layout.

  4. If you left some controls in the layout, they will now overlap with the controls you removed. While the controls are still selected, drag the selected controls away from the layout, being careful not to drop the controls back into the layout.

Top of Page

No comments:

Post a Comment