We’ve mentioned forms briefly in previous lessons but haven’t discussed them at length; in this lesson we’ll finally be able to do just that. So, what are forms? Forms, in Access, are essentially pretty data entry and data viewing objects that seem, to the user, similar to HTML-based forms, but much more advanced.
You can start developing your own forms in Access by simply clicking on the Forms object tab in the database window and clicking New in the database window toolbar. This will pop open a dialog box that gives you several options for creating different types of forms including: Design View, Form Wizard, various AutoForms, Chart Wizard, and PivotTable Wizard. I’ll discuss these options in just a few moments, but first I want to give you a brief overview of the form design elements in general.
Forms are composed of such things as record sources, controls, and properties for manipulating both form behavior and appearance. A record source is the form’s data source. Controls are objects used in forms to show data, perform certain actions, and improve appearance. Controls can be bound, unbound, or calculated. Bound controls are tied to the underlying data while unbound controls are not. Calculated controls by contrast, get their values from expressions that use the underlying data.
Go ahead and create your first form now so you can see what the form design window looks like. Select Design View and choose a record source on which to base your form. When you’re done you’ll see the form design window as shown below:
Access gives you the tools you need to design your form, including a design toolbar, a formatting toolbar, a toolbox, property sheets, fields lists, and alignment tools. The formatting toolbar is used to customize element appearances and the toolbox shows the available controls. Use the field list to drag fields from your record source to the design. Add controls to the design by clicking a control button in the toolbox and drawing in the design with your mouse. Create calculated controls by combining record source values in specific ways. For instance, suppose you have a record source from a costs database. You could create a calculated control by inputting values into a control by double-clicking a control, thus opening the properties dialog, and entering the calculated field into the control source property. You would then use an expression similar perhaps to one of the following:
[Price]*[Sales Tax]
Or
[Cost of Item]*1.09+[Shipping & Handling]
You can add symbols, such as the currency symbol, to the calculated field by setting the format property in the properties dialog. Choose the Currency option in the drop-down list in the Format property box. While you’re there notice the name of the unbound control you’ve created; it can be changed to suit your needs. That name is what Access uses to reference the control in macros, expressions, and procedures.
Tip 1: Assign the current date to a field by typing the following in the control’s Default Value property:
=Date()
Tip 2: Add a validation rule to a control in a form by entering the appropriate validation expression in the Validation Rule property.
Conditional formatting is another useful feature. It allows you to manipulate what certain controls look like under specified conditions. To use this feature just select the control you want to change, choose the Format | Conditional Formatting menu option and make your selections in the dialog box. You can set up to three conditions for changing a control’s text or background color, or text formatting such as bold, italic, or underline. You can set the following conditions:
Set the "Field Value is" condition to specify a value or range of values to which to apply formatting,
Set the "Expression Is" condition to specify what happens when an expression you enter is true, and;
Set the "Field has Focus" condition to make changes to a field that has current focus.
You can change the style of a form by opening the form in design view and choosing the form selector (upper-left corner), and then choosing the Format | AutoFormat menu option. Click the Options button to customize the properties of the design. This is a handy feature, especially if, like me, you are not satisfied with the design options Access gives you.
The form wizard is probably the easiest way to create a form. Double-click the "Create form by using wizard" option in the database window with the forms objects visible to use the wizard. Choose your record source(s) and field(s). Choose how you want to view your data, select a layout and style for your form, and finally give your form a name.
Instead of doing what you just did you could click the "New" button with the forms objects open in the database window to get the options I discussed previously, including the various AutoForm options. The AutoForm options include: Columnar, Tabular, Datasheet, PivotTable, and PivotChart styles. The Columnar style organizes the fields in columns while the tabular style puts the data for the records on a line across the form.
The Datasheet style is similar to the Datasheet view for tables and is generally used for subforms. Subforms show data from records with related data to the current record. The PivotTable style analyzes and summarizes your data, while the PivotChart style shows this analysis and summary graphically.
Once you’ve created your form you can modify the form’s design by adding headers and footers, resizing the form or window, adding special controls, moving or reformatting the controls, changing label text, adding lines, or changing the order of the cursor through the controls when the TAB button is used. These possibilities are available to you both from the form toolbar and through the form properties worksheet.
Let’s return to our first form, the one we created in design view. This is how the form starts out if you don’t use one of the AutoForm’s or the Form Wizard; it’s blank. When you create a new, blank form you can either choose to incorporate a record source as you did with the form wizard, or you can choose not to use a record source at all. Why would you want to do that? Well, you might want a form that merely shows a course of action the user can take, not based on any underlying data. However, you’ll generally want to use underlying data in your forms, so choose a record source.
In addition to the bound text box controls you created when you dragged fields from the field list previously, you can create list and combo boxes for your form that allow the user to choose from a list of values. Both controls can be bound or unbound. The two controls are similar but generally you use a list box when you have a smaller choice of items and a combo box when you have many items to choose from. You can also add Yes/No controls such as check boxes and option buttons, as well as toggle buttons such as option groups. To add any of these controls just make sure the Control Wizard button is pressed and then make your selection in the toolbox.
Forms are used to input data into your tables in an attractive and easy manner. One of the ways to assist the user in accomplishing this task is to use command buttons that perform certain actions when clicked. Add a command button by simply clicking the command button control in the toolbox and then clicking in the form design to create the button. A dialog shows you available actions; select the action you want the button to perform and give your button a name. It’s as simple as that!
You add calculated controls to the form by inserting a control in the form and changing the control source property of the control, just as we did before. Just enter the expression you want to use on your underlying data into the control source property of the control beginning with an = sign. The following are some examples of expressions you might use:
=Count([Last Name])
Or
=Sum([Price]*[Quantity])
The first expression above counts the number of records with a value in the Last Name field. The second one shows the sum of the values in each record for the price of a particular item multiplied by the quantity of the product. If you’re having trouble building expressions in Access you can always use the Expression Builder by simply double-clicking the (…) button next to the control source property for a control and entering the data you wish to use in the expression, including the operators.
One final thing I would like to mention in this lesson is Active X controls. You can see what Active X controls are available to you by choosing the Tools | Active X Controls menu option. From there you’ll be able to add things such as calendars, spreadsheets, and even animation to your form design! Active X controls give you many exciting options for your form design. Have fun, and in next week’s lesson we’ll review reports.