Archives: pcpress
Posted Date: July 11, 1995
CD article first appeared on: July 1995
Presented by: F. Scott Barker
F. Scott Barker, a former member of the Microsoft Access database and Microsoft Foxpro database teams, contracts with Microsoft. Scott is now doing contract development and training in Microsoft Access. He is also a writer and contributing editor for Access Advisor magazine, speaker at International Access Conferences, and recently Technical Editor for "A Visual Guide To Access", a book specializing in Microsoft Access 2.0. His company, Applications Plus, specializes in Microsoft Access.
Phone: (206) 485-5907
CompuServe 71042,1073
Microsoft® Access database forms offer great power for displaying and editing data, and provide the user interface for your database applications. Through properties, events, macros, and Microsoft Access Basic code, they can be extended in a variety of ways.
This session discusses tips and techniques for designing forms, and introduces form features available in Microsoft Access 2.0. Many important features of Microsoft Access 2.0 affect form design, allowing you to create more powerful forms for your applications. Along with the sample database, the session also provides implementations of several commonly desired form.
It is assumed that the reader has a basic familiarity with Microsoft Access forms and Form Design, as documented in the Microsoft Access User's Guide.
The sample database used in this session is called AC208.MDB. It contains several database objects that are based on the same tables as NWIND.MDB (the Northwind sample database shipped with Microsoft Access).
This section points out some form design features that are easily overlooked, but help make the task of designing forms much easier.
If you've used the field list to add text boxes to a form, you know how convenient it is that Microsoft Access automatically binds the controls to the fields you choose, sets properties from the field in the table, and attaches labels. It's also easy to create other bound controls, such as combo boxes or option groups, in the same way. Just select the type of control you want in the toolbox before dragging the field from the field list. Using this method, you'll be sure that the control inherits the correct property settings, and you won't usually need to edit the text in the label.
If you want to create several controls of the same type, use the tool lock in the toolbox to set the control type before dragging fields from the field list.
Form Design uses standard Microsoft Windows® conventions to select controls. A simple click selects a single control. Shift-click adds or removes one control from the selection, and clicking and dragging can be used to define a bounding box for multiple-control selections. As you progress past designing the simplest forms, you may find several less obvious techniques useful.
To select a form (rather than some section or control within that form) with the mouse, click in the empty white box between the horizontal and vertical rulers in the upper-left corner of the Form Design window. (A form can also be selected from the Edit menu.)
Normally, you can click on the background of a section to select it. However, if you have an opaque rectangle or other large control present on your form, it can sometimes be hard to find some section background to click on. In such cases, just click on the section bar displayed at the top of the section in design mode. The bar contains the text describing the section ("Form Header" or "Detail").
In Microsoft Access 2.0, the property sheet includes all properties that are common to the selected controls. This allows you to set properties for several controls much more easily.
A great help for form design can be found in the alignment, sizing, and spacing commands on the Format menu. Lining up controls in the desired arrangement - and fitting them all on your form - can be a tedious task. Microsoft Access 2.0 offers the Align, Size, Horizontal Spacing, and Vertical Spacing commands to help make this task quicker and easier. As soon as you size and position one control, select this control followed by others to size or line them up in one step.
Whenever you use a tool in the toolbox to create a control, the control is created with a default set of property values. Many of these default property values are under your control; the default property values are saved as part of the form's definition. Intelligent use of default property values speed the process of designing a form.
To set control defaults, select a tool in the toolbox, then set properties in the property sheet. You can also use the palette or toolbar to set default colors or fonts for controls.
In addition to speeding up the form-design process, default property settings can reduce the disk space used by a form and reduce the time it takes to open the form. Controls on a form are saved by recording those property values that differ from those of the default control. So, if all 20 text boxes on your form are blue and your form's default text box is red, then the saved form definition must include "blue" 20 times, once for each text box. If the default text box on the form is blue, then "blue" is recorded only once for all the text boxes.
Setting defaults for a single form is useful but time consuming. Often, you want all the forms in your application to have a similar appearance or behavior. To use the same control defaults in all new forms you create, use a template form. Set all the desired control defaults in the toolbox, and set general properties of the form such as background color and default style and view. Then, save the form as "Normal." New forms you create will use the same defaults as the Normal form.
Microsoft Access 2.0 includes several Wizards and builders that help make designing forms more friendly. This section discusses the Wizards and builders you'll use when designing forms.
Setting the many properties needed to get the desired behavior from controls requires a great deal of practice and patience; the Control Wizards can take on some of this burden.
For example, creating combo boxes, list boxes, and option groups requires setting many different properties. The properties are easily confused or overlooked, causing your controls to work incorrectly. When you add a control in Microsoft Access 2.0, a Control Wizard asks you questions about the control you want, then sets all these properties for you. The Option Group wizard even creates all the button controls in the group automatically.
You can always change control properties later in the property sheet if necessary. Additionally, if you don't want to use a Control Wizard, you can disable the feature by deselecting the button in the lower-left corner of the toolbox.
Perhaps the most useful application building tool is the Command Button Wizard. When you add a button to a form, the wizard gives you over 30 common actions that you can choose for the button to perform. The wizard gives the button a picture or text, and automatically sets up the code necessary to perform the action.
Certain form and control properties have builders available. For example, the Expression Builder can help set expressions for calculated fields or validation rules. You use a builder by clicking the dot-dot-dot button next to the property.
Another builder, the Query Builder, is especially useful for creating a Structured Query Language (SQL) statement for a record or row source setting. In version 1.x you had to type SQL into the property sheet (or use a saved query); you can now use the query by example (QBE) grid to create the queries your form needs.
Combo boxes and list boxes are among the most powerful yet complicated controls. You'll probably find it easiest in Microsoft Access 2.0 to use Control Wizards to create combo and list boxes, but it's also convenient to understand the important combo and list box properties so you can easily customize them.
This section discusses some common combo and list box functionality you might want to put into your own forms. Most of the techniques described here can be used with combo or list boxes, although combo boxes are used in the examples.
During the discussion, keep in mind the distinction between ControlSource, which is the table or query field to and from which the control saves and loads data, and RowSource, which is the source of the data displayed in the list. Other properties central to the discussion are ColumnCount, ColumnWidths, BoundColumn, and RowSourceType.
This section describes one of the best and most common Microsoft Access techniques for displaying data from more than one table. Because combo and list boxes support multiple columns, they allow you to easily display related data from another table without basing your form on a query that joins the tables. This technique, which involves a bound combo or list box control that stores an ID number but displays names in a list, is used throughout the Northwind sample database.
For example, suppose you're creating a form to display product information, and you want to identify the supplier for each product record. In a well-designed database, you store only a supplier ID number with each product record, while you store the supplier's name and other information in a separate table. You want your form to include a combo box that displays supplier names in the list, but stores supplier ID numbers in the field. (For an example of this technique, see the "Combo 1" form in AC208.MDB.)
To achieve this, you create a multiple-column combo box. Set the RowSourceType property of the list box to Table/Query. You could base the list on a table, but you want the list of names to be sorted; instead, set the RowSource property to a query that includes supplier ID numbers in the first field, and names sorted ascending in the second field. The easiest way to do this is using the Query Builder for the RowSource property to create an SQL statement; alternatively, you can create and save a query to provide the list. Finally, because we are interested in only the first two columns, set the ColumnCount property to 2.
The first column contains the data that is saved by the control when a row is selected by the user. This column is identified by the BoundColumn property (set to 1 by default). The bound column containing ID numbers doesn't have to be visible to the user. The ColumnWidths property contains a semicolon-separated list of visible column widths for the columns in the drop-down menu. Microsoft Access uses default algorithms to determine the widths of any columns for which you do not explicitly choose a width. If you choose a width of 0 for any column, that column is effectively hidden from the user on the screen, but it is not hidden from the rest of your forms, Microsoft Access Basic code, or macros. In this case you can set the property to 0, indicating to Microsoft Access that you want the first column to be invisible and the second column to have the default width. (You can use additional semicolon-separated numbers to specify widths for any of the remaining columns, instead of allowing Microsoft Access to calculate default column widths.)
The second column, in this case the name, is the one the user's text input is matched against. The first visible column in the combo box is always used for this purpose.
Note that combo boxes with a bound column equal to the first visible column can be painted more quickly than those with a bound column other than the first visible column, since Microsoft Access takes longer to display a different value than it stores in the field.
A lot of times you may want to add a choice to a combo box that allows you to select all the items in the list. An example of this would be if you wanted to print a activity report for either a particular supplier or all suppliers.
The steps you took to create the last combo box will also be used in this case except the Row Source. We are going to use a Union SQL statement to add our <<All Suppliers>> choice to the front of the list. Prior to this technique you had to either use Access Basic Code or add the choice to a table. (For an example of this technique, see the "Combo 2" form in AC208.MDB.)
Here is the Select statement that is used in the Row Source: Select 0 as [Supplier ID], "<<All Suppliers>>" as [Company Name] From Suppliers Union (Select [Supplier ID], [Company Name] From Suppliers) Order By [Company Name];
The expression for testing this case can be found in the "qCombo2" query in the query grid. The expression is:CheckSupplierToPrint: IIF(Forms![Combo 2]![txtSupplierToPrint] = 0, True, Forms![Combo 2]![txtSupplierToPrint]=[Supplier ID])
The criteria simply contains a the statement True.
Now if <<All Suppliers>> is chosen, the CheckSupplierToPrint expression equals True, otherwise it compares each Supplier ID to one used on the form.
You may want to use a combo or list box to let the user pick some record, and then update the contents of other form controls with corresponding values from that record. This section discusses the following ways to achieve this behavior without macros or Microsoft Access Basic code (for examples of using code to work with combo boxes, see "Application Design Techniques" later in this document):
AutoLookup is a feature of the Microsoft Access database engine that allows related fields on a form to display correctly when you change data. It applies if a query includes a one-to-many join and certain output fields. Forms based on queries perform AutoLookup as well (when the AllowUpdating property is set to Default Tables). Although AutoLookup behavior isn't particularly associated with combo or list boxes, it can be used with them to display related data when you select a value in a list. (For an example of this technique, see the "Combo 3" form in AC208.MDB.)
To use AutoLookup, create a query with a one-to-many relationship. Include fields from both tables, but be sure to include the linking value from the "many" table (not the "one" table), since this is the table in which you want to edit it. When you modify this field on the "many" side of the join, the Microsoft Access database engine automatically refreshes the output information fetched from the "one" side of the join. So, for example, if you have a combo box where you select the supplier for a product, when you select a supplier (which sets the Supplier ID value in the Products table), other fields such as the supplier's name and phone number are displayed correctly for the corresponding supplier.
Note that using AutoLookup requires that the one side of the one-to-many relationship be a query field (or a form-field bound to a query field). If you want to use an unbound combo box to select records, then AutoLookup isn't the solution.
Using the DLookup function, you can "look up" data in another field based on a choice the user makes in a list. Instead of including the value you want to display in a query with a join, you effectively run a separate query to fill the control each time it's displayed. Because each lookup expression runs its own query, this strategy is usually only appropriate if you're looking up one field. (For an example of this technique, see the "Combo 4" form in AC208.MDB.)
Any expression reference in one control's ControlSource property to another control in the same form causes automatic recalculation to occur. One control can have its ControlSource set to be an expression doing a DLookup based upon the value in another control. For example, to display an employee's phone extension in a text box when the employee's ID was selected using a multicolumn combo box, you would set the text box's ControlSource to something like:=DLookup("[Extension]", "Employees", "[Employee Id] = " & [Combo Name])
Note that the example would be typed in as one line in a text box's ControlSource property.
Note Run performance comparisons using this method against joining the tables in a query. Depending on the amount of data one method may be quicker than another.
A linked subform provides another possibility of displaying data associated with a record identified by a combo selection. In this case, you use the built-in capability of subforms to display related data as you make changes. (For an example of this technique, see the "Combo 5" form in AC208.MDB.)
Suppose you want to display the customer information for a customer selected with a multicolumn combo box. Create a new form with RecordSource property set to the Customers table and create text boxes in it to display various Customers fields. Embed that form in a master form. Place an unbound (empty ControlSource) combo box with RowSource also set to the Customers table. Set the LinkMasterFields property to the name of the combo box, and set the LinkChildFields property to Customer ID. In Form view, whenever you select a row in the combo, the subform automatically shows the data corresponding to the selected customer.
Subforms are useful for displaying information from two different tables or queries, where many records in one table (displayed in the subform) are associated with one record in another table (displayed in the master form).
The LinkMasterFields and LinkChildFields properties of the subform control are used to choose the records in the subform appropriate for each record in the master form. Whenever any of the LinkMasterFields is changed, Microsoft Access automatically re-queries the subform.
When creating a subform, you may want to display subform aggregate information in the master form. For example, you may want to display the count of the records in the subform somewhere on your main form. (For an example of this technique, see the "Subform 1" form in AC208.MDB.)
To put such an aggregate in the master form, you must first put it in the subform! Displaying aggregate information in the subform itself is straightforward. Place a text box wherever you want in the subform and put an aggregate expression, such as " = Count([Product Id])", into its ControlSource property. Either put the text box in the form's header or footer (if you don't intend to display the header or footer), or make it hidden.
In the master form, insert a new text box with ControlSource:
= Subform1.Form![Aggregate Control]
This control will update every time you change its value in the subform.
A one-to-many-to-many form is one in which there are two subforms on a main form. The first subform contains multiple records associated with the displayed (current) record in the master form. The second subform contains multiple records associated with the current record within the first subform. (For an example of this technique, see the "Subform 2" form in AC208.MDB.)
For example, suppose the main form displays customer data. The first subform lists all of the orders placed by a given customer. The second subform contains all of the order item details for the current order in the first subform.
First, create the main form and the first subform, which display customers and their orders. These forms are linked on Customer ID. Next, create a second subform to display order details, and set the LinkMasterFields and LinkChildFields for the subform control to [Order ID].
For the second subform to work correctly, you need an Order ID field on the main form with which it is synchronized. Put a hidden control on the master form and set its ControlSource to the expression:
= [Orders Subform].Form![Order ID]
Since the details subform is linked to this hidden field, each time you change a record in the first subform, the second is re-queried.
Since forms provide the user interface for your Microsoft Access applications, it's exciting to explore the capabilities of Microsoft Access 2.0 for combining forms with macros and Microsoft Access Basic code. This section discusses two major improvements that allow for much simpler and more powerful development with forms.
In version 1.x, the only form and control properties you could set from macros or Microsoft Access Basic code were the value of a control and whether it was visible. To change properties in an application, the developer had to resort to strange workarounds. In Microsoft Access 2.0, virtually all properties can be set at run time. This allows you to change colors, sizes, or behavior of your forms and controls in response to the user's actions.
For example, you can change the RowSource property of a combo box in response to some event so that the contents of the list will change. Examples of this and other techniques are shown in the application design techniques covered at the end of this session.
In Microsoft Access 2.0, forms have procedures associated with events and saved with the form definition. To create a procedure to respond to the OnCurrent event of a form, for example, you simply click the Code Builder button next to the OnCurrent property in the property sheet and write the desired code. The procedure is saved with the form itself. Only global procedures that you share between forms are stored in modules as before. This model makes it much easier to write, update, and keep track of your application's code.
The remainder of this session explores form techniques commonly used in creating the user interface for an application. Each technique combines macros or code with features of forms. Most of these techniques use features available in Microsoft Access 2.0.
The following techniques are discussed:
Creating a Dialog Box. By setting form properties such as the Modal and ScrollBars properties, adding unbound controls for data entry, and creating buttons with event procedures, you can create dialog boxes for your application's user interface.
Finding a Record Based on a Combo Box Selection. By responding to the AfterUpdate event of a combo box, you can let the user choose records from a list. You'll also want to update the combo box when the user navigates to another record in other ways.
Adding a New Record When It's Not in the List. If your user wants to add a new record rather than selecting an existing one in a list, you need to allow the user to make the entry, and then add the new entry to the list. In Microsoft Access 2.0, you can perform these actions in response to the NotInList event.
Setting a Control's Record Source During Runtime. Since Microsoft Access 2.0 lets you set nearly all properties while your application is running, it's possible to change the contents of a form, subform, or list on the fly. For example, you can limit the choices displayed in a combo box based on what the user selects in an option group.
Examples of these techniques can be found in AC208.MDB.