Documentation/OOoAuthors User Manual/Writer Guide/Using Forms in Writer

From Apache OpenOffice Wiki
Jump to: navigation, search

This is Chapter 15 of the OpenOffice.org 2.x Writer Guide (Third edition), produced by the OOoAuthors group. A PDF of this chapter is available from the OOoAuthors Guides page at OpenOffice.org.

<< User Manuals page
<< Writer Guide Table of Contents
<< Chapter 14 Working with Fields   |    Chapter 16 Math Objects >>


Introduction

This chapter covers the use of forms within Writer documents. Most of the information here also applies to forms in other OpenOffice.org components, but there are some differences.

The chapter presents information on using forms in four main sections: setting up a basic form, an example for creating a form, linking a form to a data source and finally some advanced techniques.

OpenOffice.org forms cover a lot of ground and not everything is included here. Notable omissions are using forms in HTML documents and writing macros to link to form controls.

When to use forms

A standard text document displays information: a letter, report or brochure, for example. Typically the reader may edit everything or nothing in any way. A form has sections that are not to be edited, and other sections that are designed for the reader to make changes. For example, a questionnaire has an introduction and questions (which do not change) and spaces for the reader to enter answers.

OpenOffice.org offers several ways to fill information into a form, including checkboxes, option buttons, text boxes, pull-down lists and spinners.

Forms are used in three ways:

  • To create a simple document for the recipient to complete, such as a questionnaire sent out to a group of people who fill it in and return it.
  • To link into a database or data source and allow the user to enter information. Someone taking orders might enter the information for each order into a database using a form.
  • To view information held in a database or data source. A librarian might call up information about books.

Using forms to access a database offers a fast and easy way to build up complex graphical front ends. Your form can include not only the fields that link up to the data source but also text, graphics, tables, drawings and other elements.

Alternatives to using forms in Writer

In OpenOffice.org version 1.x, forms were the only way to access a data source. In OOo version 2.x, Base—the database component—provides an alternative way to do this. There are a lot of similarities between forms in Base and Writer, but it may be that one is better for a particular task than the other. Base is appropriate only if the form accesses a data source; you would not use it for simple forms.

Note: Most other OpenOffice.org components—Calc, Impress and Draw—also support forms in almost exactly the same way as Writer does.

Creating a simple form

This section explains how to create a simple form without any links to a data source or database and without advanced customization.

Create a document

There is nothing special to be done when creating a document to use as a form. Create a new Writer document with File > New > Text document.

Form toolbars

Two toolbars control form creation: Form Controls and Form Design. Select View > Toolbars > Form Controls and View > Toolbars > Form Design to show them both. See Form controls reference for a summary of the tools on these toolbars.

Tips: The Form Design toolbar can also be launched from the Form Controls toolbar. Some of the less commonly used controls are on a third toolbar—More Controls—also launched from the Form Controls toolbar.

You can dock these toolbars in different places on the Writer window, or leave them floating.

The three form design toolbars
The three form design toolbars.

The Form Controls toolbar has a button for each of the most commonly used types of control.

Note: In OpenOffice.org form controls are the text boxes, list boxes, option buttons, push buttons and other items that can be placed in forms.

Design Mode

When creating a form, you will want to check that all the parts of the form work correctly. If design mode is off, the form behaves as it would for the end user. Buttons can be pressed, checkboxes checked and list items selected. If design mode is on, clicking on a control item selects it for editing.

Click the Design Mode On/Off button FormDesignOnIcon.png on the Form Controls toolbar to turn design mode on and off.

Tip: If the Design Mode button is not available, click on the Select button SelectIcon.png. This activates most of the tools on the three toolbars.

Insert form controls

  1. To insert a form control into the document, click the control’s icon to select it. The mouse pointer changes to look like this: FormMousePointer.png
  2. Click in the document where you want the control to appear. (You can move it later.)
  3. Holding the left mouse button down, drag the control to size it.
  4. The control button remains active, so you can insert several controls of the same type without needing to go back to the toolbar.
  5. To change to another tool, click its icon on the toolbar.
    To stop inserting controls, click on the Select button SelectIcon.png on the Form Controls toolbar, or click on any of the controls you have just inserted. The mouse pointer changes back to its normal appearance.

Tip: Holding down Shift when creating a form control makes the control square. If you press Shift when resizing an existing control, its proportions are kept the same.

Note: When you insert a group box, list box or combo box, a wizard is launched to guide you through the setup. If you prefer not to run the wizard, click the Wizards On/Off button FormWizardsIcon.png on the Form Controls toolbar.

Configure controls

Having inserted the controls, you need to configure them to look and behave as you want. Right-click on a form control within your document and select Control from the pop-up menu.

The Form Control dialog box has three pages: General, Data and Events. For simple forms, only the General page is of any interest. On this page you can set the look and feel of the control.

Some controls have other useful options:

  • Some controls have visible labels, such as Push Button and Option Button. The label text can be set.
  • The List Box contains a list of options to choose from. Set these in the List entries box.

Tip Double-clicking on a control also brings up the Control Properties dialog box.

Control Properties dialog box
Control Properties dialog box.

Using the form

To use the form, leave design mode by clicking the Design Mode On/Off button FormDesignOffIcon.png.

A typical way to use a form is:

  1. You design the form, then save it when you are happy with it.
  2. You send the form to others (for example, by email).
  3. They fill in the form, save it and send it back to you.
  4. You open the form and see what their answers are.

Tip: By using a data source, or setting a form to update over the web, you can automatically gather data. However, both of those are more complex and you might prefer to keep things simple.

Form controls reference

Form Control toolbar

SelectIcon.png Select Select a form control to perform some other action on it.
FormDesignOffIcon.png Design mode on/off Toggle between design mode on (to edit forms) and design mode off (to use forms).
FormControlIcon.png Control Launch form control properties dialog box. This dialog box can be kept open as different controls are selected.
FormIcon.png Form Launch form properties dialog box, controlling properties for the form as a whole, such as which data source it connects to.
FormCheckbox.png Check Box A box that can be selected or deselected on the form. You can label the box.
FormTextbox.png Text Box Control to create a box into which the form user can type any text.
FormattedField.png Formatted Field Control allowing numeric formatting options. For example, you can set maximum and minimum values for the number entered, or the number type (decimal places, scientific, currency).
FormPushbutton.png Push Button Creates a button that can be linked to a macro. The label is the name that appears on the button.
FormOptionButton.png Option Button Creates an option or radio button. When multiple buttons are grouped together, only one can be selected at a time. The easiest way to group multiple buttons is to use the Group Box button on the More Controls toolbar, with wizards enabled.
FormListbox.png List Box Creates a list of options, as a pull-down menu, that the user can choose from. If wizards are on, creating a list box launches the List Box Wizard. This wizard is only useful if your form is linked to a data source.

If not linked to a data source, turn wizards off and create an empty list box. Then click the Control button and, in the List Entries option on the General tab, enter the options you want to appear on the list.

FormCombobox.png Combo Box As with a List Box, you set up a list of choices. In addition a panel at the top either displays the choice made or allows the form user to type in something else. Otherwise this works the same as the List Box.
FormLabelField.png Label Field A text label. The difference between this and just typing on the page is that, as a control, you can link a label field to macros so, for example, something happens when the mouse passes over it or clicks on it.
FormMoreControls.png More Controls Launch the More Controls toolbar.
FormDesign.png Form Design Launch the Form Design toolbar (which can also be opened with View > Toolbars > Form Design).
FormWizardsIcon.png Wizards On/Off Some form controls (List Box and Combo Box) have optional wizards. If you do not want the wizard to launch when you create one of these controls, use the Wizards On/Off button to switch wizards off.

More Controls toolbar

FormSpinButton.png Spin Button A spin button allows form users to choose a number by cycling through the numbers. Specify maximum, minimum, default and the step between numbers.

This control is not commonly used in Writer, as the number is not displayed. In Calc, however, a Data tab appears on the Control Properties dialog box, allowing you to link the spin button to a cell.

FormScrollbar.png Scrollbar Creates a scrollbar, with a number of options to define the exact appearance.

This control is not commonly used in Writer. In Calc, a Data tab appears on the Control Properties dialog box, allowing you to link the scroll bar to a cell.

FormImageButton.png Image Button Behaves exactly like a push button, but displays as an image. Choose the image in the Graphics option on the General tab in the Control Properties dialog box.
FormImageControl.png Image Control Only useful when the form is connected to a data source and a field in the data source exists that can hold images. You can add new images to the database or retrieve and display images from it.
FormDateField.png Date Field Holds a date. Configure the earliest and latest dates the field will accept, the default date and the date format. You can add a spinner.
FormTimeField.png Time Field Works like a date field, specifying a time.
FormFileSelect.png File Selection Allows a user to select a file, either by typing the path and name directly or by clicking on a Browse button and choosing the file from a dialog box.
NumericField.png Numeric Field Displays a number. Specify formatting, maximum, minimum and default values. You can add a spinner.
CurrencyField.png Currency Field Works like a numeric field; additionally you can add a currency symbol.
PatternField.png Pattern Field Pattern fields are useful when the form links into a data source. Specify an Edit Mask to restrict what a user can enter into the field. Specify a Literal Mask to restrict which data is displayed from the data source.
FormGroupBox.png Group Box The group box control has two different uses depending on whether wizards are on or off.

If wizards are on, creating a group box launches the Group Element wizard. This creates a group of option buttons (in which only one may be selected at a time). In most cases, using a group box is the best way to create a set of option buttons.

If wizards are off, a group box is simply a visual box to group together different controls. It has no effect on the way the controls operate.

FormTableControl.png Table Control Table Control is only useful with a data source. If no data source is specified, you will be prompted to choose one in the Table Element Wizard.

You then pick the fields to display and, when design mode is off, the data appears in the table. The table also includes controls to step through the records.

Records can be added, deleted and modified in the table.

FormNavBar.png Navigation Bar A navigation bar is the same as the Form Navigation toolbar (View > Toolbars > Form Navigation), but can be placed anywhere in the document and be resized.

Form Design toolbar

SelectIcon.png Select Select a form control to perform an action on it.
FormDesignOnIcon.png Design mode on/off Toggle between design mode on (to edit forms) and design mode off (to use forms).
FormControlIcon.png Control Launch form control properties dialog box. This dialog box can be kept open as different controls are selected.
FormIcon.png Form Launch form properties dialog box, controlling properties for the form as a whole, such as which data source it connects to.
FormNavigator.png Form Navigator The Form Navigator is a nice utility, displaying all the forms and controls in the current document, allowing you to edit and delete them easily.

If you use the Form Navigator, it's a good idea to give your controls names (in the properties dialog box). The name appears in the navigator so, for example, if you have ten text boxes, you can tell which is which.

FormAddField.png Add Field Add Field is only useful if you have specified a data source for the form. If no data source is specified, an empty box opens.

If you have specified a data source, Add Field opens a list of all the fields in the specified table, which you can then drag and drop onto the page. The fields are placed on the page with the name of the field before them.

This is a quick and easy way to create a form from a data source.

ActivationOrder.png Activation Order Allows you to specify the order in which focus shifts between controls. You can test the order by leaving design mode and using Tab to switch between the controls.
OpenDesignMode.png Open in Design Mode Open the current form in design mode (to edit the form rather than entering data into it).
FormAutoFocus.png Automatic Control Focus If activated, focus is set to the first form control.
FormPositionSize.png Position and Size Launches the Position and Size dialog box, allowing you to specify both by typing in precise values, rather than dragging the control. You can also lock the size or position, so they do not get changed accidentally. For some controls, you can rotate and set the slant and corner radius.
FormAnchor.png Change Anchor Just as with a frame, any form control can be anchored to page, paragraph or character and also anchored as a character (meaning that it behaves like any other character on the page).
FormAlign.png Alignment The Alignment button is disabled unless the control is anchored as a character. You can align a control in different ways, for example so the top of the control lines up with the top of the text or the bottom lines up with the bottom of the text.
FormGridDisplay.png Display Grid Display a grid of dots on the page, to help you line up controls.
FormSnapGrid.png Snap to Grid When a control is brought close to a grid point or line, it will snap to the grid. This makes it easier to line up controls.
FormGuides.png Guides when Moving When a control is being moved, lines extend from the control horizontally and vertically to help you position it accurately.

Example: a simple form

Create the document

Open a new document (File > New > Text Document). It is a good idea to write down the outline of the document, without form controls, though of course it can easily be changed later.

Document without form controls
Initial document without form controls.

Add form controls

The next step is to add the form controls to the document. We will have four controls:

  • Name is a text box.
  • Sex is two option buttons, male or female.
  • Favourite shape is a list of options.
  • All shapes you like is a series of check boxes.

To add these controls:

  1. Select View > Toolbars > Form Controls to open the Form Control toolbar.
  2. If the tools are not active, click the Form Navigator button (on the Form Control toolbar) to activate them. If necessary, also click the Design Mode On/Off button.
  3. Click the Text Box button FormTextbox.png, then click in the document and, with the left mouse button held down, drag the shape of the Name text box to approximately the size you want.
  4. Make sure the Wizards On/Off button FormWizardsIcon.png is on (shaded with a border). Click the More Controls button FormMoreControls.png to launch the More Controls toolbar.
  5. On the More Controls toolbar, click the Group Box button FormGroupBox.png. Draw a group box by the Sex: entry to launch the Group Element Wizard.
  6. On the first page of the wizard, enter two names for the options fields: Male and Female. Click Next>> three times, delete the caption and click Finish.
  7. Now create the list box. On the Form Controls toolbar, click the Wizards On/Off button FormWizardsIcon.png to turn wizards off. Click the List Box button FormListbox.png and draw a list box by Favourite Shape: in the document. This will just be an empty pane for now.
  8. Finally create four check boxes by All the shapes you like. Click on the Check Box button FormCheckbox.png and then draw out four check boxes, side by side across the page.

You should now have a document looking something like this:

Document with form controls
Document with form controls.

Configure form controls

No further configuration is required to the Name and Sex fields (though, of course, there is configuration that could be done if you wanted, such as giving a name to each control and changing the appearance of the controls).

The list box must be configured to add the list of options. The check boxes must be configured to add in the names (instead of Check Box, Check Box1...)

  1. Click on the List Box control within the document and then the Control button [[Image:]] on the Form Design toolbar to launch the control properties dialog box. Select the General tab.
  2. In the List Entries box, enter the names of the shapes: Circle, Triangle, Square and Pentagon. Type in one shape and press Enter. Then position the cursor at the end of the line to type in the next shape. You should end up with a line saying “Circle”;”Triangle”;”Square”;”Pentagon”.
  3. Click on the first Check Box. The Properties dialog box stays open but changes to show the properties for the check box.
  4. Change the Label to Circle and press Enter. The label on the check box will change immediately.
  5. Click on each of the other three check boxes in turn. Change the Label in the Properties dialog box to Triangle, Square and Pentagon in turn.
  6. Close the Properties dialog box.
  7. Turn Design Mode off [[Image:]] and close the two Controls toolbars.

You have now completed the form and your form hopefully looks something like this:

Completed form
Completed form.

Finishing touches

The form is complete, but you are free to make further changes to the document. If you were sending this out to other people to complete, you would probably want to make the document read-only. The effect would be that users would be able to fill in the form, but not to make any other changes to the document.

To make the document read-only, select Tools > Options > OpenOffice.org > Security > Open this document in read-only mode.

Note: If the document is read-only, anyone filling in the form will need to use File > Save as to save the document.

Accessing data sources

The most common use for a form is as the front end of a database. You can provide a form that allows users to enter information into a contacts database and, because it is part of a Writer document, the form can contain all the graphics, formatting, tables and other elements to make it look just the way you want. Even better, modifying the form is as simple as editing a document.

OpenOffice.org can access numerous data sources. These include ODBC, MySQL, Oracle JDBC, spreadsheets and text files. As a general rule, databases can be accessed for read and write; other data sources (such as spreadsheets) are read-only.

Tip: To see the full list of supported data source types, select File > New > Database. Click Connect to an existing database and then click the triangle next to the list box.

Creating a database

Chapter 10 (Getting Started with Base) in the Getting Started guide covers in more detail how to create a database. We will give a short guide here to creating a very simple database with OOo Base.

  1. Select File > New > Database to start the Database Wizard.
  2. Select Create a new database and click Next.
  3. On the next page, select Yes, register the database for me and Open the database for editing. Registering the database just means that it can be accessed from other OOo components such as Writer and Calc. You need to do this if you want to link your forms into it.
  4. Click Finish and save your new database, giving it a name. Unlike creating other documents in OOo, databases must be saved when you first create them.

Database Wizard
Database Wizard.

After saving the database, you should see the main Base window, which contains three panels. The left-hand panel is Database, with icons for Tables, Queries, Forms and Reports.

Main Base window
Main Base window.

The next step is to create a table. Again, this is covered in more detail in Chapter 10 (Getting Started with Base) in the Getting Started guide. Here we are going to create a small table as an example.

  1. Choose Tables in the left-hand column, then choose Create Table in Design View under Tasks.
  2. Use the Table Design window to tell Base which fields to create. We will have just three fields: Name, Address and Telephone.
  3. Database table design
    Database table design.

  4. On the first line, enter under Field Name ID and set the Field Type to Integer [INTEGER]. In the gray box at the left of the line, right-click and select Primary Key, bringing up a key icon in the box. In the Field Properties at the bottom of the window is an Auto Value option; change this to Yes.
  5. Tip: Setting up the Primary Key field with Auto Value set to Yes is an important step. If this is not done, the form you create later will be much trickier to use and may generate errors for the user. Make sure you get this step right!

  6. On three lines, enter under Field Name Name, Address and Telephone. Accept the default Field Type of Text [VARCHAR] and leave Description blank.
  7. Save the table (File > Save). You will be prompted to name it (the name can be anything you like).
  8. Finally, save the whole database from the main Base window (File > Save).

Accessing an existing data source

If you have an existing data source, such as a spreadsheet or database, you simply need to tell OpenOffice.org about it. This is called registering a data source.

To register an existing data source:

  1. Select File > New > Database to launch the Database Wizard.
  2. Database Wizard
    Using the Database Wizard to connect to an existing database

  3. Select Connect to an existing database and choose the type from the drop-down list.
  4. Click Next and follow the instructions to select the database to register (the exact process varies between different types of data source).
  5. In Step 3: Save and proceed, check that Yes, register the database for me is selected. Deselect Open the database for editing – you just need to register it, not edit it through Base.

Creating a form for data entry

Whether you created a new database, or already had a data source, it must be registered with OpenOffice.org (see above). Once it is registered, linking your form to the data source is simple. Follow these steps to create a new form and link it to a registered data source.

  1. Create a new document in Writer (File > New > Text Document).
  2. Design your form, without putting in the actual fields (you can always change it later).
  3. Show the Form Controls toolbar (View > Toolbars > Form Controls).
  4. Click the Design Mode On/Off button FormDesignOffIcon.png to put the document into design mode, if necessary. With design mode off, most of the toolbar buttons are greyed out. If the Design Mode button is also grayed out, click on the Select button to activate it.
  5. Click the Text Box button FormTextbox.png. Click in the document and, holding down the left mouse button, drag the mouse to create a text box for the first form field (for example, Name, if you are linking to the database created above).
  6. Click the Text Box button FormTextbox.png again and drag the mouse to draw another field. Additional fields, of any type, can be added in the same way (click and drag).

So far you have followed the same steps you used before when you created your first form. Now you link your form with the data source you registered.

  1. Launch the Form Properties dialog box. You can do this by clicking on the Form button FormIcon.png in the Form Controls toolbar or by right-clicking on any of the fields you inserted and selecting Form. The form button is grayed out unless you have one of the form elements selected. As soon as you select a form element in your document, the Form button becomes clickable.
  2. In the Form Properties dialog box, click on the Data tab.
    • Set Data Source to be the data source you registered.
    • Set Content Type to be Table.
    • Set Content to be the name of the table you want to access.
    • Close the dialog box.

    Form properties
    Form properties, connecting to a data source.

  3. For each form control in turn, launch the Properties dialog box. Click on the control to select it (so small green boxes appear around it). Then either right-click and select Control or click on the Control button FormControlIcon.png on the Form Controls toolbar.
  4. In the Properties dialog box, click on the Data tab. If you set up the form correctly, the Data Field option will contain a list of the different fields in the data source (for example, Name, Address and Telephone). Select the field you want.
  5. Form control properties
    Form control properties, Data tab.

  6. Repeat for each control in turn until every control that should be has been assigned to a field.

Tip: If you created a database in OOo Base and your Primary Key field had Auto Value set to Yes, that field does not need to be part of the form. If Auto Value was set to No, you will have to include it and have your users enter an unique value into that field whenever they make a new entry—not something you want.

Entering data into a form

Once you have created a form and tied it to a database, you want to use it to enter data into your data source, or modify data already there.

  1. Make sure that the form is not in design mode. In the Form Controls toolbar, click on the Design Mode On/Off button FormDesignOnIcon.png. If design mode is off, most of the buttons on the toolbar will be grayed out.
  2. Make sure that the Form Navigation toolbar is on (View > Toolbars > Form Navigation). This toolbar normally appears at the bottom of the window.
    Form Navigation toolbar
    Form Navigation toolbar.
  3. If there is existing data in the data source, use the control buttons on the Form Navigation toolbar to look at different records. You can amend data in a record by editing the values in the form. To submit the changes, press the Enter key with the cursor in the last field. The record is saved and the next record is displayed.
  4. If there is no data in the form, you can enter it. To submit the new record, press the Enter key with the cursor in the last field.
  5. Other functions can be performed from the Form Navigation toolbar, including deleting a record and adding a new record.

Advanced form customization

Linking a macro to a form control

You can set any form control (for example, text box or button) to perform an action when triggered by some event. To see the full list of events, right-click on the form control when the design mode is on, select Control and click on the Events tab.

To assign a macro to an event:

  1. Create the macro. (See Chapter 17, “Getting Started with Macros” in the Getting Started guide.)
  2. Right-click on the form control, select Control and click on the Events tab.
  3. Control properties events tab
    Control properties events tab.

  4. Click the ... button to the right of the event to bring up the Assign action dialog box.
  5. Assign action dialog box
    Assign action dialog box.

  6. Click Assign and select the macro.

Macros can also be assigned to events relating to the form as a whole. To assign these, right-click on a form control in the document, select Form and click on the Events tab.

Read-only documents

Having created your form, you want whoever is using it to be able to access the information stored in the database, or complete the form, without changing the layout. There is an easy way to do this: make the document read-only.

Select Tools > Options > OpenOffice.org > Security> Open this document in read only mode.

Fine-tuning database access permissions

By default, when a database is accessed from a form, any changes can be made to it: records can be added, deleted and amended. You may not want that behaviour. For example, you may want users to be able only to add new records or to be prohibited from deleting existing records.

In design mode, right-click on a form control and select Form from the pop-up menu. On the Data tab of the Form Properties dialog box are a number of options: Allow additions, Allow deletions, Allow modifications and Add data only. Set each of these to Yes or No to control the access users have to the data source.

Individual fields can also be protected. This might be useful if you wanted a user to be able to modify some parts of a record but only view others, such as a stock list where item descriptions are fixed and quantities can be modified.

To make an individual field read-only, in design mode, right-click on the form control within the document and select Control from the pop-up menu. Select the General tab and set Read-only to Yes.

Form control formatting options

You can customize the way form controls look and behave in a number of ways. These are all accessed in Design mode. Right-click on the form control, select Control from the pop-up menu and select the General tab in the Properties dialog box.

  • Set a label for the control in the Label field. Some form controls, such as push buttons and option buttons, have visible labels that can be set. Others, such as text boxes, do not.
  • Set whether the form control will print out if the document is printed with the Print option.
  • Use the Font setting to set the font, typeface and size for a field.
  • For a text box, you can set the maximum text length. This is very useful when adding records into a database. Every database text field has a maximum length and, if the data entered is too long, OOo displays an error message. By setting the maximum text length of the form control to be the same as that of the database field, this error can be avoided.
  • You can set the default option for a form control. By default, a control is blank, or has every option unselected. You can set the control to start with a particular option or list item selected.
  • For controls where a password is being entered, setting the Password character (for example to *) displays only that character, but saves what the user really types.
  • You can add additional information and help text for a form control.
  • Other formatting controls such as background color, 3-D look, text formatting, scroll bars and borders allow you to further define how the control appears.

XForms

XForms are a new type of web form, developed by the World Wide Web Consortium (W3C). OpenOffice.org 2.x supports the XForms 1.0 open standard for creating web forms with XML.

In OpenOffice.org, an XForms document is a special type of Writer document. XForms use the same controls as the ordinary forms described in this chapter.

After you create and save an XForms document, you can open the document, fill out the form, and submit the changes to a server.

A detailed discussion of XForms is beyond the scope of this chapter.


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools