June 29, 2004

How to create OpenOffice.org forms

Author: Tamar E. Granor

In some situations, you want to create a document that contains some fixed text, but allows users to enter some items and perhaps take some actions on those items. Such a document is called a form, analogous to the preprinted forms everyone has filled out dozens of times. You can create custom forms in most of the OOo applications; forms you create without the Form AutoPilot don't have to connect to a data source (though they can -- see "How do I attach a data source to a form?" below). If the form isn't connected to a data source, information the user enters there is simply saved with the form. (You might do that when you want to send a form to a group of people for them to fill out and return.) Writer, HTML Editor, Calc, Impress, and Draw all support forms.

This article is excerpted from the recently published book OOsSwitch: 501 Things You Wanted To Know About Switching to OpenOffice.org from Microsoft Office.

To create a form, start with a document in the appropriate application. Long-click the Show Form Functions button on the Main toolbar. (In some cases, you may have to add the button to the toolbar first.) The Form Functions toolbar (Figure 1) appears. The toolbar includes an assortment of controls you can put on your forms, as well as buttons for managing the form as a whole.

As with the other toolbars available through a long click, once you make a selection from the Form Functions toolbar, it closes. However, you can drag the toolbar away from the button that opens it and it stays open until you close it. When designing a form involving a number of controls, this can be a very handy feature.

The items in the first row of the Form Functions toolbar are controls. The second row contains a variety of options for working with forms and controls. Most of them are discussed later in this article.

Figure 1. The Form Functions toolbar lets you add controls to a document to create an input form.

To add a control to a form, click it on the Form Functions toolbar. The cursor changes to crosshairs. Click and drag where you want to place the new control.

Once you choose a control from the Form Functions toolbar, you can continue dropping that type of control. To turn off that control type, either click another control to select it, click the form itself without dragging, click the same control on the Form Functions toolbar, or click the Select button on the Form Functions toolbar.

What controls are available?

OOo offers a wide variety of controls from the general to the specific. Table 1 lists them.

Table 1. The selection of controls available for use in forms is extensive. Controls are shown here in the order in which they appear on the Form Functions toolbar.

Control

Use

Push
Button

Used to execute commands.

Option
Button

Used, with one or more other option buttons, to allow choice among
mutually exclusive options. See Group Box.

Check Box

Used for on/off or yes/no choices.

Label
Field

Used to provide labels for other controls. Doesn't accept input.

Group Box

Used to visually combine several controls, as well as create
groups of option buttons.

Text Box

Used to enter free-form text.

List Box

Offers a list of choices. User can choose one or, in some cases,
several.

Combo Box

Offers a list of choices. User can choose one or enter a new
value.

Image
Button

Used to execute commands; has a picture rather than a text
caption.

Image
Control

Displays a picture on the form.

File
Selection

Allows user to point to a file.

Date
Field

Used to enter a date. Supports up arrow and down arrow to change
portions of the date.

Time
Field

Used to enter a time. Supports up arrow and down arrow to change
portions of the time.

Numerical
Field

Used to enter numbers. Optionally has spinner arrows to change
value with the mouse.

Formatted
Field

Used to enter text applying formatting codes as in Calc.

Currency
Field

Used to enter monetary values. Supports up arrow and down arrow to
change value. Optionally has spinner arrows to change value with
the mouse.

Pattern
Field

Used to enter text, applying a specific formatting pattern.

Table
Control

Used to display data from a table or query in a grid format.

How do I customize controls?

With some controls, you can simply drop them on a form and use them as is. That works for things like a Text Box, Date Field, or Time Field. With others, though, once you add the control, you need to provide additional information. Even for those that work immediately, you may want to customize their appearance or behavior.

To set up a control, click it to select it (green sizing handles appear around the edges), and then choose Control... from the shortcut menu, Format | Control... from the menu, or Control Properties... from the Form Functions toolbar. The Properties dialog for the control appears. The contents of the Properties dialog vary, depending on the type of control.

Just as the contents of the Properties dialog vary with the type of control, so does what you need to do to get it working. Table 2 lists some of the items common to a number of controls.

Table 2. The Properties dialogs for the various controls let you set them up as you want. The properties shown here are common to a number of the controls.

Property

Meaning

Name

Identifies the control. In some cases, including option buttons,
assigning the same name to several controls turns them into a
group.

Label

The caption that appears on the control.

Label
field

Links the control to a Label Field control. Used for controls
(like Text Boxes) that don't have their own label.

Enabled

Determines whether the control is available to the user.

Tabstop

Determines whether the control is part of the tab order, that is,
whether the user can reach it by tabbing from one control to the
next.

Order

Determines the position of the control in the tab order.

Character
set

Specifies the font characteristics for the control.

Default
value

Specifies the initial value displayed in the control.

Background
color

Specifies the background color for the control.

Help text

Specifies the tooltip for the control.

Individual controls have additional properties related to their purpose. For example, a Check Box has a Default Status property that determines whether it's initially checked and a Text Box has a MultiLine input property that determines whether you can put multiple lines of text into the control.

Some controls need certain properties specified in order to be useful. In some cases, AutoPilots are available to help you specify the necessary information. The next few sections consider some of those controls.

How do I create a group of option buttons?

Option buttons (also known as "radio buttons") let you choose one item from among a mutually exclusive group.

There are a couple of ways to set up a group of option buttons, but the easiest is to make sure the AutoPilots On/Off button on the Form Functions toolbar is on. (When it's on, it has a thin border around it.) Next, put a Group Box on the form. When you do so, the AutoPilot Group Element appears to guide you through the creation of an option group. On the first page, specify the options to appear in the group. For each, type it in, and then press Enter or click the >> button. When all the items have been entered, click Next.

On the second page, indicate which item in the list should be initially selected. You can also specify no item be selected. Click Next to go to the third page, which allows you to associate a value with each item. Specifying a value is particularly significant when data on the form is tied to a data source. (See "How do I associate a form with a data source?" below.) To specify the value for an item, click that item in the list, and then type the appropriate value in the text box. By default, character items are associated with numbers starting from 1. Click Next to move to the Database Field page.

The Database Field page lets you automatically save the user's choice to a field in a table. If you prefer, you can specify that the value is available only in the form. Click Next to reach the final page of the AutoPilot. On this page, you specify a name for the option group-the name appears as a caption on the surrounding box. Click Create to exit the AutoPilot and create the option group.

How do I set up a combo box?

Like an option group, a combo box lets a user choose from among a mutually exclusive group of options. However, a combo box usually takes up less space than an option group and also allows the user to type in a value not on the list. Again, the easiest way to configure a combo box is to use the AutoPilot.

Make sure the AutoPilot's On/Off button is on and put a combo box on the form. The AutoPilot Combo Box opens. Combo boxes always draw their contents from a data source. If you haven't associated a data source with the form yet (either by using it for another control or by explicitly doing so -- see "How do I associate a form with a data source?" below), the first page that appears lets you choose a data source and table for the form. Click Next to move to the Table Selection page once you choose a data source.

If the form already has a data source, the AutoPilot opens on the Table Selection page. The data you display in the combo box doesn't have to come from the table the form is linked to, and in fact, often comes from another table that contains look-up information. Click Next once you choose the table that contains the list of items to appear in the combo box.

The Field Selection page lists the fields from the specified table. Choose the one to use in the combo box. The specified field not only provides both the list of items the user sees, but also the value to store based on the user's choice. (With a combo box, there's no way to display data from one field, but store the value from a different field. List boxes do provide that option; see the next section "How do I set up a list box?") Click Next to move to the Database Field page to indicate where to store the user's choice. Click Create to create the combo box.

How do I set up a list box?

A list box has a lot in common with a combo box, so it's not surprising the method for specifying a list box isn't much different from that for a combo box. Like a combo box, the easiest way to set up a list box is with the AutoPilot.

A list box must be tied to data from a data source. It draws data from a table and stores its results in a table. Usually, the data comes from one table (a look-up table) and the results are stored in another table. Unlike a combo box, a list box can store data from a different field than is displayed.

As with a combo box, if no data source is associated with the form, the first page of the AutoPilot lets you choose one. Once you finish and click Next, choose the table that contains the data to appear in the list. After clicking Next, the process continues as with a combo box. On the next page, you specify the field to appear in the list box.

At this point, list boxes part company from combo boxes. When you click Next, you reach the Field Link page of the AutoPilot. On this page, you link a field from the form's data source table to a field from the list box's table. The link indicates that when the user makes a choice from the list box, the value of the specified field of the list box's table is copied to the specified field of the form's table.

When you click Create, the list box is created. By default, it's a drop-down list that looks like a combo box, but doesn't allow the user to type in a new value. To change it to a regular list box, with a number of items displayed at once, choose Control from the list box's shortcut menu or with the list box selected, choose Control Properties... from the Form Functions toolbar or Format | Control from the menu. Change the Drop-down property to No.

Do any other controls have an AutoPilot?

Most of the remaining controls are simple enough to configure that no AutoPilot is needed or provided. However, the Table control is fairly complex. It provides a grid type display of the data in a table, along with navigation controls. The Table Element AutoPilot lets you choose which fields display and in what order.

How do I attach a data source to a form?

As described in the last few sections, using the AutoPilots for some kinds of controls requires you to specify a data source for the form. However, sometimes, you may want to do so even if you're not creating a combo box or list box.

To link a data source to a form, choose Form Properties... from the Form Functions toolbar. Choose the Data tab (Figure 2), and then choose a Data Source from the drop-down list. Once you choose a data source, choose a table from the Content drop-down list. You can also specify a query or write a SQL command directly to provide the form data by changing the Content Type, and then specifying the appropriate Content.

Figure 2. The Data tab of the Form Properties dialog lets you specify the data source for a form.

How do I associate data with a field?

Once the form has a data source, you can link fields to controls. To do so, add the control to the form, and then open its Properties dialog. On the Data tab, choose a field from the Data Field drop-down list.

Be aware that when you link a field to a control in this way, every time you use that control, changes you make are stored in the actual data source.

How do I make something happen in my document?

While editing a data source is one use for forms, you may want to use a form for other things. For example, you might want to add a button to a spreadsheet to perform a calculation or to a presentation to run a video clip.

Each control has a number of events associated with it. An event is something that occurs due to user action. For example, when the mouse is moved, an event fires each time it enters the area of a control and each time it leaves that control's area. Typing a character also fires an event. So does clicking the mouse.

The Events tab of the Properties dialog for each control lists the events available for that control. Figure 3 shows the Events tab for a text box. For each event, you can specify what happens when that event fires. You do so by clicking the ellipsis button next to the event, and then pointing to the OOo macro that performs the desired action.

Figure 3. Every control has a number of events that occur as the user interacts with it. You can associate actions with events.

The form itself has events, as well. To access these, click the Form Properties... button on the Form Functions toolbar and go to the Events tab. As with control events, you can assign a macro to any form event.

How do I use the form?

Once you finish laying out a form, you can "run" it by clicking the Design Mode On/Off button on the Form Functions toolbar. When design mode is off, the controls operate as you would expect -- you can select and clear check boxes, open combo boxes and drop-down lists, and so forth.

Ordinarily, when you open a saved form, it automatically has design mode off. So, a user opening a form you supply sees it as they would expect. (Be aware, however, that a user can turn design mode on and change the form.) If you're working on a form and want it to open in design mode each time, click the Open in Design Mode button on the Form Functions toolbar before saving the form.

When a form linked to a data source has design mode turned off, the database bar appears at the bottom of the form.

OOo doesn't have a mechanism to prevent users from changing forms to Design mode. In Microsoft Office, you can protect a form document and give it a password to prevent users from seeing inside.

Click Here!