LibreOffice Logo

Getting Started Guide 24.8

Chapter 8,
Getting Started with Base

Relational databases in LibreOffice

Copyright

This document is Copyright © 2024 by the LibreOffice Documentation Team. Contributors are listed below. You may distribute it and/or modify it under the terms of either the GNU General Public License (https://www.gnu.org/licenses/gpl.html), version 3 or later, or the Creative Commons Attribution License (https://creativecommons.org/licenses/by/4.0/), version 4.0 or later.

All trademarks within this guide belong to their legitimate owners.

Contributors

To this edition

Rob Thornton, B Antonio Fernandez, Olivier Hallot,

To previous editions

Skip Masonsmith, Rob Westein, Dave Barton, Drew Jensen, Dan Lewis, Jean Hollis Weber, Hazel Russman, Kees Kriek, Olivier Hallot, Vasudev Narayaran, JiHui Choi, Pulkit Krishna, Steve Fanning, Magnus Adielsson, Ron Faile Jr., Iain Roberts

Feedback

Please direct any comments or suggestions about this document to the Documentation Team’s forum at https://community.documentfoundation.org/c/documentation/loguides/ (registration is required) or send an email to: loguides@community.documentfoundation.org.

Everything you send to a mailing list or forum, including your email address and any other personal information that is written in the message, is publicly archived and cannot be deleted.

Publication date and software version

Published August 2024. Based on LibreOffice 24.8 Community.
Other versions of LibreOffice may differ in appearance and functionality.

Using LibreOffice on macOS

Some keystrokes and menu items are different in the macOS version of LibreOffice, and the most important differences between macOS and the Window/Linux versions are listed in the table below. For a detailed list, refer to LibreOffice’s online help.

Windows or Linux

macOS equivalent

Result

Tools > Options
menu selection

LibreOffice > Preferences

Access setup options

Right-click

Control+click or right-click depending on computer setup

Open a context menu

Ctrl (Control)

(Command)

Used with other keys

Alt

(Option) or Alt

Used with other keys

F11

⌘+T

Open the Sidebar Styles deck

Introduction

A data source, or database is an organized collection of information that can be accessed or managed by software. When you manage a database in LibreOffice, you will do it through the Base module.

For example, a list of names and addresses could be turned into a database that could help you create a mail merge letter, or a business stock list could be a data source managed through LibreOffice.

Base serves as a database front end that can create and store embedded databases (Figure 1) or connect to external databases (Figure 2).

Figure 1: Local database

Local database

Figure 2: Remote database server

Remote database server

This chapter explains how Base can create databases and reads what is contained in a database, and it also describes how the different parts of the module are used.

LibreOffice uses the terms “Data Source” and “Database” to refer to the same thing, which could be a database such as MySQL or dBase, a spreadsheet or text document holding data, or an address book.

Database components explained

Tables

A database consists of a number of fields that contain the individual pieces of data, which in turn can be organized in records and then into tables. Unlike Calc spreadsheets, database tables cannot have formulas and when creating a table, you also determine the characteristics of each field within it (text, characters, integer numbers, floating point numbers, binary data, etc…)

Forms

Forms are documents created for data entry into the fields of one or more tables which have been associated with the form. For example, when you enter your data in a web service, you are using a form. Forms can also be used for viewing fields from one or more tables associated with the form.

Queries

A query creates a new table from the existing tables. A query extract data from tables given a specific search criteria you define.

Reports

A report organizes the information from the fields of a query or table into a formatted document according to your requirements.

Some features of the Base module, such as  the HSQLDB database engine, and the ability to create reports) need the Java Runtime Environment (JRE). The JRE is not installed in Base by default. Please go to Tools > Options > LibreOffice > Advanced to select a JRE from those installed on your computer.

Type of databases in LibreOffice Base

Base can create both flat and relational databases, and it can create databases where fields have relationships with each other.

Flat databases

A flat database is a simple database system in which each database is represented as a single table in which all of the records are stored as single rows of data. The table is usually stored and physically represented as a simple text file, CSV file or spreadsheet.

Relational databases

A relational database (RDB) is a collective set of multiple data sets organized by tables, records and columns (fields). RDBs establish a well-defined relationship between database tables. Tables communicate and share information, which facilitates data searchability, organization and reporting.Base use Structured Query Language (SQL), a standard user application that provides an easy programming interface for database interaction.

For example: Consider a database that lists a CD music collection. It will contain fields for the names of the artists and a field that holds the CD title. While there is an obvious relationship between the artist and the CD they have created, the CD collection may contain more than one CD by the same artist. These are one-to-many relationships: many artists are linked to more than one CD, so this database contains multiple one-to-many relationships. However, the record company database can contain the names of the artist plus other fields like artist agent. The relationship between artist and agent is one to one: only one agent for each artist.

If you are acquainted with mathematical sets, a relational database can easily be explained in terms of sets: elements, subsets, unions, and intersections. The fields of a database are the elements and the tables are subsets. Relationships within databases are defined in terms of unions and intersections of the subsets (tables).

Planning a database

Databases exist to organize data for easy and accurate retrieval. Database tables are much stricter to define and create, therefore planning in advance is necessary. In this chapter, we will show you how to plan a database. In our example, we will create a database that contains automobile expenses.

The first step in creating a database is to define it by determining what fields are needed. In this case, we will ask a series of questions about the data:

What are the user output requirements?

Information is needed on three broad areas: fuel usage, maintenance, and vacation.

What input data is required for the output?

Fuel purchases, maintenance expenses, and vacation expenses.

What fields fit the fuel purchases area?

Date purchased, odometer reading, fuel cost, fuel quantity, and payment method for it. (Fuel economy need not be included, as it can be calculated using a query.)

What fields fit the maintenance area?

Date of service, odometer reading, type of service, cost of service, and next scheduled service of this type (for example, for oil changes, list when the next oil change should be). A field for notes was added to the list.

What fields fit the vacations area?

Date, odometer reading, fuel (including all the fields of the fuel table), food (including meals and snacks), motel, total tolls, and miscellaneous. These purchases are made by one of two bank cards or cash, so we will need a field to state which payment type was used for each item.

What fields fit into the food category?

Breakfast, lunch, supper, and snacks seem to fit. Should all the snacks be listed individually or list the total cost for snacks for the day? In this case, the snacks are divided into two fields: number of snacks and total cost of snacks, and a payment type is also needed for breakfast, lunch, supper, and total cost of snacks.

What are the fields that are common to more than one area?

Date appears in multiple areas, as does odometer reading and payment type.

How will these fields be used to get the required information?

While on vacation, the expenses for each day will be listed together. These date fields suggest a relationship between the vacation table and the dates in the fuel and food tables, so the date fields in these tables will be linked when the database is created.

These payment type will include two bank cards and cash. So a table will be created with a field for the type of payment and use it in list boxes in the forms.

After the fields for the database tables have been listed, we will need a field for the primary key, which is an identifier that is unique to each record. Some of the tables already have a suitable field for the primary key but an additional field for the primary key must be created for other fields (like payment type).
If a table does not have a primary key field, any data that is added, deleted, or modified must be done with the Execute SQL Statement dialog (Tools > SQL…).

Figure 3: LibreOffice Base window

LibreOffice Base window

Using the Database Wizard to create a new database

To create a new database, you should start with the Database Wizard. The wizard can be started by doing one of the following:

Once the Wizard is started, the first page of the wizard appears (Figure 4). Select the HSQLDB Embedded entry is selected in the Embedded database drop-down menu, select Create a new database, then click Next. (Look at the Base Guide for information about using alternative embedded database engines or existing external databases).

Figure 4: Creating a new database

Creating a new database

Next, the second page of the Database Wizard appears (Figure 5) with two questions. For the first question, make sure the database is registered. And for the second question, click the Open the database for editing checkbox. Click Finish. The Database Wizard will prompt you to select a location to name and save the database.

Figure 5: Registering the new database

Registering the new database

To open and close the Data Sources window that contains the list of registered databases Writer and Calc, select View > Data Sources or press Ctrl+Shift+F4. If a database is not registered, they will not appear in this window. and you cannot access it in Writer or Calc if you need to use data from it.

Save this new database with the name Automobile and the Automobile.odb – LibreOffice Base window appears (Figure 3).

Every time the Automobile database is opened, the Automobile.odb – LibreOffice Base window opens. Changes can then be made to the database. The title for this window is always <database name> – LibreOffice Base.

When you create a database, you should save your work regularly. This means you should save the whole database as well.
For example, when you create your first table, you must save it before you can close it. This makes it part of the database in memory. But only when you save the database file that the table is written to disk.

All database files in Open Document Format are stored with the *.odb extension. Files in that format contain all elements of the database, including forms, reports, tables, and the data itself. The same format can also store a connection to an external database server instead of the local data, for example, to access a MySQL or PostgreSQL database server in your network.

Creating database tables

Database tables store information in a group of things called fields. For example, a table will use fields to hold the data in an address book, a stock list, a phone book, or a price list. A database must have at least one table and will probably have have multiple tables.

Each field can hold data of a single type. For example, an address book that contains phone numbers would contain phone numbers in fields that are the Phone type. Similarly, a price list table might contain two fields: Name and Price. The Name field would contain the names of the items; the Price field would contain the amount of each item.

To work with tables, click the Tables icon in the Database list, select View > Tables, or press Alt+A. The tasks that you can perform on a table are in the Tasks list (see Figure 1).

Using the Table Wizard to create a table

This Base wizard  allows you to create two types of tables: business and personal. Each category contains sample tables from which to choose. Each table has a list of available fields.

The Table Wizard provides sample tables as a starting point to create your own table.

Click Use Wizard to Create Table. This opens the Table Wizard (Figure 7).

Figure 6: Creating a table using Wizard

Creating a table using Wizard

Step 1: Select fields

First, we will use the wizard to generate a table in the Personal category. We will borrow a table from the supplied CD-Collection:

  1. Category: Select Personal and a list of sample tables for personal use appears.

  2. Sample tables: Select CD-Collection and a list of available fields appears in the Selected fields window..

  3. Selected fields: By selecting each field and using the > button, move the following fields from the Available fields list to the Selected fields list: CollectionID, AlbumTitle, Artist, DatePurchased, Format, Notes, and NumberofTracks.

Figure 7: Selecting fields from the Personal category

Selecting fields from the Personal category

Next, we will use the wizard to create a table from the Business category:

  1. Category: Select Business as the Category. Select Employees in the drop-down list of sample tables.

Figure 8: Selecting fields for the table

Selecting fields for the table

  1. Use the > button to move the Photo field from the Available fields list to the Selected fields list. It will be at the bottom of the list, directly below the NumberofTracks field. If you make a mistake in selecting fields, click on the field name in the Selected fields list and use the < button to move it from the Selected fields list back to the Available fields list.

  2. If necessary,  click the field name  and use the up and down arrows on the right side of the Selected fields list to move it to the correct position.

  3. Click Next.

You can double-click on a field to transfer it from from the Available fields list  the Selected fields list or  double-click a field in the Selected fields list to transfer it back to the Available fields list.

Step 2: Set field types and formats

Once the wizard has helped you to select the fields for your database, it will allow you to assign properties to each field. (See Figure 10.) In this example, set each field’s properties as follows:

If any of these fields requires a mandatory entry, set Entry required to Yes. A blank field will then not be allowed. By default, the Entry required property is set to No. (Because AutoValue has been set to Yes. Figure 10 does not display the Entry required field.)

Since Base requires you to specify the maximum length of each field on creation, make sure you make each field as large or maybe larger than necessary. By default, Base uses the VARCHAR format for a text field, so it will only use the number of characters necessary for the field even if it takes up less space than than the limit. So when a field uses VARCHAR, a field that contains 20 characters will only take up 20 characters in the database, even if the limit for the field is set at 100. In contrast, a text field with the CHAR text format has a fixed length and that field will be 100 characters long even if the actual length is 1 or 100.

Each field has a Field type, which must be specified. Types include text, integer, date, and decimal. If the field is going to have general information in it (for example, a name or a description), use text. If the field will always contain a number (for example, a price), the type should be decimal or another numerical field. The wizard picks the right field type, so to get an idea of how this works, see what the wizard has chosen for different fields.

Since up and down arrows are available on this page (not shown in Figure 10), you can use these to reorder the list of fields. Also + and buttons are available and you can use these to remove fields from the list or to add new fields.

When you have finished, click Next.

Step 3: Set primary key

  1. In this example, Create a primary key should be checked.

  2. Next, select option Use an existing field as a primary key.

  3. In the Fieldname drop-down list, select CollectionID.

  4. Select Auto value if it is not already checked. Click Next.

A  primary key field contains a unique number that can be used to identify a specific entry in a database. In this case, CollectionID will contain a number that is assigned automatically by Base to each record of this table.

Step 4: Create table

  1. At this point, the wizard will allow you to rename the table. For this example, make no changes.

  2. Leave the option Insert data immediately checked.

  3. Click Finish to complete the Table Wizard. Close the Table Data View window created by the Table Wizard. You are now back to the main window of the database, but a table named CD-Collection is now listed in the Tables portion of the window.

  4. Click the Save button on the Standard toolbar at the top of the main window.

Creating a table by copying an existing table

If you want to create identical tables for each type of music you have, you can make copies of the original table and name each according to the type of music contained in it.

  1. Click on the Tables icon in the Database area of the LibreOffice Base window to see the existing tables.

  2. Right-click on the CD-Collection table icon. Choose Copy in the context menu.

  3. Right-click, and select Paste. The Copy table dialog opens.

  4. Change the Table name to CD-Jazz, and set Options to Definition and data and click Create.

Figure 9: Creating copies of original table

Creating copies of original table

The action buttons on this page allow you to move selected fields, or all fields, in either direction. You can also double-click to move a field from one list to the other.

  1. Click the Save button on the Standard toolbar at the top of the LibreOffice Base window.

After a table has been created in Base and data has been entered, the field properties cannot be changed. Before you create a table, make sure you have decided the correct names, length, and format for each field.
As a result, deleting a field deletes all the data once contained in that field. Changing the field type of a field after the table has been created can lead to data being lost either partially or completely.
Deleting a table removes all of the data contained in every field of the table. As a result, do not delete a table unless you are certain.

  1. Double-click on the CD-Jazz table icon and visually verify the data before closing the table

  2. Right-click on the CD-Jazz table icon, select Delete and confirm deletion.

Creating Tables in Design View

Once you are familiar with creating a table with the Wizard, use Design View to create a new table. View allows you to directly enter information about each field in a table. We will use this method for the next example (the Automobile database).

While the field types and formatting are different in Design View, the process is similar to the one used in the Table Wizard.

When we create the Automobile database, the first table we will generate is Fuel. Its fields will be FuelID, Date, FuelCost, FuelQuantity, Odometer, and PaymentType.

  1. Find the Tasks area of the LibreOffice Base window and click Create Table in Design View to open LibreOffice’s Base: Table Design dialog.

  2. Enter FuelID into the Field Name field then press Tab key to move to the Field Type column. Select Integer [INTEGER] from its drop-down field.

You can select fields from the Field Type drop-down list by pressing the key for the first letter of the choice. You can cycle through the choices for a given letter by repeatedly pressing that key.

  1. Change the Field Properties in the bottom section of the dialog for FuelID.
    Change AutoValue from No to Yes.

  2. Set FuelID as the primary key for the Automobile database.
    Click in the Field Name cell directly below FuelID. The dialog automatically sets FuelID as the primary key and places a key icon in front of FuelID (Figure 11)

Figure 10: Changing field types

Changing field types

.

Figure 11: Defining the primary key field

Defining the primary key field

When you assign certain integer field types (Integer and BigInt for example) to a field, selecting Yes for AutoValue automatically makes that field the primary key.
Primary keys for any other field type must be selected by right-clicking the rectangle before the field and selecting Primary Key in the context menu.

The primary key serves as a unique ID for each record in a database. You can use any name for this field. We have used FuelID for convenience, so we know to what function it serves.

  1. When you are creating the Date, FuelCost, FuelQuantity, Odometer, and PaymentType fields, do the following:

  1. Type the field name in the Field Name column.

  2. Select the field type:

  1. FuelCost, FuelQuantity, and Odometer also need changes in the Field Properties section (Figure 12).

Figure 12: Changing field properties

Changing field properties

  1. Repeat steps a) through c) until you have entered all of the fields.

Figure 13: Format example options

Format example options

  1. To save and close the table in the Table Design dialog, select File > Save, or click the Save icon on the Standard toolbar, or press Ctrl+S. Name the table Fuel.

  2. Close the Table Design dialog.

  3. In the main database window, select File > Save, click the Save icon on the Standard toolbar, or Ctrl+S to save the database.

Follow the same steps to create the Vacations table. The fields, field types, and descriptions are listed in Figure 14.

  1. Make the following changes in the Field Properties area of the window.

  1. Odometer: As described in step 3)), bullet c)) for the corresponding field of the Fuel table.

  2. Motel, Tolls, Breakfast, Lunch, Supper, SnCost, and Miscellaneous: Format currency the same as step 3)), bullet c)) for the FuelCost field of the Fuel table.

  1. Making Date the primary key in this table has to be done in a different way because this field is typed as Date, not Integer.

  1. Right-click to the left of the field name Date.

  2. Select Primary Key in the context menu.

Following similar steps to those above, you should use Design View to create a much simpler table entitled Payment Type. This table contains two fields, named Type, with the field type set to Text [ VARCHAR ] and PaymentID (field type Integer [ INTEGER ]). Set the PaymentID field as the primary key for the table.

Figure 14: Fields in Vacations table

Fields in Vacations table

Defining relationships

Now that the tables have been created for the Automobile database, what are the relationships between these database tables?

When on vacation, you will want to enter all of your expenses at once each day. Most of these expenses are in the Vacations table, but the fuel we buy is not. So we will link these two tables using the Date fields. Since the Fuel table may have more than one entry per date, this relationship between the Vacations and Fuel tables is one to many (it is designated 1:n).

  1. Make sure you are still in the Automobile database and select Tools > Relationships. The Automobile – LibreOffice Base: Relation Design window opens and the Add Tables dialog pops up. (You can also open it by clicking the Add Tables icon on the Relation Design window if you ever need to do so.)

Figure 15: Designation for a 1:n relationship

Designation for a 1:n relationship

  1. Once the Add Tables dialog is active, use one of two ways to add a table to the Relation Design window:

  1. When you have added the tables you want, click Close to close the Add Tables dialog.

  2. Next, define the relationship between the Vacations and Fuel tables by clicking the New Relation icon on the Standard toolbar or selecting the Insert > New Relation command. Either command opens the Relations dialog (Figure 16). Our two tables are listed in the Tables Involved section.

Figure 16: Selected fields in a relationship

Selected fields in a relationship

  1. In the Fields Involved section, click the drop-down list under the Fuel label.

  2. Select Date in the list.

  3. Click in the cell to the right of this drop-down list. This opens a drop-down list for the Vacations table.

  4. Select Date in the list. It should now look like Figure 16.

  5. Modify the Update Options and Delete Options section of the Relations dialog (Figure 17).

Figure 17: Update Options and Delete Options section

Update Options and Delete Options section

  1. Click OK.

  2. In the Relation Design dialog, click the Save icon or select File > Save.

  3. Close the Relation Design dialog.

  4. Click the Save button on the Standard toolbar at the top of the main database window.

The primary key can contain more than one field. Its foreign key (– A field in a table that stores values of the primary key of records in another table). will contain the same number of fields.) If this were the case in Figure 16, the other fields of the primary field for the Fuel table would be listed under Date. The corresponding fields of the foreign key would be listed under Vacations. Detailed information about this is in the Base Guide.

While these options are not strictly necessary, they do help. Having them selected permits you to update a table that has a relationship defined with another table which has been modified. It also permits you to delete a field from the table without causing inconsistencies.

Creating a database form

Once you have created a database to store data, you will need a form. In the language of databases, a form is a front end for data entry and editing.

A simple form should include all of the fields from a table (Figure 18). More complex forms can contain much more, including additional text, graphics, selection boxes, and many other elements. Figure 19 is made from the same table with a text label (Fuel Purchases), a list box placed in PaymentType, and a graphic background.

A list box is useful when a field contains a fixed number of options. It saves you from having to type data by hand and having to double-check that the entered data is correct, and ensures that invalid options are not entered.

Figure 18: Fields of a simple form

Fields of a simple form

Figure 19: Simple form with additions

Simple form with additions

In our database, payments for food or fuel might be made from one of two credit cards (Dan or Kevin) or in cash, so these would be the available options for all boxes that contain payments.

To create a list box as in Figure 19, we first need to create a small, separate table containing the name of the options. Then use the contents of the table to fill the list box. See Modifying a form below

Creating a form with the Form Wizard

We will use the Form Wizard to create a Vacations form, which will contain a form and a subform.

In the main database window (Figure 3), activate the Form Wizard by clicking the Forms icon in the Database area, selecting View > Forms, or pressing Alt+M. You can also click Use Wizard to Create Form on the Tasks list to open the Form Wizard (Figure 20). When the Form Wizard is activated, it displays the Database Form window and the Form Wizard dialog.

Figure 20: Selecting fields in the Form Wizard

Selecting fields in the Form Wizard

Step 1: Select fields

  1. On the Form Wizard dialog, under Tables or queries drop-down, select Table: Vacations. The Available fields list contains the fields for the Vacations table.

  2. Move all of these fields to the Fields in the form list by selecting each field and clicking the  >> button. Click Next.

Using the four action buttons in the center of this page, you can move selected fields, or all fields, in either direction. You can also double-click to move a field from one list to the other. The up and down arrows on the right can be used to re-order entries in the Fields in the form list.

Step 2: Set up a subform

Since we have already created a relationship between the Fuel and Vacations tables, we will use that relationship. If no relationship had been defined, you would have to do it later in the wizard (Step 4: Get joined fields).

  1. Click the box labeled Add Subform.

  2. Click the Subform based upon existing relation radio buttons.

  3. Fuel is listed as a relation we want to add. So click Fuel to highlight it, as in Figure 21. Click Next.

Figure 21: Adding a subform

Adding a subform

Step 3: Add subform fields

This step is similar to Step 1: Select fields, but not all the fields will be used in the subform.

  1. Fuel is preselected under Tables or queries.

  2. Use the >> button to move all the fields to the Fields in the form list.

  3. Click the FuelID field to highlight it.

  4. Use the < button to move the FuelID back to the Available fields list (Figure 22).

  5. Click Next.

Figure 22: Selecting fields for a subform

Selecting fields for a subform

Step 4: Get joined fields

This step is for tables or queries for which no relationship has been defined. Because the relationship has been defined in this example, the wizard will skip this step.

Relationships between two tables can be based upon more than one pair of fields. For more information on this, consult the Base Guide.

When creating a relationship between fields from two tables, those fields have to have the same field type. That is why we used the Date field from both tables: both their field types are Date [ DATE ].
Whether a single pair of fields from two tables are chosen as the relationship, or two or more pairs are chosen, certain requirements must be met for the form to work.

Step 5: Arrange controls

A form’s control consists of two parts: label and field. In this step, you determine where a control’s label and field are placed relative to each other. From left to right, you can choose Columnar – Labels Left, Columnar – Labels on Top, As Data Sheet, and In Blocks – Labels Above.

  1. In this example, we will arrange the main form by clicking the second icon (Columnar – Labels on Top). The labels will be placed above the related field.

  2. Next, we will arrange the subform by clicking the third icon (As Data Sheet). The labels are column headings and the field entries are in spreadsheet format. Click Next.

Figure 23: Arrange controls

Arrange controls

Step 6: Set data entry

In this example, accept the default settings and click Next.

Step 7: Apply styles

  1. Select the color you want in the Apply styles list. In this example, use the Beige option.

  2. Select the Field border you want. In this example, use the 3D look option. You might want to try different settings.

  3. Click Next.

Step 8: Set name

  1. Enter the name for the form. Use Vacations.

  2. Click Modify the form.

  3. Click Finish. The form opens in edit mode, Base will display the Fuel – LibreOffice Base: Database Form window.

Modifying a form

When a form is generated with the Wizard (Figure 24), it usually needs to be reformatted. Figure 24 is not organized at all. Every control seems to have a different size and the color needs to be changed.

Figure 24: Basic form created using Wizard

Basic form created using Wizard

These controls are different sizes because they contain different types of field data. Since Base has three types of controls (text, formatted, and date), these fields are formatted differently. The width of text controls is designed to display a large number of characters, while formatted controls  are shorter because they will usually contain numbers or decimals. Date controls are fairly short, except for the date formats is day of the week, month, day, year (Thursday, October 31, 2020), which requires a larger width than 10/31/2020 or 31/10/2020.

First, we will modify the payment type controls. These fields need to be replaced with a list box, and each list box will be related to an existing field in the table, Payment Type.

The following steps assume that the PaymentType table already contains three single-field records. These might be Cash, Dan, and Kevin. If necessary, you could use the Form Wizard to create a very simple form to input or edit these values.

Next, we will carry out the following steps:

  1. Replace fields with list boxes.

  2. Modify text controls (label field first and then the data field).

  3. Modify the date control.

  4. Modify formatted controls.

  5. Modify fields in the table.

  6. Group the controls (food, fuel subform, and miscellaneous).

  7. Modify the memo control.

  8. Add headings (for each group in the form).

  9. Change the background to a picture, then modify some of the labels so that they can be read clearly against this background. Change the font color of the headings.

  10. Change tab order of fields.Figure 25 shows a section of the Form Design toolbar and key icons are identified that will be used during the following steps. Figure 33 shows the Form Navigator.

Figure 25: Part of Form Design toolbar

Part of Form Design toolbar

  1. Design mode

  1. Control Properties

  1. Form Properties

  1. Form Navigator

Figure 26: Fuel economy calculation field

Fuel economy calculation field

Figure 27: Selecting fields for grouping data

Selecting fields for grouping data

Figure 28: Report Builder template determined by the Report Wizard

Report Builder template determined by the Report Wizard

Figure 29: Report without modifications

Report without modifications

Figure 30: Data Sources navigation buttons

Data Sources navigation buttons

Figure 31: Insert Database Columns dialog

Insert Database Columns dialog

Figure 32: Navigation arrows of a form

Navigation arrows of a form

Figure 33: Form Navigator

Form Navigator

Step 1): Replace fields with list boxes

  1. Open the Control Properties and Form Navigator dialogs and click the Control Properties and Form Navigator icons. (You may need to select a control to make the Control Properties icon available)

  2. Open the Form Navigator and click the first text box whose name contains the word “Payment”.

  3. If both the label and text box components of the field are highlighted in the Form Navigator, double-click the text box so that only it is highlighted.

If the Form Navigator does not have the input focus, selecting a field highlights both field components (for example, label and text). If this arises, then double-clicking the required component will highlight it, and enable you to subsequently move from one component to another with a single click.

  1. Right-click the text box in the Form Navigator, right-click to bring up the context menu, then select Replace with > List Box.

  2. In the Properties dialog, the heading will become Properties: List Box and the General tab is selected. Scroll down to the Dropdown property and change No to Yes.

  3. Select the Data tab.

  4. Go to Type of list contents. Change Valuelist to Sql.

  5. The line below this becomes List content. Click the ellipsis (…) to the right of this property to open the SQL Command – LibreOffice Base: Query Design window and the Add Table or Query dialog.

  6. The PaymentType table has the required entries. Click it, and then click the Add button.

  7. Close the Add Table or Query dialog.

  8. In the SQL Command – LibreOffice Base: Query Design window, double-click Type in the table named PaymentType. This places Type in the Field row of the tabular area.

Figure 34: Selecting PaymentType

Selecting PaymentType

  1. Save and close this window. The SQL code appears in the List content property. (SELECT "Type" FROM "PaymentType")

  2. Change the Bound field value from 1 to 0.

  3. Repeat these steps for each field containing the word “Payment”, six fields in total. (See Note below before doing this.)

There are times, as in this example, when 8)) through12) ) produces the same SQL code. When this happens, copy the code. Paste the code into the Type of list contents property before doing 13)). It is a nice shortcut.

Step 2): Modifying text controls

  1. In this step, you will modify the text labels and fields to make them more meaningful.

  1. Locate the topmost text control in the Form Navigator.

  2. Select the associated control label (begins with lbl). The title of the Properties dialog should have become Label Field.

  3. Click the General tab if it is not already selected.

  4. Click the down-arrow at the right end of the Label property to open the drop-down. Only then rename the label to Payment Type and press Enter.

  5. Click on another property to save this change.

  6. Adjust the width of the label field if necessary for the new width of the label name.

  7. Repeat a)) through f)) for each label field in the form that contains the word Payment.

Base allows you to paste the title of a control in multiple control fields. Once the control title Payment Type has been entered for the first time, it can be copied.  For the next label, highlight the current label name, paste the name you want, and click another property.

  1. Resize the fields.

  1. Make sure that you are in the edit mode and have opened the Form Navigator and Properties dialogs.

  2. In the Form Navigator dialog, click the field whose size you want to change.

  3. Scroll down to the Width property in the Properties dialog.

  4. Estimate the width required based on the set of possible values for this field and change the Width property to that value. In the case of our Payment Type fields, the text box should be wide enough for the longest possible payment type value of “Kevin”.

Figure 35: Modifying listbox properties

Modifying listbox properties

  1. Click either the PositionY or the Height property to save the change. Also, clicking on any other property will do the same thing.

  2. Use these same steps to change the size for each of the remaining text controls, including txtSnackNo and all the payment type fields.

Step 3): Modify date control

  1. With the Properties and Form Navigator dialogs open, select the datDate field in the Form Navigator dialog. The heading of the Properties dialog changes to Properties: Date Field.

  2. In the General tab of the Properties dialog, scroll down to the Date format drop-down list, and select the date format you desire.

  3. Scroll down to the Dropdown property and select Yes.

  4. Is the width of the Date field inappropriate for the selected format? If so:

  1. The Date field has a border around it.

  2. Move the cursor over the middle green marker on the right side of the Date field. It should become a horizontal double arrow. Drag this to the right to increase the width. Drag it to the left to reduce the width. (It may take several attempts to get this right.)

Step 4): Modify formatted controls

Next, make a list of the formatted fields and their labels in the Form Navigator. The fields have the fmt prefix. (There are eight of them.)

One of these fields, Odometer, contains the odometer readings as integers. The rest are currency fields.

Perform the following tasks with each field:

  1. Revise the label of the control so it has the proper annotation. In the case of lblSnCost, change it to Snack Cost.

  2. Click the label of the control to be modified in the Form Navigator. If the border for the label is longer or shorter than the text in the label, adjust its width as described for the Date field above.

  3. Click this formatted field in the Form Navigator list.

  1. Under the General tab, scroll down to the Formatting property.

  2. Click the ellipsis (…) on the right to open the Format Number dialog.

  3. For the Odometer field click Number in the Category list, otherwise click Currency.

  4. Under Format, select a field.

  5. Select the desired number of decimal places (one for Odometer, two otherwise).

  6. Select the desired number of leading zeros.

  7. Negative numbers red: check if yes, remove the check if no.

  8. Thousands separator: check if yes, remove check if no.

  9. Click OK.

  10. If the width of the field is not correct for the selected format, adjust its width as described for the Date field above.

  1. Repeat these steps for each field name in the Form Navigator that begins with fmt.

Step 5): Modify fields in the table

The labels in the controls are in the column headings and the values of the field are in the column. The Form Navigator does not show any information about them. So modifications must be done using the Properties dialog.

The controls in the table control must be modified. To change things, access that particular control’s properties by clicking the header.

  1. Click the Date label (select the column named Date).

  1. Scroll to the Date format property and select the format you desire from the drop-down list, if that format differs from the default setting.

  2. Scroll to the Width property. Adjust the width if necessary.

  3. Scroll to the Dropdown property. If you want to have a small calendar pop-up window, select Yes.

  1. Click the FuelCost label.

  1. Open the drop-down list for the Label property. Add a space between Fuel and Cost then press Enter.

  2. Scroll to the Formatting property. Click the ellipsis (…) on the right. Select Currency from the Category column. Uncheck the Negative numbers red and the Thousands separator. Click OK.

  3. Scroll to the Width property. Adjust the width if necessary.

  4. Change the Alignment property if you do not want data to be to the left. Select Center to put the data in the middle of the cell.

  1. Click the FuelQuantity label.

  1. Open the drop-down list for the Label property. Add a space between Fuel and Quantity. Press Enter.

  2. Scroll to the Formatting property and change to show three decimal places if needed.

  3. Scroll to the Width property. Adjust the width if necessary.

  4. Change the Alignment property if you do not want data to be to the left. Select Center to put the data in the middle of the cell.

  1. Click the Odometer label.

  1. Scroll to the Formatting property. If you wish to show one decimal place, make the change.

  2. Scroll to the Width property. Adjust the width if necessary.

  3. If you do not want data to be to the left, change the Alignment property and if you want to put the data in the middle of the cell, select Center.

  1. Click the PaymentType label.

  1. Right-click this label, then add a list box in a context menu by selecting Replace with > List Box.

  2. Open the drop-down list for the Label property. Add a space between Payment and Type. Press Enter.

  3. On the Data tab, change the Type of list contents property to Sql.

  4. Paste the SQL code SELECT "Type" FROM "PaymentType" into the List content property. (This is a shortcut method – see the Tip on page 1 and the detailed instructions above it if you want more details).

Step 6): Group the controls:

This is a suggested layout which looks reasonably well organized. Use this one or create one that appeals to you. The controls may be wider or narrower than yours because of the content that will be in them. Note that this layout has left space for the additional labels that will be added later.

  1. Three icons need to be activated on the Form Design toolbar: Display Grid, Snap to Grid, and Helplines While Moving. If this toolbar is on the side of the form, click the double arrow (») to open a sub toolbar showing them. If the toolbar is on the top or bottom of the form, they are on the right end of it. If there is a double arrow at the end, click it to reveal them. Make sure they are highlighted as in this figure.

Figure 36: Help for moving controls

Help for moving controls

  1. Some of the controls need to be moved in pairs so the Form Navigator needs to remain open. Close the Properties dialog now.

You can also control the Display Grid, Snap to Grid, and Helplines While Moving options using the View > Grid and Helplines menu.

When moving a control, do not use the PositionX and PositionY properties to place the control, because this will place the control’s label and field on top of each other. Those properties can move a label or field to a specific location, but are not designed to move a control. Use the Position and Size icon on the Form Design toolbar to move a control or group of selected controls.

The list in the Form Navigator reveals which controls needed to be grouped based upon their names:

That is six groups of controls; each one will be moved separately.

The layout shows where each control of a group goes for each of these groups. For most of them, the first control holds the cost, and the second contains the payment type in a row. The Snacks places the number of snacks followed by the cost and then the payment type. The Miscellaneous controls contains the payment below the cost.

  1. Before you move an entire group to a new location, move the controls within the group to where you want them to be relative to each other.

  1. Click on the first control, placing a border around it.

  2. Place the mouse pointer over the label or field where it changes its shape.

  3. Drag it to an open area of the form.

  4. Using the same method, move the second control to the correct position relative to the first control.

  5. As soon as you begin to move it, the helplines will appear. Use them to line up the edges of the controls before releasing the mouse button.

  6. If there is a third control, move it to the correct position relative to the second one.

  1. Move the controls for the group to where you want them:

  1. Click the first component in the Form Navigator list.

  2. Use the standard Control+Click and Shift+Click mechanisms until all of the labels and fields in this group are highlighted.

  3. Place the mouse pointer over one of the labels or fields changing the arrow.

  4. Drag the group to where you want its controls to be.

  1. Repeat for the other controls using these same steps. If a group is going to be below another group of controls, use the helplines to line up the left end of the two groups (for example: first control under left control).

Figure 37: Form suggested layout

Form suggested layout

Step 7): Modify memo control

The size of the Note control allows it to be moved to a place that permits the controls of the form to be organized without changing the size of the Note control. If the size needed modification, this is done using the same methods as for other text controls.

The other modification is adding scrollbars. The choices are none, vertical, horizontal, or both.

  1. To change the size of this control, follow the same steps for changing the size for any text control.

  2. If necessary, move the control to the required position using the same steps described above for grouping the controls (Step 6)).

  3. Click the Control Properties icon on the Form Design toolbar if the Properties dialog is not open.

  4. Select the txtNotes control in the Form Navigator dialog.

  5. On the Properties dialog, click the General tab if it is not selected.

  6. Scroll down to the Text type property.

  7. Select the Multi-line option in its drop-down list.

  8. Scroll down to the Scrollbars property.

  9. In its drop-down list, select the option you need (None, Vertical, Horizontal, or Both).

  10. Close the Properties dialog.

Figure 38: Scroll bar selections in the Properties dialog

Scroll bar selections in the Properties dialog

Step 8): Add headings

This task is easier to complete if you have end-of-paragraph markers visible. You should turn them on by selecting View > Formatting Marks, clicking the Toggle Formatting Marks icon on the Standard toolbar, or by pressing Ctrl+F10.

  1. Make sure that the cursor is in the top left hand corner of the Database Form window.

  2. Move the cursor with the Enter key until it aligns with the gap between the Date field and the Breakfast field.

  3. Select the Heading 2 style from the Set Paragraph Style drop-down list.

  4. Place the input cursor where the first heading should be. Enter the heading Meals.

  5. Move the input cursor with the space bar to the location where the second heading will appear. Type the heading Snacks.

  6. Press the Enter key repeatedly to move the cursor down until it aligns with the gap between the Supper field and the table.

  7. If the style has changed, use the Set Paragraph Style drop-down list on the Formatting toolbar to reselect the Heading 2 style.

  8. Move the input cursor  to the position where you want the table heading to appear. Type the heading Fuel Data.

The Styles deck can be opened by selecting View > Styles or by pressing F11. Also, you can quickly modify the appearance of all three headings by right-clicking the Heading 2 style and selecting the Modify option in the context menu would allow. See the Writer Guide for details.

Step 9): Change the background

The background for a form can be a color or an image. Select a color from the color palette at Tools > Options > LibreOffice > Application Colors or create a custom color from the RGB fields.

To add a color to the form background:

  1. Right-click the form and select Page Style in the context menu.

  2. The Page Style dialog (Figure 39) will appear. Make sure the Area tab is selected.

Figure 39: Page Style dialog

Page Style dialog

  1. Select Color in the row of buttons near the top of the tab.

  2. Select a color from the grid. If you wish to access more choices, choose a palette from the Palette drop-down menu or use the RGB or Hex boxes to create a specific color or click Pick for even more choices.

  3. Click Apply to see what your selection will look like in your form.

  4. Change if necessary.

  5. Click OK to accept your color choice and close the dialog.

To add other form backgrounds:

  1. Perform steps 1) and 2) from the previous numbered

  2. Select the type of background from the row of buttons: None, Gradient, Bitmap, Pattern, or Hatch.

  3. In the selected type, choose one of the selections provided or create your own. For details, see Chapter 4, Changing Object Attributes, in the Draw Guide.

  4. Click Apply to see what your selection will look like in your form.

  5. Click OK to accept your choice and close the dialog.

If you selected a bitmap in the list, the form might look like Figure 40.

Step Error: Reference source not found: Change the tab order

The Tab key moves the cursor from field to field. This method permits us to group our expenses into areas before we begin entering data. For example, all of our meal receipts can be grouped together as can our snacks and also our fuel purchases.

Figure 40: Finished form

Finished form

  1. Select View > Toolbar > Form Design to open the Form Design toolbar.

  2. Click the Activation Order icon (circled in Figure 41).

Figure 41: Form Design toolbar with Activation Order icon circled

Form Design toolbar with Activation Order icon circled

  1. Rearrange the field order in the Tab Order dialog.

  1. If you click the Automatic Sort button, Base will automatically generate a tab order.

  2. You can use the Move Up and Move Down buttons to change the position of a selected control in the list.

  3. If necessary, drag a control from one position to another in the Controls list.

  4. Define a tab order. If you like, you can put the fields in the same order as Figure 42.

  5. Click OK.

  1. Save and close the form.

  2. Save the database.

Figure 42: Possible tab order for the Vacations form

Possible tab order for the Vacations form

Entering data in a form

Different types of fields allow different methods of data entry. In many cases, more than one data entry method can be used.

First, open the form from the main database window (Figure 3).

  1. Click the Forms icon in the Database list, or select View > Database, or press Alt+M.

  2. Find the form’s name in the Forms list (Vacations).

  3. Double-click the form’s name.

The quickest way to enter a date in the Date field is to click on the arrow that opens the drop-down calendar (Figure 43) and select the day that you want. Then press the Tab key to go to the next field.

Figure 43: Calendar drop down

Calendar drop down

The Odometer, Tolls, and Motel fields are numeric fields. Enter values directly into them, or use the up and down arrows (if you chose to display to displace the arrows when the form was created). After you have entered a value in a field, use the Tab key to go to the next field.

The Motel’s Payment Type field is a drop-down list. If the elements of the list start with different letters, you move to a field by entering the first letter of the field’s title.

The form’s remaining fields are either numeric fields or drop-down lists until you reach the Notes field (which is a text field). Type anything you desire in this field just as you would any simple text editor.

The Tab key is used to move between fields. However, there are some limitations where other keys.
Since text fields do not have tab positions, they can not move from one position in the field to another one. All spacing must be done by the space bar.
The Enter key can be used to move between non-text fields, but the function of the Enter key in text fields depends upon whether the text field has a single line property. If it does, pressing Enter will move the cursor to the next field. But if the text field has been assigned a multi-line property, pressing Enter moved will only move the cursor down one line. With this field, the Tab key has to be used to move the cursor to the next field.
If there was no subform for fuel data, pressing the Tab key in the last field would save all of the fields, clear them, and make the form ready to accept data for the next record.

Because we have a subform, the Tab key has a different function. It places the cursor in the first Date field of the subform with the date automatically entered to match the Date field of the main form.

The Fuel Cost, Fuel Quantity, and Odometer fields are numeric fields. The Payment Type field is a drop-down list. Enter the data just as you did in the main form, and use the Tab key to go to the next field.

When you use the Tab key to leave the Payment Type field, it goes to the Date field of the next line and automatically enters the date. Now you can enter your second set of fuel data for this day.

When the form has a subform, click any of the fields of the main form to move to another record.  In this case, click the Date field of the main form. Then use the directional arrows at the bottom. Move from left to right: First Record, Previous Record, Next Record, and Last Record (all have keyboard shortcuts identified in their extended tips). To the right of these arrows is the New Record icon.

To create a new record while in the last record of the main form, click either the Next Record icon or the New Record icon.

Each record is numbered and the Record box contains the number of the record whose data is shown in the form.
If you need to go to a specific record, enter the record’s number into the record box and type Enter to take you to that record.Figure 44 is a record with data inserted in its fields.

Figure 44: Sample record of the Vacations form and subform

Sample record of the Vacations form and subform

Quickly populate a table with data from a spreadsheet

You can drag and drop a spreadsheet that uses the format for a database table into a database table by doing the following:

  1. Open the database file in the LibreOffice Base window (Figure 3) and select the Table view.

  2. Open the spreadsheet in Calc. Use a sheet that is formatted the same as the database table.

  3. Place the two windows side by side in the desktop.

  4. Drag the sheet tab at the bottom of the Calc window into the table list of the database file. The mouse pointer shows a square with a + sign. (Sheet tab: This has the name of the sheet on it.)

  5. Drop the sheet by releasing the mouse button. The Copy table wizard appears to help you migrate the content to the database table.

  6. When the first page of the wizard appears to select the options of the copy operation and name the database table. Each option is explained in the Help (F1). Next, use the second page select the sheet columns you want to copy into the table. And finally, the third page of the wizard allows you to define the data type of each column of your table.

  7. Click Create to populate the new table with the spreadsheet data.

The Copy table operation copies only values and strings from the Calc spreadsheet. It does not copy formulas.

Creating queries

If you need get specific information from a database, use a query. Their results are special tables within the database. This document will demonstrate two different methods for building queries by:

Method 1: Use the Wizard to create a query

Queries created by the Query Wizard provide a list of information—a single answer or multiple answers, depending upon the circumstances.

To generate a query with the Wizard, do the following:

  1. Open the main database window (Figure 3).

  2. Click the Queries icon in the Database section.

  3. Select View > Queries, or press Alt+Q.

In the Tasks section, click Use Wizard to Create Query. The Query Wizard dialog opens (Figure 60). We will use the query to find albums by a certain musical group or individual (the album’s artist), including when each album was bought.

When working with a query, more than one table can be used. Since different tables may contain the same field names, the format for naming fields in a query is table name.field name, with a period (.) between the table name and the field name. For example, the Lunch field of the Vacations table used in a query has the name Vacations.Lunch.

Step 1: Select the fields

  1. Select the Table: CD-Collection option in the Tables drop-down list.

  2. Find the Available fields list fields in the CD-Collection table:

  1. Click Artist, and use the > button to move it to the Fields in the Query list.

  2. Move the AlbumTitle and DatePurchased fields in the same manner. You can also move a single field from one list to the other by double-clicking.

  3. If you make a mistake, you can return an unwanted field from the Fields in the Query list to the Available fields list by selecting it and then typing the < button.

  4. In other circumstances, you can use the >> and << buttons to move all fields in one list to the other list.

  1. Click Next.

To change the order of the fields, select the field you want to move and click the up or down arrow to the right of the Fields in the Query list.

Step 2: Select the sorting order

Base allows you to sort the information of our query with up to four fields. In this step, we must first decide which field is most important. In this query, the artist field is most important to us, next field is the album title, and finally the date purchased field is of least importance.

Figure 45: Sorting order page

Sorting order page

  1. Click the first Sort by drop-down list.

  1. Select the CD-Collection.Artist option.

  2. To list the artists in alphabetical order (a-z), select Ascending on the right.

  1. Click the first Then by drop-down list.

  1. Click the second Then by drop-down list.

  1. Click Next.

Step 3: Select the search conditions

Next, we will compare the name we entered with the names of the artist in our database and decide whether to include a particular artist in our query results or not.

The following options are available:

These conditions apply to numbers, letters (using alphabetical order), and dates.

  1. Since we are only searching for one thing, we will use the default setting of Match all of the following.

  2. We are looking for a particular artist, so select CD-Collection.Artist in the Fields drop-down and is equal to in the Condition drop-down.

  3. Type the name of the artist in the Value box.

  4. Click Next.

Step 4: Select the type of query

We want simple information, so the default setting: Detailed query is what we want. Click Next.

Since we have a simple query, the Grouping and Grouping conditions are not needed. Steps 5 and 6 of the wizard are skipped in our query.

Step 7: Assign aliases if desired

The fields, AlbumTitle and DatePurchased, have names made up of two words without separation. Instead, aliases can be made containing two words each (Album Title and Date Purchased, respectively).

  1. Change the alias for AlbumTitle to Album Title.

  2. Change the alias for DatePurchased to Date Purchased.

  3. Click Next.

Step 8: Checking results

  1. Make sure that you have the query conditions listed in the Overview.

  2. If something is wrong, use the Back button to move to the step that contains the error.

  3. Then use the Next button to return to this step.

  4. Name the query (suggestion: Query_Artists).

  5. To the right of this are two choices. Select Display Query.

  6. Click Finish.

Base displays the LibreOffice Base: Table Data View query showing any records that match the query. Close the window when you have finished with it.

Method 2: Use Design View to create a query

Creating a query using Design View is not as difficult as it may first seem. It may take multiple steps, but each step is fairly simple.

What fuel economy is our vehicle getting (miles per gallon in the USA)? This question requires us to create two queries, with the first query used as part of the second query.

Step 1: Open the first query in Design View

Step 2: Add tables to window

  1. In the Add Table or query dialog, click Fuel to highlight it.

  2. Click Add.

  3. Click Close.

  4. Base closes the Add Table or Query dialog and includes the Fuel table in the upper area of the Query Design window.

Figure 46: Add Table or Query dialog

Add Table or Query dialog

Move the mouse pointer over the bottom edge of the Fuel table (Figure 47) and drag the edge to make it longer and easier to see all of the fields in the table.

Figure 47: Fuel table in query

Fuel table in query

Step 3: Add fields to the table at the bottom

  1. Double-click the FuelID field in the Fuel table.

  2. Double-click the Odometer field.

  3. Double-click the FuelQuantity field.

  4. Double-click the FuelCost field.

The query table at the bottom of the Query Design dialog should now have four columns (Figure 61).

Step 4: Set the criterion for the query

We want the query to include FuelID values that are greater than 0.

  1. Type >0 in the Criterion cell under FuelID in the query table.

  2. Click the Run Query icon on the Query Design toolbar(circled in red in Figure 48), or click View > Run Query, or press F5.

Figure 48: Run Query icon in Query Design toolbar

Run Query icon in Query Design toolbar

Base presents the results of the query in the area above the query table.Figure 49 shows the Fuel table with sample entries. The query results based upon the Fuel table are shown in Figure 50.

Figure 49: Fuel table

Fuel table

Figure 50: Query of Fuel table

Query of Fuel table

Step 5: Save and close the query

Since this query contains the final odometer reading for our calculations, you sure name it End-Reading when saving it. Then close the query. Now click the Save icon in the main database window.

Step 6: Create the query to calculate the fuel economy

  1. Click Create Query in Design View in the Tasks area to open a new query.

  2. Base opens the LibreOffice Base: Query Design window and the Add Table or Query dialog.

  3. Add the Fuel table to the query just as you did in Step 2: Add tables to window but do not close the Add Table or Query dialog.

  4. Add the End-Reading query to this query.

  1. Click Queries to get the list of queries in the database (Figure 51).

  2. Click End-Reading.

  3. Click Add, and then click Close.

  4. Base closes the Add Table or Query dialog and displays the Fuel table and End-Reading query in the upper area of the Query Design window.

Figure 51: Selecting queries to add to another query

Selecting queries to add to another query

Step 7: Add fields to the table at the bottom of the query

When calculating the fuel economy, we will need the fuel quantity and distance traveled. Since the final odometer reading reveals the fuel quantity, we will use the End-Reading query to get it. We will also use the Odometer field from both the Fuel table and End-Reading query.

Figure 52: Tables in this query

Tables in this query

  1. Double-click FuelQuantity in the End-Reading query.

  2. Double-click Odometer in the End-Reading query.

  3. Double-click Odometer in the Fuel table.

Figure 53: Added fields to the query

Added fields to the query

Step 8: Enter the FuelID difference field

The difference between the FuelID value of the Fuel table and FuelID value of the End-Reading query should equal one (1).

  1. Type "End-Reading"."FuelID" - "Fuel"."FuelID" in the field to the right of the Odometer field of the Fuel table (Figure 62).

  2. Type = '1' in the Criterion cell of this column.

  3. Leave the Visible cell of this column unchecked.

  4. Calculate the distance traveled:

  1. Calculate fuel economy: Type ("End-Reading"."Odometer" - "Fuel"."Odometer")/"End-Reading"."FuelQuantity" in the next column to the right of the word Field (Figure 26).

  2. Type Fuel Economy as the alias.

When entering fields for these calculations, follow this format: table or query name. Then add a period and the field name. For hyphenated or multiple-word names (table or query), use double quotes around the table or query name. The query will then add the rest of the double quotes as in Figure 26.
Use the arithmetical symbol between the two. More than one calculation can be done by using parentheses to group the arithmetical operations.

Step 9: Run the query and make some modification

First, we will run the query to make sure it works correctly. Once we are sure that the query will work, we will hide all of the fields that we do not need.

Figure 54: Result of running the fuel economy query

Result of running the fuel economy query

  1. Click the Run Query icon on the Query Design toolbar (Figure 48). The results are shown in Figure 54.

Two of the column headers are identical. By giving these two headers different aliases, we can distinguish them.

  1. Add Aliases: Type the aliases as they are listed in Figure 55.

Figure 55: Query table with Odometer aliases added

Query table with Odometer aliases added

  1. Run the query again. The results are in Figure 56.

Figure 56: Query run with aliases

Query run with aliases

Step 10: Close, save, and name the query

Name the query Fuel Economy.

  1. Click the Save icon.

  2. Name the query.

  3. Close the query.

  4. Save the database file.

There are obviously other calculations that can be made in this query such as cost per distance traveled and how much of the cost belongs to each of the payment types.

To fully use queries requires a knowledge of set operations (unions, intersections, and, or, complements, or any combinations of these). Having a copy of the RDBMS manuals, is also extremely useful.

Creating reports

Reports retrieve information found in the database and arrange it in useful ways. They are similar to queries except that they are designed to be read by people. Queries are only designed to retrieve selected data from the database. Reports are generated from the database’s tables, views, or queries.

All reports are based upon a single table, view, or query, so first you need to decide what fields you want to use in the report. Before you use fields from different tables, you must first combine these fields in a single query or view. Then you can use this information to create a report.

For example, a report on vacation expenses includes both fuel costs and meal costs. These values are contained in fields of two different tables: Vacations and Fuel. Then this report will allow you to create a query or view.

Dynamic reports update only the data that is changed or added to a table or query. They do not show any modifications made to the table or query itself.
For example, after creating the report below, open the Fuel Economy query created in the previous section. For the “End-Reading”.“Odometer” – “Fuel”.“Odometer” column, change the number 1 to the number 3 (see Figure 62). The report will be identical before and after you make the change. But if you add more data to the query and run the report again, it will contain the new data. However, all data will be based upon “End-Reading”.“Odometer” – “Fuel”.“Odometer” having the value 1. No data will be present in the report for which “End-Reading”.“Odometer” – “Fuel”.“Odometer” has the value 3.

Example: Creating a report

We will create a report on vacation expenses. Before we create the report, we will need to know.

The expenses for our vacation are motel, tolls, miscellaneous, breakfast, lunch, supper, snacks, and fuel. One possible report could list the totals of each of these expense groups. Another could list the expense totals for each day of the vacation. A third could list the totals for each expense group for each type of payment. (This would let us know where the money came from to pay the expenses.) Once you create a query to do any one of these, you can create a report based upon the query.

We will now create two reports. One will list the expenses each day (other than fuel) and the second listing fuel statistics. The fields we need for the first report from the Vacations table are: Date, Motel, Tolls, Breakfast, Lunch, Supper, Snack Cost, and Miscellaneous. This report only requires the Vacations table. Had the report listed the total expenses for each of these fields, we would have to create a query to provide us with these totals, which is beyond the scope of this chapter.

The next report involves the Fuel table. Since this table includes fuel purchases at times other than during the vacation, we need to create a query that contains only the fuel purchased during vacation periods.

Report Wizard vs Report Design View

  1. When you open the Report Wizard, the Report Builder open as well. As you make your selections in the wizard, these appear in layout in the Report Builder. When you have finished making your selections, you save the report, name it and then close it.

  2. When using Design View to create a report, you open the Report Builder to design the layout of it. (There is only one layout available when the wizard is used.)

Vacations table report

Before we begin, we must create a new report:

  1. Click the Reports icon in the Database area of the main Base window (Figure 3), or select View > Reports, or press Alt+R.

  2. In the Tasks list, click Use Wizard to Create Report. The Report Wizard dialog and the Report Builder window open.

Step 1: Field selection

  1. Select Table: Vacations in the Tables or queries drop-down list.

  2. Use the > to move these fields from the Available fields list to the Fields in report list: Date, Motel, Tolls, Miscellaneous, Breakfast, Lunch, Supper, and SnCost. The other buttons (<, >>, <<, up arrow, and down arrow) and the double-click action perform similar functions to the other wizard pages as described earlier in this chapter.

  3. Click Next.

Step 2: Labeling fields

Change any field labels you wish. We will shorten Miscellaneous to Misc. and expand SnackCost into two words.

  1. Shorten Miscellaneous to Misc.

  2. Change SnCost into Snack Cost.

  3. Click Next.

Step 3: Grouping

  1. Since we are grouping by the date, use the > button to move the Date field from the Fields list to the Groupings list.

  2. You can use the < button to return an entry in the Groupings list to the Fields list. You can use the up and down arrows to re-order entries in the Groupings list or you can double-click to move a field from one list to the other.

  3. Click Next.

Step 4: Sort options

We do not want to do any additional sorting.

Step 5: Choose layout

  1. Select Columnar, three columns in the Layout of data list.

  2. Layout of headers and footers have no possible selections.

  3. Select Landscape as the Orientation for the page layout.

  4. Click Next.Figure 28 shows part of the final template that has been built up in the Report Builder as you reach the end of the wizard.

Step 6: Create report

  1. Title the report: Vacation Expenses.

  2. Select the Dynamic report option.

  3. Select the Create report now option.

  4. Click Finish.

  5. LibreOffice opens the output report in read-only mode in Writer.

The report (Figure 29) has been created. However, the date could be formatted better and all the numbers need to be formatted as currency, and the report could have a heading that includes its name, its author, and the date it was prepared. If the report had more than one page, the page numbers could be put in a footer of each page, perhaps including the total number of pages. But to do these things, you must use Report Builder.

Report Builder: another way to create reports

The Report Builder allows you to create complex database reports. You can define group and page headers, group and page footers, and calculation fields. Report Builder is installed with LibreOffice.

While the Report Wizard allows us to create a template in Report Builder for our report. When we open Report Builder, we open it with this template. By modifying the template, we also modify the report. For example, we can change the Date field’s format, and it will change the format of all the dates contained in that field in the above report. Similarly, we can change the field formatting of any of the other fields and change the format everywhere that field appears in the report.

Report Builder can also create reports by itself. To do this, click Create Report in Design View. For instructions on how to use the Report Builder, see Chapter 6, Reports, in the Base Guide.

Accessing other data sources

LibreOffice allows data sources to be accessed and then linked into LibreOffice documents. For example, a mail merge can link to an external document containing a list of names and addresses into a letter. One copy of the letter being generated for each entry.

To access a data source that is not a *.odb file:

  1. Select File > New > Database, or click the arrow at the right of the New icon on the Standard toolbar and select the Database option in the menu, to open the Database Wizard.

  2. Select Connect to an existing database. Select the required database type in the drop-down list. Click Next.

  3. The remaining steps in the Database Wizard may vary depending on which database type you have chosen.

  4. At some stage the wizard may prompt you to browse and select a database of interest. Normally, you will select it to register the database and open the database for editing.

  5. Finally, click Finish to exit the wizard. Name and save the database in the location of your choice.

Accessing a spreadsheet as a data source

Accessing a spreadsheet is similar to accessing other databases:

  1. Select File > New > Database, or click the arrow at the right of the New icon on the Standard toolbar and select the Database option in the menu, to open the Database Wizard dialog.

  2. Select Connect to an existing database. Select Spreadsheet in the drop-down list. Click Next.

  3. Click Browse to locate the spreadsheet you want to access. If the spreadsheet is password protected, check the Password required box. Click Next.

  4. Make sure that the Yes, register the database for me and Open the database for editing options are selected.

  5. Click Finish to exit the wizard. Name and save the database in the location of your choice.

If you access a spreadsheet with this method, you cannot change anything in the spreadsheet using Base. You can only view the contents of the spreadsheet, run queries, and create reports based upon the data already entered into the spreadsheet.
All changes in a spreadsheet must be made in the spreadsheet itself with Calc. After modifying the spreadsheet and saving it, the changes will be in the database. When you create and save an additional sheet in your spreadsheet, the database will have a new table the next time you access it.

Registering *.odb databases

Databases created by LibreOffice are in the *.odb (OpenDocument Database) format. Other programs can also produce databases in this format. Registering a *.odb database is simple:

  1. Choose Tools > Options > LibreOffice Base > Databases.

  2. Under Registered Databases, click New. LibreOffice displays the Create Database Link dialog.

  3. Browse to where the database is located. Make sure the registered name is correct.

  4. Click OK.

Sometimes after updating LibreOffice to a newer version, your list of registered database files disappears. When that happens, you can use these steps to re-register your database files with your latest version of LibreOffice.

Using data sources in LibreOffice

Once the data source is registered, whether a spreadsheet, text document, external database or other accepted data source, you can use it in other LibreOffice components including Writer and Calc.

Viewing data sources

First, open a document in Writer or Calc and view the data sources available by pressing Ctrl+Shift+F4 or select View > Data Sources. This brings up a list of registered databases,  including Bibliography and any other registered database, such as the Automobile database created earlier in this chapter.

To view each database, click the drop-down arrow for the database’s name (Figure 57). This expands to show Queries and Tables. Click the drop-down arrow for Tables to view the individual tables created. Now click on a table to see all the records held in it.

Figure 57: Databases in Data Sources window

Databases in Data Sources window

Editing data sources

Some data sources (but not spreadsheets) can be edited in the Data Sources window. A record can be edited, added, or deleted.

View a table’s data

If you click on a table, its rows and columns of data appear on the right side of the Data Sources window. Editing this data requires only a click in the cell whose data should be changed, change the data, and click in the row above or below it to save the new data.

Beneath the records are five small buttons. The first four move the cursor to the beginning, to the left, to the right, and to the end respectively. The fifth button, with a small star, inserts a new record.

To delete a record, right-click on the gray box to the left of a row to highlight the entire row, and select Delete Rows in the context window to remove the selected row.

Figure 58: Deleting a row in the Data Sources window

Deleting a row in the Data Sources window

Launching Base to work on data sources

You can launch LibreOffice Base at any time in the Data Sources window. Just right-click on a database or its Tables or Queries components and select Edit Database File in the context menu. Once in Base, you can edit, add, and delete tables, queries, forms, and reports.

Using data sources in Writer and Calc

Data can be placed into Writer and Calc documents from the tables in the Data Sources window. In Writer, values from individual fields can be inserted. Or a complete table can be created in the Writer document. One common way to use a data source is to perform a mail merge.

Figure 59: Toolbar for the Data Sources window

Toolbar for the Data Sources window

Choose Tools > Mail Merge Wizard or click on the Mail Merge icon (circled in Figure 59) in the Data Sources window to start the Mail Merge Wizard, which steps you through creating a mail merge document. See Chapter 14 in the Writer Guide.

Writer documents

When you need to insert a field from a table in the Data Sources window into a Writer document, click on the field name (the gray square at the top of the field list) with the left mouse button held down, drag the field onto the document. In a Writer document, it will appear as <FIELD> (where FIELD is the name of the field you dragged).

For example, if you wish to enter the cost of meals and who paid for them on a certain date of a vacation into the database:

  1. Open the list of data sources (Ctrl+Shift+F4) and select the Vacations table in the Automobile database.

  2. Use this sentence: “On (date), our breakfast cost (amount) paid by (name), our lunch cost (amount) paid by (name), and our supper cost (amount) paid by (name).” But only type
    “On, our breakfast cost paid by, our lunch cost paid by, and our supper cost paid by.”

  3. To replace (date), click the field name Date in the Data Sources window and drag it to the right of the word On. Insert an extra space if necessary. The result: On <Date>. If you have field shadings turned on (View > Field Shadings), <Date> has a gray background. Otherwise it does not.

  4. To replace first (amount), click the Breakfast field name and drag it to the right of our breakfast cost. Make sure you have the proper spacing between the field names and the words before and after them. Result: breakfast cost <Breakfast>.

  5. To replace the first (name), click the BPayment field name and drag it to the right of paid by. Result: paid by <BPayment>.

  6. In the same way, fill in the rest of the fields in the sentence.

  1. Final result: On <Date>, our breakfast cost <Breakfast> paid by <BPayment>, our lunch cost <Lunch> paid by <LPayment>, and our supper cost <Supper> paid by <SPayment>.

  2. Add data to the fields of the sentence:

  1. Click the gray box to the left of the row of data you want to add. That row should be highlighted like the second row of Figure 65.

  2. Click the Data to Fields icon in the Table Data toolbar (circled in Figure 65). This should fill the fields with the data from the row you chose.

  3. Click another row and then click this icon again. The data in the sentence changes to reflect this selected row of data.

  4. Save the document if you want to use it as an example later.

Adding any data in table format is easier. Some of the steps are quite similar.

  1. Navigate to the place you want to place the table and click the location.

  2. Ctrl+Click the gray box to the left of each row of the data source that you want to be a row in your table if the rows are not consecutive. To select consecutive rows, click the gray box to the left of the top desired row and Shift+click the bottom desired row.

  3. Click the Data to Text icon to open the Insert Database Columns dialog (Figure 31). (The Data to Text icon is to the left of the Data to Fields icon in Figure 65.)

  4. Move the fields you want in your table from the Database columns list to the Table column(s) list.

  1. To start over, click the double arrow pointing to the left (this icon may have a different appearance in some environments).

  2. Select the settings for your table. Use the default settings as in Figure 31.

  3. Click OK. Save the document.

Calc spreadsheets

There are two ways to transfer data into a Calc spreadsheet:

Enter the data into the spreadsheet cells.

Create complete new records in the spreadsheet. While you can directly access the data inserted into the spreadsheet cells, new records created in the spreadsheet are read-only.

Use the Data to Text icon to enter data directly to the spreadsheet cells as if you are making a table in a Writer document. But there are certain differences.

The steps are straightforward.

Click the cell of the spreadsheet which you want to be the top left cell of your data, including the column names.

  1. Use Ctrl+Shift+F4 to open the Data Sources window and select the table whose data you want to use.

  2. Select the rows of data you want to add to the spreadsheet:

  1. Click the Data to Text icon to insert the data into the spreadsheet cells.

  2. Save the spreadsheet.

Adding records to a spreadsheet is fairly easy. You need to have the Data Sources window open, your spreadsheet open, and the table you want to use selected.

  1. Drag and drop the gray box containing the field name for the table’s ID field (the column header) to where you want the record to appear in the spreadsheet.

  2. Repeat until you have copied all of the fields you need to where you want them in the spreadsheet.

  3. Select File > Save, or click the Save icon on the Standard toolbar, to name and save the spreadsheet. Close the file.

Next time you open the spreadsheet, the fields will be populated with data from the first record of the table, and the Form Navigation toolbar will appear at the bottom of the spreadsheet.

Figure 60: First page of the Query Wizard

First page of the Query Wizard

Figure 61: Query table

Query table

Figure 62: Typing in calculation of fields

Typing in calculation of fields

Figure 63: Field for distance traveled calculations

Field for distance traveled calculations

 

Figure 64: Adding fields to a report

Adding fields to a report

Figure 65: Selected row in Data Sources window

Selected row in Data Sources window

Figure 66: Giving aliases to fields

Giving aliases to fields

Click the arrows on the Form Navigation toolbar to view the different records of the table. (The arrows are circled in red in Figure 32.) The number in the box changes when you change the record number by clicking an arrow. The data in the fields changes to reflect the data for that particular record number.

Contents