Adding Controls

In this section, you’ll learn how to change a form’s design using Design View. You’ll add, move, and resize different controls, as well as customize other aspects of a form.

Click on Design View from the View drop-down in the Home tab’s Views group to switch a form to Design View. The Design tab on the Form Design Tools contextual tab—shown in Figure 35-6—lets you add and customize controls on your form.

Figure 35-6. The Design tab lets you add and customize controls in a form’s Design View.


New Feature

The Controls group on the Ribbon’s Design tab replaces the Toolbox from previous versions of Access.


Resizing the form area

The white area of the form is where you work. This is the size of the form when it is displayed. Resize the white area of the form by placing the cursor on any of the area borders and dragging the border of the area to make it larger or smaller. Figure 35-7 shows a blank form in Design View being resized.

Figure 35-7. Design View of a blank form. Resize the form area by dragging the bottom-right corner.


Saving the form

You can save the form at any time by clicking on Save in the Quick Access toolbar. When you’re asked for a name for the form, give it a meaningful name (for example, frmProducts, frmCustomers, frmProductList). If you’ve already given the form a name, you won’t be prompted for a name when you click Save.

When you close a form, Access asks you to save it. If you don’t save a form, all changes since you opened the form (or the last time you pressed Save) are lost. You should frequently save the form while you work if you’re satisfied with the results.

Tip

If you are going to make extensive changes to a form, you might want to make a copy of the form. If you want to work on the form frmProducts , you can copy and then paste the form in the database window, giving it a name like frmProductsOriginal. Later, when you have completed your changes and tested them, you can delete the original copy.


Understanding controls

Controls and properties form the basis of forms and reports. It is critical to understand the fundamental concepts of controls and properties before you begin to apply them to custom forms and reports.

Note

Although this chapter is about forms, you will learn that forms and reports share many common characteristics including controls and what you can do with them. As you learn about controls in this chapter, you will be able to apply nearly everything you learn when you create reports.


The term control has many definitions in Access. Generally, a control is any object on a form or report, such as a label or text box. These are the same controls that you use in any Windows application, such as Access, Excel, or Web-based HTML forms, or those that are used in any language, such as .Net, Visual Basic, C++, or even C#. Although each language or product has different file formats and different properties, a text box in Access is the same as a text box in any other Windows product.

You enter data into controls and display data using controls. A control can be bound to a field in a table (when the value is entered in the control it is also saved in some underlying table field), or it can be unbound and displayed in the form but not saved when the form is closed. A control can also be an object, such as a line or rectangle. Calculated fields are also controls, as are pictures, graphs, option buttons, checkboxes, and objects. Some controls that aren’t part of Access are developed separately—these are ActiveX controls. ActiveX controls extend the base feature set of Access 2007 and are available from a variety of vendors. Many ActiveX controls are shipped with Access 2007.

Whether you’re working with forms or reports, essentially the same process is followed to create and use controls. In this chapter, we explain controls from the perspective of a form.

The different control types

Forms and reports contain many different control types. You can add these controls to forms using the Controls group shown in Figure 35-6. Hovering the mouse over the control displays a ScreenTip telling you what the control is. Table 35-1 briefly describes each control.

Table 35-1. Controls in Access Forms and Reports
ControlWhat It Does
Text BoxDisplays and allows users to edit data.
LabelDisplays static text that typically doesn’t change.
ButtonAlso called a command button. Calls macros or runs VBA code when clicked.
Combo BoxA drop-down list of values.
List BoxA list of values that is always displayed on the form or report.
Subform/SubreportDisplays another form or report within the main form or report.
LineA graphical line of variable thickness and color, which is used for separation.
RectangleA rectangle can be any color or size or can be filled in or blank; the rectangle is used for emphasis.
ImageDisplays a bitmap picture with very little overhead.
Option GroupHolds multiple option buttons, checkboxes, or toggle buttons.
Check BoxA two-state control, shown as a square that contains a check mark if it’s on and an empty square if it’s off.
Option ButtonAlso called a radio button, this button is displayed as a circle with a dot when the option is on.
Toggle ButtonThis is a two-state button—up or down—which usually uses pictures or icons instead of text to display different states.
Tab ControlDisplays multiple pages in a file folder type interface.
PageAdds a “page” on the form or report. Additional controls are added to the page, and multiple pages may exist on the same form.
ChartThis chart displays data in a graphical format.
Unbound Object FrameThis frame holds an OLE object or embedded picture that is not tied to a table field and can include graphs, pictures, sound files, and video.
Bound Object FrameThis frame holds an OLE object or embedded picture that is tied to a table field.
Page BreakThis is usually used for reports and indicates a physical page break.
HyperlinkThis control creates a link to a Web page, a picture, an e-mail address, or a program.
AttachmentThis control manages attachments for the Attachment data type.

The Use Control Wizards command, located on the right side of the Controls group, doesn’t add a control to a form; instead, it determines whether a wizard is automatically activated when you add certain controls. The Option Group, Combo Box, List Box, Subform/Subreport, Bound and Unbound Object Frame, and Command Button controls all have wizards that Access starts when you add a new control. You can also use the ActiveX Controls command (found in the bottom-right corner of the Controls group) to display a list of ActiveX controls, which you can add to Access 2007.

Understanding bound, unbound, and calculated controls

These are the three basic categories of controls:

  • Bound controls: These are controls that are bound to a table field. When you enter a value into a bound control, Access automatically updates the table field in the current record. Most of the controls that let you enter information can be bound; these include OLE (Object Linking and Embedding) fields. Controls can be bound to most data types, including text, dates, numbers, Yes/No, pictures, and memo fields.

  • Unbound controls: Unbound controls retain the entered value, but they don’t update any table fields. You can use these controls for text label display, for controls such as lines and rectangles, or for holding unbound OLE objects (such as bitmap pictures or your logo) that aren’t stored in a table but on the form itself. Unbound controls are also known as variables or memory variables.

  • Calculated controls: Calculated controls are based on expressions, such as functions or calculations. Calculated controls are also unbound because they don’t update table fields. An example of a calculated control is =[SalePrice] - [Cost]. This control calculates the total of two table fields for display on a form but is not bound to any table field.

The two ways to add a control

You add a control to a form in either of two ways:

  • Click a button in the Design tab’s Controls group on the Ribbon and draw a new unbound control on the form.

  • Drag a field from the Field List to add a bound control to the form.

A bound control is one that is linked to a table field, while an unbound control is one that is not bound to a table field. A control bound to a table places the data directly into the table by using the form.

Using the Controls group to add a control

By using the buttons in the Controls group to add a control, you decide which type of control to use for each field. The control you add is unbound (or not attached to the data in a table field) and has a default name such as Text21 or Combo11. After you create the control, you decide what table field to bind the control to, enter text for the label, and set any properties. You’ll learn more about setting properties later in this chapter.

You can add one control at a time using the Controls group. To create three different unbound controls, perform these steps:

1.
Click the Create tab on the Ribbon, and then click on the Form Design command in the Form group to create a new form in Design View.

2.
Click the Design tab on the Ribbon, and then click the Text Box button (ab|) in the Controls group.

The selected button appears with a colored background.

3.
Move the mouse pointer to the Form Design window.

The cursor changes to the Text Box icon.

4.
Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.

5.
Click the Option Button in the Controls group.

6.
Move the mouse pointer in the Form Design window.

The cursor changes to the Option Button icon.

7.
Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.

8.
Click the Check Box button in the Controls group.

9.
Move the mouse pointer in the Form Design window.

The cursor changes to the Check Box icon.

10.
Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.

When you’re done, your screen should resemble the one shown in Figure 35-8.

Figure 35-8. Unbound controls added from the Controls group.


Tip

Clicking the Form Design window with a control selected creates a default-sized control. If you want to add multiple controls of the same type, double-click on the icon in the Controls group, and then draw as many controls as you want on the form.


Using the Field List to add a control

The Field List displays a list of fields from the table or query the form is based on. You add bound controls to the form by dragging fields from the Field List onto the form. Select and drag them one at a time, or select multiple fields by using the Ctrl key or Shift key.

  • Select multiple contiguous fields by holding down the Shift key and clicking the first and last fields that you want.

  • Select multiple noncontiguous fields by holding down the Ctrl key and clicking each field that you want.

Click the Add Existing Fields command in the Design tab’s Tools group to display the Field List. By default, the Field List appears docked on the right of the Access window, shown in Figure 35-9. This window is movable and resizable and displays a vertical scroll bar if it contains more fields than can fit in the window.

Figure 35-9. Click Add Existing Fields in the Tools group to show the Field List, docked on the right of the Access window.


Generally, dragging a field from the Field List window adds a bound text box to the Form Design window. If you drag a Yes/No field from the Field List window, you add a checkbox. If you drag a field that has a Lookup property, you add a List Box control or Combo Box control. If you drag an OLE field from the Field List window, you create a bound object frame. Optionally, you can select the type of control by selecting a control from the Controls group and dragging the field to the Form Design window.

Caution

When you drag fields from the Field List window, the first control is placed where you release the mouse button. Make sure that you have enough space to the left of the control for the labels. If you don’t have sufficient space, the labels slide under the controls.


You gain several distinct advantages by dragging a field from the Field List window:

  • The control is bound automatically to the field that you dragged it from.

  • Field properties inherit table-level formats, status-bar text, and data-validation rules and messages.

  • The label control and label text are created with the field name as the caption.

  • The label control is attached to the field control, so they move together.

Select and drag the Description, Category, RetailPrice, and AuctionEndDate fields from the Field List window to the form, as shown in Figure 35-10. Double-clicking a field also adds it to the form.

Figure 35-10. Drag fields from the Field List to add bound controls to the form.


You can see four new controls in the form’s Design View—each one consists of a Label control and a Text Box control (Access attaches the Label control to the text box automatically). You can work with these controls as a group or independently, and you can select, move, resize, or delete them. Notice that each control has a label with a caption matching the field name, and the Text Box control displays the bound field name used in the text box. If you want to resize just the control and not the label, you must work with the two controls separately.

Close the Field List by clicking the Add Existing Fields command in the Design tab’s Tools group or the Close button on the Field List.

Tip

In Access, you can change the type of control after you create it; then you can set all the properties for the control. For example, suppose that you add a field as a Text Box control and you want to change it to a List Box. You can right-click the control and select Change To from the pop-up menu to change the control type. However, you can change only from some types of controls to others. You can change anything to a Text Box control; option buttons, toggle buttons, and checkboxes are interchangeable, as are List Boxes and Combo Boxes.


In Figure 35-10, notice the difference between the controls that were dragged from the Field List window and the controls that were created from the Controls group. The Field List window controls are bound to a field in tblProducts and are appropriately labeled and named. The controls created from the Controls group are unbound and have default names. The default names are automatically assigned a number according to the type of control.

Later, you learn how to change the control names, captions, and other properties. Using properties speeds the process of naming controls and binding them to specific fields. If you want to see the differences between bound and unbound controls, switch to Form View using the View command in the Home tab’s Views group. The Description, Category, RetailPrice, and AuctionEndDate controls display data since they’re bound to tblProducts. The other three controls don’t display data because they aren’t bound to any data source.

Note

If a form’s Record Source property isn’t set, you will not see a Field List window.


Tip

If you first select a control type in the Controls group and then drag a field from the Field List, a control is created (using the selected control type) that is automatically bound to the data field in the Field List.


Which method to use

The deciding factor of whether to use the field list or the Controls group is this: Does the field exist in the table/query or do you want to create an unbound or calculated expression? By using the Field List window and the Controls group together, you can create bound controls of nearly any type. You will find, however, that some data types don’t allow all the control types found in the Controls group. For example, if you select the Chart control type from the Controls group and drag a single field to the form, a text box control is added instead of a chart control.

The following properties always inherit their settings from the field’s table definition:

  • Format

  • Decimal Places

  • Status Bar Text (from the field Description)

  • Input Mask

  • Default Value

  • Validation Rule

  • Validation Text

Note

Changes made to a control’s properties don’t affect the field properties in the source table.


Each type of control has a different set of properties, as do objects such as forms, reports, and sections within forms or reports. In the next few chapters, you learn about many of these properties as you use each of the control types to create more complex forms and reports.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset