Chapter 13. Designing Advanced Forms

As you learned in the previous chapter, forms can streamline day-to-day tasks and even give your database a sharp, distinctive look. To be a master database builder, you need to be able to craft top-notch forms.

In this chapter, you’ll take form building to the next level with a whole new arsenal of techniques. First, you’ll learn how to create a form in the no-holds-barred Design view, where you can tweak and polish every square inch of your form. Then, you’ll take a tour of Access’s different controls, and jazz up your form with links, tabbed panels, and buttons. You’ll also learn how to work with linked tables by creating special types of forms called subforms that work in harmony with other forms.

Customizing Forms in Design View

In the previous chapter, you learned how to quickly create different forms using the ribbon’s buttons and the Form wizard. But serious form gurus take a different approach—they build a form by hand. You go about this task in two ways:

  • Create a form in Layout view. Choose Create→Forms→Blank Form. Then, drag the fields you want from the Field List pane onto your form (Figure 13-1). You learned everything you need to pull this off in Chapter 12. You can quickly create a standard form with a stacked or tabular layout, but it doesn’t give you any extra frills.

  • Create a form in Design view. Choose Create→Forms→Form Design. Now you’ll start with a blank form in the design window. You can drag fields onto your form from the Field List pane (just as you do in Layout view), and you can add a wide variety of more specialized controls from the ribbon.

When you drop your first field onto a new form in Layout view, Access adds that field to a stacked layout and shows a smart tag icon (circled). If you want a tabular layout instead, click this icon, and then choose “Show in Tabular Layout.”
Figure 13-1. When you drop your first field onto a new form in Layout view, Access adds that field to a stacked layout and shows a smart tag icon (circled). If you want a tabular layout instead, click this icon, and then choose “Show in Tabular Layout.”

Note

If you don’t see the Field List pane, choose Design→Tools→Add Existing Fields (while in Design view or Layout view).

Of course, while you’re working with a form, you can easily jump back and forth between the two views. (Just right-click the tab title and choose the view you want, or click the view buttons at the window’s bottom-right corner.) You can add fields to your form using either view. However, when you add fields in Layout view, Access automatically positions them in a layout. When you add fields in Design view, they start out layout-free. Access assumes that people who use Design view want more control over how their fields are placed.

There’s another, more important difference between Layout view and Design view. In Design view, you have access to a greater selection of controls. These controls make the difference between the cookie-cutter forms Access creates automatically, and forms that exhibit your own style.

Form Sections: The Different Parts of Your Form

In Chapter 11, you learned that a report is divided into separate sections (like a report header, a details section, a report footer, and so on), each of which appears in a specific place. The same is true for forms. However, newly created forms start life with only one section: the Details section, which defines the content for each record.

If you want to add a title or logo at the top of your form, or some sort of summary information or message at the bottom, you’ll want to include a header and footer section. To add these elements to your form, right-click anywhere on the form’s surface, and then choose Page Header/Footer.

When working with form sections, remember to keep them small (as shown in Figure 13-2). Each form section should be just large enough to fit the content you’re displaying. An oversized form with a lot of blank space looks unprofessional. Also, you’ll end up with unnecessary scroll bars on your form’s sides.

Tip

You can’t make a form smaller than the controls it contains. If Access doesn’t let you resize a form, then something, somewhere, is still too big. (If all else fails, check that you don’t have a large box in the form header or form footer sections.)

Even though this form easily fits all its fields in the display area, it still has scroll bars. If you switch to Design view, you’ll see why—the form is wider and longer than it needs to be.
Figure 13-2. Even though this form easily fits all its fields in the display area, it still has scroll bars. If you switch to Design view, you’ll see why—the form is wider and longer than it needs to be.

Note

If your database is set to use overlapping windows instead of tabs (The Navigation Pane), you’ll see a slightly different problem—your form windows are unnecessarily large. In fact, they may not even fit in the main Access window, in which case Access chops off the edges.

Adding Controls to Your Form

You first learned about controls—graphical widgets like labels and text boxes—when you created advanced reports in Chapter 11. Access gives you the same ability to use controls with forms. In fact, you use the same ribbon section to add them. However, many of the controls that didn’t make much sense with reports really shine with forms.

Note

Behind the scenes, everything on a form is actually a control. Each time you add a field, you end up with two linked controls: a label that displays the field name, and a text box that holds the field value.

One of the simplest and most useful controls is the humble label. Using the label, you can add formatted text anywhere on your form. You could choose to use labels to highlight additional instructions, as shown in Figure 13-3.

Use labels to add helpful instructions (or cheeky commentary) to your forms. Line and rectangle controls add a little polish.
Figure 13-3. Use labels to add helpful instructions (or cheeky commentary) to your forms. Line and rectangle controls add a little polish.

To add a control, follow these steps:

  1. Head to the ribbon’s Form Design Tools | Design→Controls section.

    The Controls section has one-stop shopping for all the controls you can use.

    Tip

    It’s equally valid to insert many controls in Layout view, using the ribbon’s Form Layout Tools | Design→Controls section. These tools are particularly handy if you want to add a control into a layout table, so you don’t need to fiddle with its exact position and size. However, the Controls section in Layout view leaves out quite a few controls in an attempt to make Layout view seem simpler to use than Design view. For that reason, this book assumes you mostly add your controls in Design view.

  2. Optionally, switch on the Use Control Wizards button.

    Some types of controls, like buttons and lists, come equipped with helpful wizards. As soon as you drop one on your form, the wizard pops up to help you perfect it. Ordinarily, wizards are switched on. However, control experts who know exactly what they want may find that the wizards just slow them down.

    To see the Use Controls Wizards button, head to the Form Design Tools | Design→Controls section of the ribbon, and click the drop-down arrow to the right of the control gallery. When the Use Control Wizards button is not highlighted, the Control wizards leave you alone.

  3. Click the icon for the control you want.

    On most people’s screens, Access can’t fit the control name on the ribbon’s control button. (Thirty-inch monitor owners, congratulate yourselves and skip to the next paragraph.) Hover over each control icon for a moment, and Access displays the control name in a tooltip.

    Once you click your icon, it remains highlighted. Your mouse cursor changes to a cross with a small picture of the control superimposed. That change is your indication that your control is ready and waiting for you to drop it onto the form.

    Tip

    If this is your first time experimenting with controls, why not try the label—it’s easy to master, and genuinely useful.

  4. To place your control on the form, drag the mouse cursor to draw the control on the form.

    If you don’t get it right the first time, you can always drag a control to a new position, or drag its borders to resize it.

    If you decide that you don’t want to add the control you picked, then just press Esc, or click the arrow at the far left of the control gallery. Either way, your mouse pointer returns to normal, and you can now click the form to select an existing control.

  5. If your control has a Control wizard and you choose to use Control Wizards (see step 2), the wizard appears now.

    Answer all the questions to configure your control, or just press Esc to skip out of the wizard, and do all the configuration on your own.

  6. If you’re adding a label, supply some text for the control.

    After you drop a label onto your form, Access waits for you to type in some text (which is set in the Caption property). If you don’t type anything for your label, Access assumes you don’t really want the label, and just gets rid of it.

  7. If the Property Sheet isn’t already visible (at the right side of the window), click Form Design Tools | Design→Tools→Property Sheet to show it.

    To configure the control’s many settings, or properties, you need to use the Property Sheet.

  8. Change the appropriate settings in the Property Sheet.

    If you’ve added a bound control (see the box above), select the Data tab, and then set the Control Source field to the name of the field you want to display.

    Tip

    If you have a label control that doesn’t fit all the text you’ve entered, you can bump its size up in one step. Just right-click the control, and then choose Size→To Fit. Access resizes the label so it’s just large enough to fit all its content. Don’t try this with other controls like text boxes—it won’t work.

  9. Optionally, give your control a better name by setting the Name property (in the Other tab).

    If you’ve just created a new label, Access bestows a name like “Label46.” If you want to honor your control with something more becoming, just change the text in the Name property. You’ll have an easier time finding your control in the drop-down list in the Property Sheet next time you want to change it.

  10. Format your control.

    Although you can adjust many formatting details via the Property Sheet, the ribbon is much easier to use. Use the Form Design Tools | Format→Font section for basic font and color formatting, and use the Form Design Tools | Format→Control Formatting section to add a stylized border around your control.

Later in this chapter, you’ll consider how to create some popular form designs with controls. But first, it’s worth taking a quick overview of all the controls on the ribbon so you can see exactly what’s available (and what isn’t). Table 13-1 introduces you to every member of the control family.

Table 13-1. Form Controls

CONTROL

DESCRIPTION

Text Box

Displays the value of a field from a record. You can also use a text box to show the result of an expression, as described on Expressions.

Label

Displays fixed text. Perfect for captions, notes, and helpful instructions.

Button

Performs an action (when the user clicks it). For example, you could add a new record or show a different form. Performing Actions with Command Buttons explains how to configure a button’s action.

Toggle Button

Shows a button that can be in two different states: normal and pressed. You switch it from one state to the other by clicking it. The toggle button is a rarely used oddity, but you can substitute it for the check box to display the value of a Yes/No field. In this case, the button is depressed if the field value is Yes.

Combo Box

Displays a list that pops into view when you click the drop-down arrow. This list can be a list of values you supply, or it can be drawn from another table. Access automatically uses a combo box for lookup fields or linked tables.

List Box

Displays a large box with a list of items. This list can be a list of values you supply, or it can be drawn from another table. You can use list boxes and combo boxes interchangeably—the key difference is the fact that list boxes take more space, and combo boxes let you type in your own values that aren’t in the list.

Check Box

Displays the value of a Yes/No field. If it’s Yes, the check box has a checkmark.

Attachment

Shows the first file in an attachment field. If this file is a picture, the picture is displayed directly on the form. Otherwise, all you see is a small icon that indicates the file type. If the attachment field holds more than one file, you can step through each one by using the arrows in the minibar (which appears when you click this field), as described on Showing Pictures from a Table.

Image

Displays a picture that you supply. Perfect for logos and eye candy that set your form apart. Set the Size Mode property to determine whether your picture is chopped down to fit its box (Clip), stretched to fit (Stretch), or resized without changing the shape (Zoom, which is the standard setting). You can even use the Picture Tiling property to repeat a picture over a larger surface. To keep your pictures (and database files) small, use compact .jpg files rather than bloated .bmp files.

Hyperlink

Displays a fixed hyperlink—blue underlined text that, when clicked, transports the clicker to a specific web page. Going Places with Links shows how it works.

Web Browser

Places a web page window right inside your form. You can use this area to show an external website or some database content, as demonstrated on Showing Web Pages with the Web Browser.

Navigation Control

Lets you use web-style navigation tabs, which sit in a strip at the side or top of your form. You’ll use this feature on Navigation Forms.

Line and Rectangle

The line and rectangle controls are just decoration. Skillful designers use them to separate sections and highlight important information.

Tab Control

Displays several tabs of information. You can see the content in one tab at a time—you click to pick which tab you want. This Windows staple lets you pack more information into a smaller space. Organizing with Tab Controls shows an example.

Option Group and Option Button

The option group is a rectangular container that holds one or more option buttons. The form user can select just one of these option buttons at a time.

Subform

Displays a form inside a form. Usually, a subform shows linked records from a related table. You’ll see how this works on The Subform Control.

Chart

Creates a basic chart, using the Chart Wizard that’s included with Office. Alas, charts aren’t very well integrated into Access. If you want to provide a graphical view of your data, you’re better off using a pivot chart (see Chapter 9) or just exporting your raw data to Excel, which is much more capable.

Unbound Object Frame

Shows content, which is known as an object, from another program using a somewhat old-fashioned standard called OLE. You can use this control to do things like embed a spreadsheet, audio file, or Word document within your form. Most folks resist the urge, because the results tend to be awkward and confusing.

Bound Object Frame

Similar to the unbound object frame, but this control retrieves the object you want to display from a field in the current record. This feature seems nifty, but the quirky and outdated OLE standard causes more trouble than it’s worth. If you want this sort of feature, you’re far better off using an attachment field with an attachment control (Showing Pictures from a Table), which is designed to solve these problems.

Page Break

Indicates where a page break should fall. This control has an effect only when you use your form to create a printout. Usually, you should steer clear of this control in forms and use it exclusively in reports, which are tailor-made for printing.

Arranging Controls on Your Form

By now, you’re probably comfortable working with controls in Design view. Here’s a quick refresher if your memory needs a little jumpstart:

  • Create a control. Use the ribbon to pick the control you want, and then draw it in the right place.

  • Move a control. Just drag it. You can also move several controls at once, as explained in Figure 13-4.

  • Resize a control. Drag the edges of the rectangle that surrounds it. If you have a linked label-and-text-box combination (which Access creates when you add a field), be careful to click the right part. Figure 11-9 (page 356) shows where to click to move just the caption, just the field value box, or both.

  • Modify a control. Select it, and then, in the Property Sheet, find the setting you want to change.

  • Delete a control. Select it, and then press Delete to wipe it out forever.

To move multiple controls at once, start by clicking somewhere on the form surface. Drag a selection box around all the controls you want to move, as shown here. Then, once all the controls are highlighted, drag any one of them. All the controls move as a unit. (You could also hold down Shift, and click each control one by one.)
Figure 13-4. To move multiple controls at once, start by clicking somewhere on the form surface. Drag a selection box around all the controls you want to move, as shown here. Then, once all the controls are highlighted, drag any one of them. All the controls move as a unit. (You could also hold down Shift, and click each control one by one.)

If your controls aren’t contained in a layout, it can be difficult to get them neatly arranged. To help, Access supplies some shortcuts that can line up rogue controls and iron out minor variances. The following sections provide a few useful tips for using these features.

Aligning Controls

If you have a group of controls that needs to be neatened up, select them all (by drawing a selection box, as shown in Figure 13-4), right-click the selection, and then choose an option in the Align submenu. Use the ever-popular “left” option to line all the controls up along their left edges. You can also line up controls on the right (see Figure 13-5), top, or bottom edges.

Top: These controls look messy.Bottom: Even though the controls aren’t in a layout, you can line them up properly using the Align options. Here, someone used the Align→Right command to pull them together against the right edge.
Figure 13-5. Top: These controls look messy. Bottom: Even though the controls aren’t in a layout, you can line them up properly using the Align options. Here, someone used the Align→Right command to pull them together against the right edge.

Note

Many of the commands you’ll learn about in the following sections for alignment, sizing, positioning, and anchoring, exist in two places. You can get them from the Form Design Tools | Arrange tab of the ribbon or by right-clicking a selection of one or more controls and looking in the pop-up menu. The menu approach is often faster, but the ribbon approach is sometimes more convenient for setting complex formatting.

Sizing Controls

If you have controls of different sizes, you can tell Access to make them all the same size. Select them all, right-click the selection, and then choose an option from the Size menu. Use To Widest to make all the controls as wide as the widest one of the bunch (see Figure 13-6). Alternatively, you can shrink controls by choosing To Narrowest, or change their heights with To Tallest and To Shortest.

The To Widest command makes all these text boxes (top) the same width (bottom), which creates a cleaner and more visually pleasing form.
Figure 13-6. The To Widest command makes all these text boxes (top) the same width (bottom), which creates a cleaner and more visually pleasing form.

Spacing Controls

If you have controls that are scattered unevenly over the form, you can reposition them so that a consistent amount of space appears between them. To do so, select all the controls, and then head to the ribbon’s Form Design Tools | Arrange→Sizing & Ordering→Size/Space menu. Under the Spacing heading, you’ll find several commands to adjust the spacing between controls:

  • Equal Horizontal spaces out controls so they’re an even distance apart (from side to side). The control on the far left and the control on the far right keep their positions, while the controls in between are spread out evenly.

  • Equal Vertical spaces out controls so they’re an even distance apart (from top to bottom). The control on the top and the control on the bottom remain in position, while the others are spread out between.

  • Increase Horizontal and Increase Vertical add a bit more space between all the controls you’ve selected.

  • Decrease Horizontal and Decrease Vertical remove a bit of space between all the controls you’ve selected.

Controls that Overlap

If you have overlapping controls, you may want to decide which one is placed on top and which one on the bottom. To do so, select one of the controls, right-click it, and choose Position→Bring to Front (to move the control to the top) or Position→Send to Back (to banish it to the background).

It goes without saying that most forms don’t have overlapping controls. The exception is if you’re aiming for a unique graphical effect, or if you’re trying to use a rectangle to frame a bunch of controls (in which case the rectangle needs to sit behind the other controls or have a transparent background).

Anchoring: Automatically Resizing Controls

Ordinarily, your controls have a fixed, unchanging size. This characteristic lets you precisely arrange a large number of controls next to each other. However, fixed-size controls also have a downside. If you resize the Access window to make it very large, the controls can’t use the extra space. Conversely, if you make the Access window very small, you’re sure to cut off part of your form. In other words, fixed-size controls make for easy design, but they’re inflexible.

Most people don’t worry too much about these limitations. They design their forms to fit comfortably on an average-sized screen. However, if you have one or more fields that display a large amount of data—like a Long Text field that’s chock-full of text—you might want to get a bit more ambitious.

Access includes a feature called anchoring, that lets you create controls that can grow to fill extra space when the Access window is resized. Anchoring is a little tricky to get right, but if you have huge text fields, it’s worth the trouble.

Essentially, anchoring lets you attach a control to the sides of the form. As a result, when the form changes size, the control is dragged to a new position or resized. Figure 13-7 shows an ordinary form that uses standard anchoring settings. Nothing happens when this form is resized.

Ordinarily, all controls are anchored to the form’s top-left corner (top). If the form window changes size, nothing happens to the controls, because the top-left corner never moves (bottom).
Figure 13-7. Ordinarily, all controls are anchored to the form’s top-left corner (top). If the form window changes size, nothing happens to the controls, because the top-left corner never moves (bottom).

If you anchor a control to a form’s right side, it’s a different story. As the form is resized wider, the control hugs the right side, moving to a new position. Similarly, if you attach a control to the bottom of the window and make it taller, the control keeps close to the bottom, no matter how small or large the window becomes. The really exciting bit is what happens when you anchor a control to opposite sides. In this case, its position doesn’t change, but its size does. If you anchor a control to a form’s left and right sides, the control widens as the form widens. (This makes sense for a title at the top of your form with center-aligned text.) Figure 13-8 shows how life changes when you anchor your control to different sides.

Note

The amount of space between the control and the anchored side always remains the same.

This form includes controls that are anchored to different sides. The FirstName and Email fields are anchored to the top right, and the Notes field is anchored to the bottom right. As the form grows, the controls change position.
Figure 13-8. This form includes controls that are anchored to different sides. The FirstName and Email fields are anchored to the top right, and the Notes field is anchored to the bottom right. As the form grows, the controls change position.

Theoretically, you can use anchoring to create all kinds of bizarre effects. You could anchor controls to all different sides of the form, so they move and overlap as the form changes size, scrambling the form in complete confusion. More realistically, people use anchoring to achieve two effects, which are demonstrated in the following two sections.

Making Controls as Wide as the Form

Ordinarily, you size a text box, and its size never changes. But with anchoring, you can make controls stretch wider or narrower to match your Access window’s size. And as long as you don’t put any other controls in the way, you don’t have a problem with overlapping controls.

Just follow these steps:

  1. First, make sure your form doesn’t have extra blank space. In Design view, shrink the width of the Details section so it’s just wide enough to fit your controls.

    If you leave extra blank space, it’s harder to see anchoring at work. Flip back to Figure 13-2 for a quick review of how to size forms properly.

  2. Choose the controls that you want to expand along with the window’s size.

    If you have the form shown in Figure 13-7, you may choose all the text boxes. Hold down the Shift key while you click to select them all.

    Figure 13-9 shows the final result you’re after.

    Note

    If your controls are in a layout, you need to remove them from the layout before you apply anchoring. Although you can apply anchoring settings to an entire layout container, they won’t work the way you want them to because they’ll influence the size of both the field value boxes and the field captions.

    Top: These text boxes could stand to grow so they can accommodate long names and email addresses. But you don’t want them to be so wide that people need to scroll from side to side if their monitors have a lower resolution than yours.Bottom: The solution is anchoring, so the text boxes always use the available space, and not one whit more.
    Figure 13-9. Top: These text boxes could stand to grow so they can accommodate long names and email addresses. But you don’t want them to be so wide that people need to scroll from side to side if their monitors have a lower resolution than yours. Bottom: The solution is anchoring, so the text boxes always use the available space, and not one whit more.
  3. Choose Form Design Tools | Arrange→Position→Anchoring→Stretch Across Top. (If you prefer to adjust your anchoring settings in Layout view, then choose Form Design Tools | Arrange→Position→Anchoring→Stretch Across Top.)

    This action anchors your control to three sides of the form: the top, left, and right. The top anchoring just makes sure the control stays at the same vertical position if the form window grows taller or shorter. The anchoring to the left and right sides ensures that the text box grows as the form widens, and shrinks as it narrows.

Making a Single Control as Large as Possible

In the previous example, you saw how to use anchoring to make a control grow horizontally. You can also use anchoring to make a control grow vertically, but there’s a catch. In most forms, you have several controls placed one on top of the other. If you’re not careful, when a control gets taller, it starts blotting out the control underneath it. The solution is to make sure that only one control on the form can grow vertically. This control (probably a large field that’s stuffed with text) then expands to consume all the extra space. All the controls above this control must be anchored to the top of the form. All the controls underneath it must be docked to the bottom so they stay out of the way.

Here’s how to put this model into practice:

  1. In Design view, shrink the width of the Details section so it’s just wide enough to fit your controls.

    As with all types of anchoring, extra space is your enemy.

  2. Select the control that you want to grow vertically to get the extra space.

    Consider the form in Figure 13-10, which shows customers. In this case, it’s the Notes field that has the most text and would benefit most from the extra space. Even if you anchor the Notes field to both sides, you’ll still get only a bit of extra space. Better to use whatever blank space you can get at the bottom of the form.

  3. Choose Form Design Tools | Arrange→Position→Anchoring→Stretch Down and Across.

    This action anchors your control to all four sides of the form: the top, bottom, left, and right. As a result, the control grows when the form is widened or heightened. If you want the control to grow vertically but not horizontally, you choose Anchoring→Stretch Down instead.

  4. Select the first control under the control that grows vertically. Choose Form Design Tools | Arrange→Position→Anchoring→Bottom Left.

    This action anchors the control to the left and bottom sides. That way, as the form is heightened, the control drops down to make space for the one above.

    You could also use the Stretch Across Bottom option. In this case, the control is still anchored to the bottom, but it grows horizontally to fit the width of the form.

    Note

    In the previous example, you didn’t need to anchor the labels in front of every field, because they stayed fixed in place. However, in this example, you do need to use Bottom Left anchoring for all the labels that appear underneath the control that stretches vertically. (Otherwise, this label doesn’t line up with its value box.) You never use one of the stretch anchoring options with a label, because you don’t want your label to change size.

  5. Repeat step 4 for each control underneath.

    If you forget a control, you see a telltale sign. When you resize the form window smaller, some controls overlap because the different anchoring settings don’t agree.

    Assuming you anchor everything correctly, you get the result that’s shown in Figure 13-10.

Now, when the form gets taller, the Notes field gets the extra room.
Figure 13-10. Now, when the form gets taller, the Notes field gets the extra room.

Tab Order: Making Keyboard Navigation Easier

When you’re using a form to edit a record, you need to skip from one field to another. You can jump anywhere you want by using your mouse, but hard-core keyboard jockeys don’t want to waste time raising their fingers from the keys. Here’s where the Tab key comes into the picture.

You probably already know that the Tab key lets you move from one control to another in any Windows application. The Tab key also works in the datasheet, letting you skip from one column to the next. So it should come as no surprise that the Tab key also works in your forms.

The first time you press Tab in a form, you may be in for an embarrassing surprise. If you’ve spent a fair bit of time fiddling with your controls and rearranging them, the Tab key doesn’t necessarily take you to the control you expect. Figure 13-11 illustrates the problem.

Note

The Tab key always works correctly if you’re using a tabular or stacked layout, because Access keeps it up-to-date as you move the controls around. It’s only when you’ve taken your controls out of a layout that you’ll see the problem described here.

You’d expect that you could press the Tab key here to jump from the FirstName field to the LastName field. But try it and you wind up in the Country field instead, halfway down the form.
Figure 13-11. You’d expect that you could press the Tab key here to jump from the FirstName field to the LastName field. But try it and you wind up in the Country field instead, halfway down the form.

Getting a form straightened out so that the Tab key moves from one control to the next in an orderly fashion is called setting the tab order. Essentially, every control that supports tabbing has three important properties (which you can find in the Other tab in the Property Sheet). These properties are:

  • Tab Stop determines whether a control supports tabbing. If set to Yes, you can tab to this control. If you change this setting to No, then it doesn’t matter how much tabbing you do—you’ll never get here. When you first add a control, this property is always set to Yes.

  • Auto Tab has an effect only if the control is using an input mask (see Input Masks). If you set Auto Tab to Yes, as soon as you type the last character into the mask, you’re automatically tabbed to the next control. This feature is handy for really fast data entry, but it’s annoying if you make a mistake, because you’re tabbed out of the control before you can fix it.

  • Tab Index controls the tab order—in other words, where you go each time you press Tab. When you first open a form, you start at the control that has a tab index of 0. When you press Tab, you then move to the control with the next highest tab index (like 1). This process continues until you reach the control with the highest tab index. Press Tab again, and you’ll get to the first control on the next record.

Note

The only controls that have these properties are controls that can accept focus—in other words, controls you can click and interact with. Obviously, text boxes, checkboxes, and buttons support tabbing. However, labels and pictures don’t, because there’s no way to interact with these items.

Every time you add a new control, Access gives it a new, higher tab index. Even if you drop a new control at the top of the form, Access puts it at the end of the tab order. To fix this problem, you could select each control in Design view, and then change the Tab Index setting by hand. However, a much less time-consuming alternative lets you set the tab order for the entire form in one go. Here’s how it works:

  1. Right-click a blank spot on the form design surface, and then choose Tab Order.

    The Tab Order window appears. It lists all the controls on your form that support tabbing, from lowest to highest Tab Index.

  2. In the Selection list, choose the section of your form you want to work with. It’s almost always the Details section.

    The Tab Order window lets you set the tab order separately for each section of your form. If your form includes a header and footer, you can choose to work with the header, footer, or Details section. However, it’s very rare to find a form that has controls to support tabbing outside the Details section.

  3. If you want to let Access take a crack at setting the correct tab order, click Auto Order.

    When you click Auto Order, Access sets the tab order based on the controls’ position. The order goes from left to right, and then from top to bottom. Most of the time, the Auto Order feature gets you the correct tab order (or at least gets you closer to it). However, it doesn’t work as well if you have a multicolumn layout and you want to tab through the controls one column at a time.

  4. To move a single control to a new position in the tab order, drag it.

    This step is a bit tricky. Figure 13-12 shows how it works.

    To reposition a control in the tab order, begin by clicking the gray margin that’s just to the left of the control. The entire row is selected. Next, drag the control to a new position on the list. In this example, the Country field is being moved down the tab order.
    Figure 13-12. To reposition a control in the tab order, begin by clicking the gray margin that’s just to the left of the control. The entire row is selected. Next, drag the control to a new position on the list. In this example, the Country field is being moved down the tab order.
  5. Click OK when you’ve got the tab order perfected.

Tip

The tab order goes both ways. You can move one step forward in the tab order by pressing Tab, and you can move one step backward by pressing Shift+Tab.

Taking Control of Controls

So far, you’ve seen how to create a form from scratch and how to add all the controls you want. However, you haven’t used this newfound power to do anything special. Sure, you’ve picked up the ability to add extra labels, lines, and rectangles. But that kind of eye candy pales in comparison to the truly helpful features Access lets you add to your forms. Want to prevent people from entering buggy data? Check. Want to add web-style hyperlinks? No problem. The list of what you can do to soup up your forms’ abilities is almost endless. The following sections show you the most popular ways to take charge of the controls on a form.

Locking Down Fields

In a database, almost every piece of information is subject to change. However, that doesn’t mean people should have free range over every field.

Suppose Boutique Fudge creates a form named CurrentOrders that lets people in the warehouse review outstanding customer orders, sorted by date. The warehouse personnel need to review each order, pack it up, and then ship it out. The only change they need to make is to update the order status (to indicate when it’s been sent out), or to add a record to the shipment log. Other details, like the order date, the order contents, and the customer who’s receiving the order, should be off limits. The warehouse people have no reason to change any of this information.

Forms are powerful tools in scenarios like this, because they let you prevent changes in certain fields. That way, there’s no chance that a misplaced keystroke can wipe out a legitimate piece of information.

Every bound control (a control that displays a field from your table) provides the following two properties that you can use to control editing. You can change these properties by using the Property Sheet in Design view.

  • Locked determines whether you can make changes in a field. If Locked is set to Yes, you can’t edit the field value. However, you can still select the text in a text box, and then copy it.

  • Enabled lets you deactivate a control altogether. If Enabled is set to No, the control appears with dimmed (gray) text. Although you can still see the field value in a disabled control, you can’t interact with it in any way. If it’s a text box, you can’t even select and copy the text it contains.

Note

If you want to prevent edits altogether, consider using the Allow Edits, Allow Deletions, and Allow Additions form properties instead, which are described on More Useful Form Properties.

Prevent Errors with Validation

In Chapter 4, you learned how to prevent errors from creeping into your tables by using validation rules, default values, and input masks. This bulletproofing is an essential part of database design.

However, validation rules don’t help in some situations because the rules apply sometimes, but not always. You don’t want the salespeople at Boutique Fudge to enter a new order with an old date. Clearly, that’s a mistake—a new order should receive today’s date. To try and stamp out the problem, a clever database designer like yourself may use the following validation rule on the OrderDate field:

<=Date()

However, a few weeks later you discover that the catering department neglected to enter the information about their orders on time. For recordkeeping purposes, these orders need to indicate when the order was originally placed. So you need to remove your well-intentioned validation rule before you can enter these records.

It turns out situations like these abound in real life. Fortunately, there’s a way to handle this scenario without giving up on validation. The trick is to place the validation in the controls on the form. That way, different forms can use different validation rules. If you want to make completely unrestricted changes, you can edit the data directly by using the datasheet for the table.

If you plan to move the validation out of your tables and into your forms, you’ll be interested in the following control properties, which you can tweak in the Property Sheet:

  • Validation Rule sets an expression that the value must meet to be considered valid. For example, the expression <=Date() compares the current field value to the date returned by the Date() function (which is today’s date). The entry is allowed only if it’s today’s date or before. You can find many more examples of validation expressions starting on Writing a Field Validation Rule.

  • Validation Text sets the error message that appears if you attempt to enter a value that violates the validation rule. This custom text replaces Access’s generic error message—“The value you entered does not meet the validation rule defined for the field or control”—which doesn’t make much sense to real people.

  • Input Mask sets a pattern that both guides and restricts people’s input. Input masks are a good way to deal with fixed-length text values like phone numbers, postal codes, and social security numbers. Input Masks has more about how input masks work and how to create them.

  • Default Value sets the value that appears in a field when you create a new record. (You’re free, of course, to change the default value if it’s not what you want.) You’ll find it particularly useful to set default values at the form level, because defaults often apply to a specific task rather than to the entire table.

Note

You can set a default value for the same field at the table level and the form level. If you do, the form’s default value takes over.

Performing Calculations with Expressions

An expression is a formula that manipulates some information, like numbers, dates, or text, and displays the final result (see Figure 13-13). Often, expressions perform calculations with field values. You’ve used expressions before to crunch the numbers in queries (Chapter 7) and reports (Chapter 10), and now you’ll put them to work in your forms.

In this form, the expression = “You have” & [DueDate]-Date() & “days to finish” calculates the number of days between the current date and the project due date, and places that number in a complete sentence. You’ll see this information appear as soon as you type in a due date and move to another field. (You can get around this requirement, and force the fields to update themselves as you type, by using a tiny bit of VBA code that triggers a recalculation. Page 622 shows an example.)
Figure 13-13. In this form, the expression = “You have” & [DueDate]-Date() & “days to finish” calculates the number of days between the current date and the project due date, and places that number in a complete sentence. You’ll see this information appear as soon as you type in a due date and move to another field. (You can get around this requirement, and force the fields to update themselves as you type, by using a tiny bit of VBA code that triggers a recalculation. Page 622 shows an example.)

To create an expression, follow these steps:

  1. Add a text box control to your form (from the ribbon’s Form Design Tools | Design→Controls section).

    You need to use the text box, because it can show dynamic values like expressions. A label can show only a fixed piece of text, so it’s no help.

  2. In the Property Sheet, choose the Data tab. Place your expression in the Control Source setting.

    Remember, expressions start with the equal sign. The expression =Price*1.15 calculates the price with tax for a product by multiplying the value in the Price field by 1.15.

  3. Optionally, set Enabled to No to hammer home the point that this value can’t be edited.

    When you create a control that uses an expression, Access doesn’t let you edit the calculated value. It’s just as if you set the Locked property to Yes. However, some people may still try to change this value. If you think this scenario is a problem, set Enabled to No so that the control appears dimmed and nobody can tab to it. This setting also means that you can’t copy the value in the text box.

  4. Optionally, apply formatting.

    You can adjust fonts and colors by using the ribbon’s Form Design Tools | Format→Font section. You can configure the way Access shows numeric values by using the Form Design Tools | Format→Number section.

Note

To remove the border around a text box (so it looks more like a label), select it in Design view, choose Form Design Tools | Format→Control Formatting→Shape Outline, and then pick the first item in the list (which is blank, signifying “no line”).

Organizing with Tab Controls

One of the control world’s unsung heroes is the tab control, which lets you present large amounts of content in a limited space. The tab control’s trick is the way it lets you organize this content into separate pages. You can see only one page at a time, and you choose which one by clicking the corresponding tab (see Figure 13-14).

It makes sense to put customer address information on a separate tab, because all these fields make up a logical group of related information.
Figure 13-14. It makes sense to put customer address information on a separate tab, because all these fields make up a logical group of related information.

The tab control isn’t all good news. Its main drawback is that you need to use extra clicks to get from one tab to another. For that reason, the tab control isn’t a great choice in forms that you’ve set up to create new records. In those instances, it’s better to streamline the new-record creation process and have all the controls on one page, so you can move through them quickly. A tab control makes most sense in forms that are primarily designed for editing or reviewing data. If this data can be subdivided into logical groups, and if editing tasks often involve just one group, then the tab control is a good choice.

To use a tab control, follow these steps:

  1. In the ribbon’s Form Design Tools | Design→Controls section, click the Tab Control.

  2. Draw the tab control onto your form in the place you want it.

    You’ll want to make it fairly large, so it can accommodate the content you’ll place inside.

  3. Add all the tab pages you need.

    Every new tab control starts with two pages. You can move from page to page by clicking the correct tab.

    To create a new page, right-click any tab and choose Insert Page. To remove an existing page, right-click it and choose Delete Page.

  4. Give the tabs good names.

    The tabs that Access creates start out with pointless names like Page19 and Page20. To change the name, select the page, and change the Caption setting in the Property Sheet. The page that displays customer address fields could have the caption “Address Information.”

    To rearrange your pages, right-click the tab control, and then choose Page Order. Access opens a Page Order window with a list of tabs. To change the order of a tab, select it, and then click Move Up or Move Down.

    Note

    If you create more pages than can comfortably fit in your tab control, Access adds a strange scroll bar in the top-right corner that lets you scroll through the tabs. To avoid this oddity, resize your tab control so that it’s wide enough to fit every tab, or avoid using long tab names.

  5. Place controls on the different pages.

    You can drag controls from the rest of your form onto a page, or you can add new controls from the ribbon. Either way, remember to select the tab you want first, and then add the controls you need. Even in Design view, you can see only one page of a tab control at a time.

Tip

If your controls are in a layout, you can’t drag them into a tab. Instead, select them, right-click your selection, and then choose Cut. Next, right-click inside the tab page where you want to place them, and then choose Paste.

Links are the less powerful cousin of buttons. Whereas command buttons (Performing Actions with Command Buttons) can perform almost any action, links are limited to exactly two tasks:

  • Launching your default browser and navigating to a specific site.

  • Opening a file (like a Word document) in the program that owns it.

To create a link, follow these steps:

  1. In the ribbon’s Form Design Tools | Design→Controls section, click the Hyperlink.

    The Insert Hyperlink window appears (see Figure 13-15). Using this window, you can supply the text for the link and the destination where the link transports people when clicked.

  2. Click the Existing File or Web Page option on the window’s left side.

    You can also use the “Object in This Database” option to create a link that opens another database object, like a form. However, command buttons are better suited to this task.

    Alternatively, you can choose E-mail Address to create a link to an email address. When you click this link, your default email program launches and creates a new message with the starter text you supply.

    Someone is about to create a new hyperlink. It will appear with the text “Click here to visit the company website” (which, of course, you can edit to say anything you want).
    Figure 13-15. Someone is about to create a new hyperlink. It will appear with the text “Click here to visit the company website” (which, of course, you can edit to say anything you want).
  3. In the “Text to display” text box, enter whatever you would like the link to say.

    Common choices for the text include the actual web address (like www.mycompany.com) or a descriptive message (like “Click here to go to my company’s website”).

  4. If you want to set a custom tooltip for this hyperlink, click the ScreenTip button. Type in your message, and then click OK.

    As you no doubt already know, a tooltip is a little yellow message-bearing window that opens above a hyperlink when your mouse pointer hovers over the link. If you don’t specify a custom tooltip, Access shows the full path or URL (web address) instead.

  5. If you want to add a link to a document, browse to the appropriate file, and then select it. If you want to add a link to a web page, type the URL into the Address text box.

    If you’re adding a link to a document, Access sets the address to the full file path, as in C:DocumentsResume.doc. You can type this path in, and if your network supports it, you can use UNC (Universal Naming Convention) paths that point to a file on another computer using the name of the computer, as in \SalesComputerDocumentsCompanyPolicy.doc.

    Note

    You’re free to link to files on your computer or those that are stored on network drives. Just remember that when you click the link, Access looks in the exact location you’ve specified. If you move the target file to a new location, or you open the database on another computer, Access can’t find the linked file.

  6. Click OK to insert the hyperlink.

    The new hyperlink appears on your form. You can then drag it wherever you want.

    To use a hyperlink, just click it. You’ll notice that the mouse pointer changes to a pointing hand as soon as you move over the hyperlink.

Showing Web Pages with the Web Browser

Access includes a Web Browser control that lets you embed a web browser window in a form. This way, you can display web pages that are in some way related to your record data.

There are two scenarios where the Web Browser control makes sense:

  • You have a record that includes a web link (otherwise known as a URL). For example, a Businesses table might include a field named CompanyWebSite. When you view a business record in a form, you can use the Web Browser control to show that website.

  • You have a record that includes some data that can be incorporated into a web link. For example, you might have a product code that can be fused into a URL to send a browser to a product catalog page on your company’s website. Figure 13-16 shows an example.

The Books table includes a field named Amazon_ASIN, which stores an Amazon identification number. (For books with ten-digit ISBN numbers, the ASIN and the ISBN are the same.) When you view a book record in the BookDisplay form, the Web Browser uses the ASIN value to display the book’s Amazon product page, which includes up-to-date reviews and a sales ranking. You can find this example in the AmazonBooks database with the samples on the Missing CD page (www.missingmanuals.com/cds/access13mm).
Figure 13-16. The Books table includes a field named Amazon_ASIN, which stores an Amazon identification number. (For books with ten-digit ISBN numbers, the ASIN and the ISBN are the same.) When you view a book record in the BookDisplay form, the Web Browser uses the ASIN value to display the book’s Amazon product page, which includes up-to-date reviews and a sales ranking. You can find this example in the AmazonBooks database with the samples on the Missing CD page (www.missingmanuals.com/cds/access13mm).

Here’s how to use the Web Browser control to build an example like the one shown in Figure 13-16:

  1. In the ribbon’s Form Design Tools | Design→Controls section, click the Web Browser Control and drag it onto your form.

    Assuming you have Control Wizards switched on (Adding Controls to Your Form), the Insert Hyperlink window appears. Using this somewhat awkward window, build the URL you want to use with the Web Browser. You have to assemble the web address out of several distinct pieces, each of which is classified differently. But if you understand the basics of expressions, it’s far easier to press Esc (or click Cancel) and configure the Web Browser on your own, as described in the next step.

  2. Using the Property Sheet, choose the Data tab and set the Control Source property with an expression that builds the web address.

    A URL is nothing more than a long piece of text. As you’ve already learned, any expression can join pieces of text together using an & symbol (which is known as the concatenation operator). With this in mind, it’s easy to build an expression that creates the URL you need.

    Consider the example shown in Figure 13-16. Amazon URLs can be written in several different forms, but one of the most common is this:

    http://amazon.com/o/ASIN/ASIN-VALUE-HERE

    So, if a book has an ASIN of 1449382371, you can reach its Amazon product page by typing this URL into a browser:

    http://amazon.com/o/ASIN/1449382371

    Now, assuming the ASIN is stored in a field named Amazon_ASIN, you can build the right URL for any record by using this straightforward expression:

    ="http://amazon.com/o/ASIN/" & [Amazon_ASIN]

    And that’s what you need to type into the Control Source property.

  3. Switch to Form view and try it out.

    As you move from one record to the next, the matching Amazon page loads up in the web browser window.

Note

When you set the Web Browser URL, you set its starting point. The person using the form can click hyperlinks to navigate somewhere else. However, the Web Browser control doesn’t include an address bar, so the person can’t type a URL to just anywhere.

There are two list controls in Access forms: the list box and the combo box. The difference is that the list box shows several items at once (depending on how large you make it). The combo box shows just one item—to see the list, you need to click the drop-down arrow.

Access gives you two ways to use list controls:

  • You can use them to edit a field. Access automatically creates a combo box control when there’s a lookup defined for the field. This combo box works the same as a lookup list in the datasheet.

  • You can use them to navigate to the record you want. In this case, the list shows the field value for every record in the table. When you choose one of the values, Access jumps to the corresponding record.

Using lists for navigation is a true Access power trick. If you often look for a record by using the same criteria (like if you hunt down products by name or employees by Social Security number), this technique is much faster than using the navigation buttons or filtering the records.

Here’s how to add a navigation list for an existing form:

  1. Make sure the Control Wizard feature is turned on.

    If you’re not sure, check that the Form Design Tools | Design→Controls→Use Control Wizards button is highlighted.

  2. In the ribbon’s Form Design Tools | Design→Controls section, click List Box or Combo Box.

    Both these controls work exactly the same when you’re using them for navigation. The only difference is that the List Box takes more space. If you decide to use it, place it at the side of the form. People usually choose the Combo Box (Figure 13-17).

    In this form, a Combo Box lets you jump to the product you want with one click. Notice that this list doesn’t take the place of the ProductName text box control. You use the list to find the record you want, and the text box to change the product name. Of course, if you never need to change product names in this form, you don’t need to include the ProductName text box.
    Figure 13-17. In this form, a Combo Box lets you jump to the product you want with one click. Notice that this list doesn’t take the place of the ProductName text box control. You use the list to find the record you want, and the text box to change the product name. Of course, if you never need to change product names in this form, you don’t need to include the ProductName text box.
  3. Draw the control on the form.

    As soon as you finish, a wizard appears to help you set up the list (Figure 13-18). This process works in a similar way to the Lookup Wizard you used to set up table relationships (Lookups with Related Tables).

    When you create a list, the List Box Wizard lets you choose to use it for editing or navigation.
    Figure 13-18. When you create a list, the List Box Wizard lets you choose to use it for editing or navigation.
  4. Choose “Find a record on my form,” and then click Next.

    If you don’t see the “Find a record on my form” option, there are two possible problems:

    You have a new, completely blank form. This form isn’t set up to display any records, so you can’t use a list to hunt one down.

    The other, more likely snafu, is that the data source of your form isn’t a table or query object. This situation occurs if you start with a blank form and then build it up with fields from the Field List pane. Access writes a query statement on the fly and uses that query as the form’s data source. Ordinarily, this sleight of hand isn’t a problem, except it confuses the List Box and Combo Box wizards. The fix is to change the form’s data source so it points directly to the table that has the data: Open the Property Sheet, choose Form in the list at the top, click the Data tab, and then set the Record Source underneath to the correct table. Then, return to step 3.

    However, there’s a potential complication. If you’ve built your form using fields from related tables, no single table will do. Instead, you need a query. You could create one by hand, save it, and then pick it in the Record Source list. But there’s an easier option. Click in the Record Source box and then click the ellipsis (…) button to open the ad hoc query that Access has already created for your form. Then, save it as a separate query object in your database (choose Query Tools | Design→Close→Save As). Now you can pick the query from the Record Source list and return to step 3 to add the List Box control.

  5. Choose the field you want to use for the lookup, and then click Next.

    The example in Figure 13-17 uses the ProductName field. Technically, the list always works the same way—it finds items based on their unique primary key value (The Primary Key). The list you’re creating actually has two columns. The first column stores the primary key, and the second column shows the value that’s in the field you selected. However, on your form, you don’t see the primary key, because it’s hidden.

    Note

    This technique doesn’t work as well if the field you pick allows duplicates. If you create a list that uses the LastName field, you may spot more than one MacDonald. In this case, consider adding more than one field to your lookup list (like both the LastName and FirstName).

  6. Leave the “Hide key column” checkbox turned on, and click Next to continue.

    If you don’t plan to show the primary key column—and usually you won’t—just click Next to breeze past this window.

  7. Enter a text caption for your list.

    This caption appears in a label next to the list control. You may want to use something like “Click the product you want to see.” You can move or delete the label after the fact.

  8. Click Finish to create the list.

    Now you can try out your list. Right-click the tab title, and choose Form View to switch back to the form. Then, choose an entry from the list to jump straight to the appropriate record.

Note

List-based navigation has one quirk. If you change the value that appears in the list, Access doesn’t update the list until you move to another record. In the previous example, this property means that if you rename a product, the old name appears in the list until you move on.

Performing Actions with Command Buttons

The last control you’ll consider is one of the most powerful. Command buttons let you trigger just about any action, like opening a new form, printing a report, or polishing off last year’s taxes. (All right, some tasks are more difficult than others, but if you’re willing to hunker down with some Visual Basic code, almost anything is possible.)

When you add a button to a form, Access launches the useful Command Button Wizard, where you can choose the action you want from a list of readymade choices. The Command Button Wizard then helps you build a macro (see Chapter 15) that does whatever you requested.

The choices in the Command Button Wizard provide a good menu of possibilities. Some Access fans find that they can do almost everything they want to do by just using buttons and the wizard. Other people eventually want to do something more original, in which case they need to create their own macros or write custom code (tasks you’ll tackle in Part 5).

The following steps lead you through the Command Button Wizard:

  1. In the ribbon’s Form Design Tools | Design→Controls section, click the Button.

  2. Draw the button onto your form.

    When you finish, the Control wizard starts, and gets right down to business. The first questions it asks is what action you want to perform (see Figure 13-19).

    You can instruct your form to carry out six categories of actions. Once you select a category (in the list on the left), you see a list of actions in that category (in the list on the right).
    Figure 13-19. You can instruct your form to carry out six categories of actions. Once you select a category (in the list on the left), you see a list of actions in that category (in the list on the right).
  3. Choose the action you want to perform.

    Most actions are self-explanatory. Here are some of the highlights:

    • In the Record Navigation category, you can use commands like Go To First Record, Go To Last Record, Go To Next Record, and Go To Previous Record to create your own navigation buttons. If you do, set the form’s Navigation Buttons property to No to hide the standard buttons.

    • In the Record Operations category, you can create a new blank record (Add New Record) or do something with the current one (like Delete Record, Duplicate Record, and Print Record). You can even choose to commit changes right away before you navigate to the next record (Save Record), or undo the last change (Undo Record).

    • In the Form Operations category, you can close the current form (Close Form) or print it (Print Current Form). You can also open another form (Open Form), which is one of the most used button actions because it helps you move from task to task.

    • In the Report Operations category, you can work with other reports using commands like Open Report, Preview Report, and Print Report. These actions help you make the jump from reviewing data (in a form) to printing it (in a report).

    • In the Application category, you’re limited to one action—the self-explanatory Quit Application.

    • In the Miscellaneous category, you’ll find options to run a separate query (Run Query) or fire off a macro (Run Macro). You’ll learn how to create macros in Chapter 15.

  4. Click Next.

    The next step depends on what action you selected. Some actions require extra information. If you chose to show a form or print a report, Access prompts you to pick the form or report you want to use.

    Once you’ve finished supplying any extra information, Access asks you to supply the button text and choose a picture (Figure 13-20).

    Pictures are tempting, but the ones Access includes are decidedly old-fashioned. Most Access fans decide to create picture-less buttons. If you want to include a picture, turn on the Show All Pictures checkbox to see everything Access has to offer (even pictures that may not make sense for your current action), or use the Browse button to add your own picture.
    Figure 13-20. Pictures are tempting, but the ones Access includes are decidedly old-fashioned. Most Access fans decide to create picture-less buttons. If you want to include a picture, turn on the Show All Pictures checkbox to see everything Access has to offer (even pictures that may not make sense for your current action), or use the Browse button to add your own picture.

    Note

    Any bitmap (.bmp file) works for your button picture, so long as it’s small enough to fit. Icons, .jpeg, and .gif files are also acceptable.

  5. Enter some text and choose a picture. Then, click Next.

    You can change these details after the fact by modifying the Caption and Picture properties (which appear in the Format tab).

  6. Supply a name for the button.

    The name is what appears in the Property Sheet list. Better names make your button easier to find. And if you write code that works with your buttons (Chapter 17), better names make for code that people can more easily read and understand.

  7. Click Finish.

    To try out your button, switch to Form view, and then give it a click.

  8. Optionally, use the commands in the Form Design Tools | Format→Control Formatting section of the ribbon to give your button a slick new style.

    Buttons have a unique ability. With a few mouse clicks you can replace the drab look of a vanilla button with a fancy style that incorporates visual effects (like a bevel edge, shadow, glow, or outline). The fastest way to transform your button is to select it and pick a fancy style from the Form Design Tools | Format→Control Formatting→Quick Styles menu. Figure 13-21 shows several stylized buttons.

    The quick styles change several formatting characteristics at once, but you can also refine your button with more targeted changes using the Shape Fill, Shape Outline, and Shape Effects menus, which are also found in the Form Design Tools | Format→Control Formatting section of the ribbon.

This form includes several exotic button specimens that you can create by choosing an option from the Form Design Tools | Format→Control Formatting→Quick Styles menu.
Figure 13-21. This form includes several exotic button specimens that you can create by choosing an option from the Form Design Tools | Format→Control Formatting→Quick Styles menu.

Forms and Linked Tables

As you learned in Chapter 5, few tables are truly independent. Most are linked to others in a web of relationships. Forms can take advantage of these relationships to show linked information. You can use a single form to view (and edit) information about customers and their orders. Or, you can look at products and product categories. This freedom just isn’t possible in the Datasheet view.

Note

Enterprising Access developers use join queries (Joining Tables in a Query) to show information from more than one table. However, you can’t edit the linked information in a join query. In a properly designed form, you don’t have this limitation—you can change the information in both the parent and child records.

Table Relationships and Simple Forms

Access is intelligent enough to notice relationships when you create a new form for a parent table. To see what this ability means in practice, select a table that’s the parent of another table. You can use the ProductCategories table in the Boutique Fudge database, because every category is a parent record that’s linked to one or more child records in the Products table. (You can also use the Customers table, because customers are linked to orders, or the Orders table, because orders are linked to order items. To try this, use the Boutique Fudge database that’s included with the downloadable content for this chapter.)

Figure 13-22 shows what happens if you select the ProductCategories table and then choose Create→Forms→Form. (Don’t create a split form or a multi-item form. Access ignores relationships when you create these form types.) Access creates a form that displays the records you expect (the categories) and the linked records in the child table (in this case, the products).

This form lets you browse through the different product categories. Each time you move to a new category, a mini-datasheet on the form shows the linked product records. Using this form, you can edit products and product category information.
Figure 13-22. This form lets you browse through the different product categories. Each time you move to a new category, a mini-datasheet on the form shows the linked product records. Using this form, you can edit products and product category information.

If your table is the parent of more than one child table, Access shows only records from one table. It chooses the first relationship it finds. If this relationship isn’t the one you want, don’t worry—it’s easy to change it once you understand how the subform control works. The next section has the inside scoop.

The Subform Control

Access shows linked records using the subform control. You can add the subform control to any form to show linked records. It’s available in the ribbon’s Form Design Tools | Design→Controls section with all the other controls. If you add it by hand, Access prompts you to pick the table you want to show.

Three properties determine what the subform control shows. First, the Source Object property identifies the object in the database that has the related records. You can choose an existing table, query, or form.

The next two properties—Link Master Fields and Link Child Fields—let you define the way the two tables are related. The master field is the field in the form, and the child field is the field in the source object. In the product category example, the master field’s ID (in the ProductCategories table) and the child field’s ProductCategoryID (in the Products table) are linked. Once this link is defined, Access knows how to filter the subform. It looks at the master field and displays only records that have the same value in their child fields. In Figure 13-22, Access shows only the products in the current category.

Usually, the master field corresponds to the parent table, and the child field is in the child table. However, you can reverse this relationship. You could create a form of products that includes a subform that shows each product’s matching category. When you use this approach, the subform includes only a single record (because only one parent is linked to any child).

Now that you understand how the subform control works, you can add it to your forms with wild abandon. There’s no reason you can’t add several subforms to show a whole collection of related data at once. If you’re creating a form for the Customers table, you could display two subforms—one for the orders made by that customer, and one for the payments. You just need two subform controls with different data sources.

Tip

When your form includes a subform, consider using the anchoring features described on Controls that Overlap so the subform grows to fit the available space when the form is resized.

Creating Customized Subforms

When you set the Source Object property to a table or query, Access always displays the linked records in a mini-datasheet. If you’re intent on customizing every last piece of your form, you may not want that behavior. Interestingly, Access lets you control exactly how linked records are shown, if you do a little more work.

The trick is to set the Source Object to the form that you want to show in the subform control. Then, the form appears in its default view mode, which is whatever that form’s Default View property is set to. You can show linked records in a tabular or stacked form. Figure 13-23 shows an example.

Thanks to the magic of subforms, this window actually shows two forms at once: ProductCategories_WithSubform and ProductsInCategory_Subform. In this example, the subform uses Continuous Form view (page 409), so it shows a list of all the matching products. To see the rest of the products, you need to use the second set of navigation buttons (circled).
Figure 13-23. Thanks to the magic of subforms, this window actually shows two forms at once: ProductCategories_WithSubform and ProductsInCategory_Subform. In this example, the subform uses Continuous Form view (page 409), so it shows a list of all the matching products. To see the rest of the products, you need to use the second set of navigation buttons (circled).

Depending on the effect you’re trying to achieve, you may already have a suitable form lying around ready to use. If you’re designing a form for the ProductCategories table, you can use the form you created for the Products table in a subform control. However, you’ll often want to use a completely separate form so you can customize it. In the Products table example, you may want to show products differently in the subform than they appear in their own dedicated form. After all, there’s less space available when you use the subform control, so you may choose a more compact format and leave out the report header altogether.

Tip

If you choose to create a dedicated form to use with a subform control, consider indicating that in the name. The name “ProductsInCategory Subform” suggests a form that’s designed for use as a subform.

Sometimes, try as you might, there’s no way to fit everything in the small subform area of a form. In this case, you have two options: Try to rearrange your subform to make it more compact, or use two separate forms. Linking to Related Data in Chapter 14 shows how you can use navigation and filtering to show related records in a separate form.

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

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