Getting Started Guide

Chapter 8
Getting Started with Base

LibreOffice's Database Component





Copyright

This document is Copyright © 2010 by its contributors as listed below. You may distribute it and/or modify it under the terms of either the GNU General Public License (http://www.gnu.org/licenses/gpl.html), version 3 or later, or the Creative Commons Attribution License (http://creativecommons.org/licenses/by/3.0/), version 3.0 or later.

All trademarks within this guide belong to their legitimate owners.

Contributors

Ron Faile Jr.
Hal Parker

Feedback

Please direct any comments or suggestions about this document to: documentation@libreoffice.org

Acknowledgments

This chapter is based on Chapter 8 of Getting Started with OpenOffice.org. The contributors to that chapter are:

Dan Lewis Magnus Adielsson
JiHui Choi Iain Roberts
Jean Hollis Weber

Publication date and software version

Published 30 December 2010. Based on LibreOffice 3.3.

Note for Mac users

Some keystrokes and menu items are different on a Mac from those used in Windows and Linux. The table below gives some common substitutions for the instructions in this chapter. For a more detailed list, see the application Help.

Windows/Linux

Mac equivalent

Effect

Tools Options menu selection

LibreOffice Preferences

Access setup options

Right-click

Control+click

Opens a context menu

Ctrl (Control)

z (Command)

Used with other keys

F5

Shift+z+F5

Opens the Navigator

F11

z+T

Opens the Styles & Formatting window



Contents

Copyright 2

Note for Mac users 2

Introduction 4

Planning a database 5

Creating a new database 5

Creating database tables 6

Using the Wizard to create a table 7

Creating a table by copying an existing table 10

Creating tables in Design View 10

Creating tables for the list box 13

Adding data to the list table 14

Defining relationships 14

Creating a database form 16

Using the Wizard to create a form 16

Modifying a form 19

Creating forms and subforms in Design View 29

Accessing other data sources 29

Accessing a spreadsheet as a data source 30

Registering *.odb databases 30

Using data sources in LibreOffice 30

Viewing data sources 30

Editing data sources 31

Launching Base to work on data sources 31

Using data sources in Writer and Calc 31

Entering data in a form 34

Creating queries 36

Using the Wizard to create a query 36

Using the Design View to create a query 39

Creating reports 44

Creating a static report 44

Creating a dynamic report 47

Modifying a report 49

More ways to create reports 51



Introduction

A data source, or database, is a collection of pieces of information that can be accessed or managed by LibreOffice. For example, a list of names and addresses is a data source that could be used for producing a mail merge letter. A shop stock list could be a data source managed through LibreOffice.

Note

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

This chapter covers creating a database, showing what is contained in a database and how the different parts are used by LibreOffice. It also covers using the Base component of LibreOffice to register other data sources. A data source can be a database, spreadsheet, or text document.

Note

LibreOffice Base uses the HSQL database engine. All of the files created by this engine are kept in one zipped file. The database forms are included in this zipped file.

A database consists of a number of fields that contain the individual pieces of data. Each table of the database is a group of fields. When creating a table, you also determine the characteristics of each field in the table. Forms are for data entry into the fields of one or more tables associated with the form. They can also be used for viewing fields from one or more tables associated with the form. A query creates a new table from the existing tables based upon how you create the query. A report organizes the information of the fields of a query in a document according to your requirements.

Caution

The database in LibreOffice requires Java Runtime Environment (JRE). If you do not have it on your computer, you can download it from www.java.com and install it following the instructions on the site. It should be Java 6.0 or higher. In LibreOffice, use Tools Options LibreOffice Java to register Java.

Windows' version of JRE can not be used, while there are other versions that can.

Base creates relational databases. This makes it fairly easy to create a database in which the fields of the database have relationships with each other.

For example: Consider a database for a library. It will contain a field for the names of the authors and another field for the names of the books. There is an obvious relationship between the authors and the books they have written. The library may contain more than one book by the same author. This is what is known as a one-to-many relationship: one author and more than one book. Most if not all the relationships in such a database are one-to-many relationships.

Consider an employment database for the same library. One of the fields contains the names of the employees while others contain the social security numbers, and other personal data. The relationship between the names and social security numbers is one-to-one: only one social security number for each name.

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. The tables are subsets. Relationships are defined in terms of unions and intersections of the subsets (tables).

To explain how to use a database, we will create one for automobile expenses. In the process, we will be explaining how a database works.

Planning a database

The first step in creating a database is to ask yourself many questions. Write them down, and leave some space between the questions to later write the answers. At least some of the answers should seem obvious after you take some time to think.

You may have to go through this process a few times before everything becomes clear in your mind and on paper. Using a text document for these questions and answers makes it easier to move the questions around, add additional questions, or change the answers.

Here are some of the questions and answers I developed before I created a database for automobile expenses. I had an idea of what I wanted before I started, but as I began asking questions and listing the answers, I discovered that I needed additional tables and fields.

What are the fields going to be? My expenses divided into three broad areas: fuel purchases, maintenance, and vacations. The annual cost for the car’s license plate and driver’s license every four years did not fit into any of these. It will be a table of its own: license fees.

What fields fit the fuel purchases area? Date purchased, odometer reading, fuel cost, fuel quantity, and payment method fit. (Fuel economy can be calculated with 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). But it would be nice if there was a way to write notes. So, 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. Since these purchases are made by one of two bank cards or with cash, I want 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. Do I list all the snacks individually or list the total cost for snacks for the day? I chose to divide snacks into two fields: number of snacks and total cost of snacks. I also need a payment type for each of these: breakfast, lunch, supper, and total cost of snacks.

What are the fields that are common to more than one area? Date appears in all of the areas as does odometer reading and payment type.

How will I use this information about these three fields? While on vacation, I want the expenses for each day to be listed together. The date fields suggest a relationship between the vacation table and the dates in each of these tables: fuel and food, This means that the date fields in these tables will be linked as we create the database.

The type of payment includes two bank cards and cash. So, we will create a table with a field for the type of payment and use it in list boxes in the forms.

Tip

While we have listed fields we will create in the tables of the database, there is one more field that may be needed in a table: the field for the primary key. In some tables, the field for the primary key has already been listed. In other tables such as the payment type, an additional field for the primary key must be created.

Creating a new database

To create a new database, choose File New Database from the menu bar, or click the arrow next to the New icon on the Standard toolbar and select Database from the dropdown menu. Both methods open the Database Wizard.

On the first page of the Database Wizard, select Create a new database and then click Next.

The second page has two questions. Make sure the choice for the first question is Yes, register the database for me and the choice for the second question is Open the database for editing. Click Finish.

Note

If the database is not registered, it will not be accessible to the other LibreOffice components such as Writer and Calc. If the database is registered, other components can access it.

Save the new database with the name Automobile. This opens the Automobile – LibreOffice Base window. Figure 1 shows part of this window.

Frame2

Tip

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

Caution

As you create a database, you should save your work regularly. This means more than saving what you have just created. You must save the whole database as well.

For example, when you create your first table, you must save it before you can close it. When the table is first saved, it is also made part of the database.

Creating database tables

In a database, a table stores information for a group of things we call fields. For example, a table might hold an address book, a stock list, a phone book or a price list. A database can have from one to several tables.

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

Using the Wizard to create a table

Wizards are designed to do the basic work. Sometimes this is not sufficient for what we want; in those cases we can use a wizard as a starting point and then build upon what it produces.

The Table Wizard in Base contains two categories of suggested tables: business and personal. Each category contains sample tables from which to choose. Each table has a list of available fields. We can delete some of these fields and add other fields.

A field in a table is one bit of information. For example, a price list table might have one field for item name, one for the description, and a third for the price.

Since none of the fields we need for our Automobile database are contained in any of the sample wizard tables, we will create a simple table using the wizard that has nothing to do with our database. This section is an exercise in explaining how the Wizard works.

The Wizard permits the fields of the table to come from more than one suggested table. We will create a table with fields from three different suggested tables in the Wizard.

Caution

Every table requires a Primary key field. (What this field does will be explained later.) We will use this field to number our entries and want that number to automatically increase as we add each entry.

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

Step 1: Select fields.

We will use the CD-Collection Sample table in the Personal category to select the fields we need.

  1. Category: Select Personal. The Sample Tables dropdown list changes to a list of personal sample tables.

  2. Sample tables: Select CD-Collection. The Available fields box changes to a list of available fields for this table.

  3. Selected fields: Using the > button, move the following fields from the Available fields window to the Selected fields window in this order: CollectionID, AlbumTitle, Artist, DatePurchased, Format, Notes, and NumberofTracks.

  4. Selected Fields from another sample table. Click Business as the Category. Select Employees from the dropdown list of sample tables. Use the > button to move the Photo field from the Available fields window to the Selected fields window. It will be at the bottom of the list directly below the NumberofTracks field.

  5. 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.

  6. If you make a mistake in the order of the selected fields, click on the field name that is in the wrong order and use the Up or Down arrow on the right side of the Selected fields list to move the field name to the correct position.

  7. Click Next.

Frame11

Step 2: Set field types and formats.

In this step you give the fields their properties. When you click a field, the information on the right changes. (See Figure 3.) You can then make changes to meet your needs. Click each field, one at a time, and make the changes listed below.

Frame4

Note

If any of these fields requires an entry, set Entry required to Yes. An entry with that field blank will then not be allowed. In general, only set Entry required to Yes if something must always be put in that field. By default, Entry required is set to No.

Note

In Base the maximum length of each field must be specified on creation. It is not easy to change this later, so if in doubt specify a greater length. Base uses VCHAR as the field format for text fields. This format only uses the actual number of characters in a field up to the limit set. So, a field containing 20 characters will only use space for 20 characters even if the limit is set at 100. Two album titles containing 25 and 32 characters respectively will use space for 25 and 32 characters and not 100 characters.

When you have finished, click Next.

Note

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.

Step 3: Set primary key.

  1. Create a primary key should be checked.

  2. Select option Use an existing field as a primary key.

  3. In the Fieldname dropdown list, select CollectionID.

  4. Check Auto value if it is not already checked.

  5. Click Next.

Note

A primary key uniquely identifies an item (or record) in the table. For example, you might know two people called “Randy Herring” or three people living at the same address and the database needs to distinguish between them.

The simplest method is to assign a unique number to each one: number the first person 1, the second 2, and so on. Each entry has one number and every number is different, so it is easy to say “record ID 172”. This is the option chosen here: CollectionID is just a number assigned automatically by Base to each record of this table.

Step 4: Create the table.

  1. If desired, rename the table at this point. If you rename it, make the name meaningful to you. For this example, make no changes.

  2. Leave the option Insert data immediately checked.

  3. Click Finish to complete the table wizard. Close the window created by the table wizard. You are now back to the main window of the database with the listing of the tables, queries, forms, and reports. Notice that a table named “CD-Collection” is now listed in the Tables portion of the window.

Creating a table by copying an existing table

If you have a large collection of music, you might want to create a table for each type of music you have. Rather than creating each table from the wizard, you can make copies of the original table, naming each according to the type of music contained in it.

  1. Click on the Tables icon in the Database pane to see the existing tables.

  2. Right-click on the CD-Collection table icon. Choose Copy from the pop-up menu.

  3. Move the mouse pointer below this table, right-click, and select Paste. The Copy table dialog opens.

  4. Change the table name to Pop and click Next.

  5. Click the >> button to move all the fields from the left box to the right box and click Next.

  6. Since all the fields already have the proper Field type, no changes should be needed. However, this is the time and place to make any changes if they are needed. (See Caution below for the reason why.) Click Create. The new table is created.

Caution

Once tables have been created using the wizard and data has been entered, editing them should be very limited. Fields can be added or deleted, but adding a field requires taking the time to enter the data for that one field for every record having an entry for that field.

Deleting a field deletes all the data once contained in that field. Changing the field type of a field can lead to data being lost either partially or completely. When creating a new table, it pays to create the fields with the correct names, length, and format before you add any data.

Deleting a table removes all of the data contained in every field of the table. Unless you are sure, do not delete a table.

Creating tables in Design View

Design View is a more advanced method for creating a new table, in which you directly enter information about each field in the table. We will use this method for the tables of our database.

Note

While the Field type and formatting are different in Design View, the concepts are the same as in the Wizard.

The first table to be created is Fuel. Its fields are FuelID, Date, FuelCost, FuelQuantity, Odometer, and PaymentType.

  1. Click Create Table in Design View.

  2. FuelID field:

    1. Type FuelID as the first Field Name. Press the Tab key to move to the Field Type column.

    2. Select Integer [INTEGER] as the Field Type from the dropdown list. (The default setting is Text [VARCHAR].)

      Tip

      A shortcut for selecting from the Field Type dropdown list: press the key for the first letter of the choice. You can cycle through the choices for a given letter by repeatedly pressing that key.

    3. Change the Field Properties in the bottom section.

Change AutoValue from No to Yes.

    1. Set FuelID as the Primary key.

Right-click on the green triangle to the left of FuelID (Figure 4) and choose Primary Key from the menu. This places a key icon in front of FuelID.

Frame1

Note

The primary key serves only one purpose: to uniquely identify the record. Any name can be used for this field. We have used FuelID for convenience, so we know to which table it belongs.

  1. All other fields (Date, FuelCost, FuelQuantity, Odometer, and PaymentType):

Frame6

Frame5

  1. To access additional formatting options, click the button to the right of the Format example field.

  2. Description can be anything, or can be left blank.

  3. To save and close the table, choose File Save. Name the table Fuel. Close the Fuel table.

Follow the same steps to create the Vacations table. The fields and their field types are listed in Figure 7. Make sure you make the Date field the primary key before closing. (Right-click the gray box to the left of Date and select Primary key from the menu.) Save the table and name it Vacations. Close the Vacations table.

Frame32

Creating tables for the list box

When the same information can be used in several fields, design a table for each type of information. Each table will contain two fields: the information field and ID, in that order.

Caution

You must create these tables with the information field listed first and the ID field listed last. Failure to do so will produce the wrong results. For my Payment table, I use Name and ID as my fields, with Dan, Kevin, and Cash being the Name entries. The corresponding ID entries are 0, 1, 2. When the Name field is listed first in the table, one of the three names will appear in the payment field of the Fuel table. If the ID field is listed first, 0, 1, or 2 appear in the payment field instead.

  1. Follow the directions in “Creating tables in Design View” on page 10. In the table, the two fields can be Type and PaymentID. In the Field Properties, set AutoValue to Yes for the PaymentID field. Set the PaymentID field as the primary key. (See Figure 8.)

  2. Save the table using the name Payment Type.

Frame12

Note

If you have several tables to create with the same fields, design one table and produce the other tables by cutting and pasting. (See “Creating a table by copying an existing table” on page 10.)

Adding data to the list table

List tables do not require a form. Instead, add their data directly to the table. In this example, use the names of the two people with a bank card and Cash for cash purchases.

  1. In the main database window, click on the Tables icon (Figure 1). In the list of tables, right-click on Payment Type and select Open from the pop-up menu.

    1. Enter Dan in the Type column in the first row. Press the Tab key to move to the second row. Notice that the PaymentID value for this row changes from <AutoField> to 0.

    2. Enter Kevin in the second row. The PaymentID changes to 1.

    3. Enter Cash in the third row. The PaymentID changes to 2.

  1. Save and close the table window.

Tip

You can also use the Enter key to move from entry field to entry field, or use the down arrow key to move from row to row.

Defining relationships

Now that the tables have been created, what are the relationships between our tables? This is the time to define them based upon the questions we asked and answered in the beginning.

When on vacation, we want to enter all of our expenses all at one time each day. Most of these expenses are in the Vacations table, but the fuel we buy is not. So, we will relate 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.)

The Vacations table also contains several fields for the type of payment used. For each field listing the payment type, there is only one entry from the Payment Type table. This is a one to one relationship: one field in one table to one entry from the other table. (It is designated 1:1.) Other tables also contain fields for the type of payment. The relationship between the fields of those tables and the Payment Type table are also 1:1.

Since the Payment Type table only provides a static list, we will not be defining a relationship between the Payment Type table and the fields of the other tables which use the entries of the Payment Type table. That will be done when the forms are created.

The Fuel and Maintenance tables do not really have a relationship even though they share similar fields: Date and Odometer.

Tip

As you create your own databases, you need to also determine where tables are related and how.

  1. To begin defining relationships, choose 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.)

  2. On the Add Tables dialog, use either of these ways to add a table to the Relation Design window:

  1. Click Close to close the Add Tables dialog when you have added the tables you want.

  2. You can define the relationship between the Vacations and Fuel tables in two ways:

Frame37

Frame39

  1. Modifying the Update options and Delete options section of the Relation window.

    1. Right-click the line connecting the Date fields in the two table lists to open a menu.

    2. Select Edit to open the Relations dialog (Figure 11).

    3. Select Update cascade.

    4. Select Delete cascade.

    5. Click OK to close the Relations dialog and choose File Save to save the Relation Design window.

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. It also permits you to delete a field from the table.

Frame40

Creating a database form

Databases are used to store data. But, how is the data put into the database? Forms are used to do this. In the language of databases, a form is a front end for data entry and editing.

Frame9

Frame10

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

Using the Wizard to create a form

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 1), click the Forms icon in the left column. In the Tasks list, double-click Use Wizard to Create Form to open the Form Wizard (Figure 14). Simple forms require only some of these steps, while more complex forms may use all of them.

Step 1: Select fields.

  1. Under Tables or queries, select Table: Vacations. Available fields lists the fields for the Vacations table.

  2. Click the right double arrow to move all of these fields to the Fields in the form list. Click Next.

Frame13

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, this would be done in step 4.

  1. Click the box labeled Add Subform.

  2. Click Subform based upon existing relation.

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

Frame54

Step 3: Add subform fields.

This step is exactly the same as step 1. The only difference is that not all of 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 right.

  3. Click the FuelID field to highlight it.

  4. Use the < button to move the FuelID to the left (Figure 16).

  5. Click Next.

Frame55

Step 4: Get joined fields.

This step is for tables or queries for which no relationship has been defined. Because we have already defined the relationship, the wizard skips this step.

Note

It is possible to create a relationship between two tables that is based upon more than one pair of fields. How to do that and why is discussed in the Base Guide.

Caution

When selecting a pair of fields from two tables to use as a relationship, they 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.

  • No field from the subform can be the Primary key for its table. (FuelID cannot be used.)

  • Each pair of joined fields must have the same file type.

  • One of the fields from the main form must be the Primary key for its table. (Date would have to be used.)


Step 5: Arrange controls.

Each control in a form consists of two parts: label and field. This step in creating the form determines where a control’s label and field are placed in relationship with each other. The four choices from left to right are Columnar left, Columnar - Labels on top, As Data Sheet, and In Blocks - Labels Above.

  1. Arrangement of the main form: Click the second icon (Columnar - Labels on top). The labels will be placed above their field.

  2. Arrangement of the subform: Click the third icon (As Data Sheet). (The labels are column headings and the field entries are in spreadsheet format.) Click Next.

Step 6: Set data entry.

Unless you have a need for any of these entries to be checked, accept the default settings. Click Next.

Step 7: Apply styles.

  1. Select the color you want in the Apply Styles list. (I chose the beige which is Orange 4 in the Color table.)

  2. Select the Field border you want. (I prefer the 3-D look. You might want to experiment with the different possible settings.)

  3. Click Next.

Step 8: Set name.

  1. Enter the name for the form. In this case, it is Fuel.

  2. Click Modify the form.

  3. Click Finish. The form opens in Edit mode.

Modifying a form

We will be moving the controls to different places in the form and changing the background to a picture. We will also modify the label for the PaymentType field as well as change the field to a list box.

First, we must decide what we want to change. The discussion will follow this ten step outline of our planned changes.

  1. Provide a dropdown capability for the Date field in the main form, and lengthen the field to show the day of the week, month, day, and year.

  2. Shorten the length of the payment fields (all fields containing the word Payment).

  3. Move the controls into groups: food, fuel subform, and miscellaneous.

  4. Change the wording of some of the labels. Some single words should be two words. Some abbreviations should be used if possible (Misc. for miscellaneous).

  5. Change the lengths of several fields and labels. Only Lunch, Supper, Motel, and Tolls have acceptable lengths. But for a better appearance, changes will be made to these as well.

  6. Replace all the fields whose label ends in Payment with a list box containing the entries from the Payment Type table.

  7. Lengthen the Note field vertically, add a scroll bar, and move it.

  8. Make changes in the Date and PaymentType columns of the subform that are similar to the changes in the main form.

  9. Add headings for each group in the main form.

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

Here are some methods that we will be using in these steps. The controls in the main form consists of a label and its field. Sometimes we want to work with the entire control, other times we want to work with only the label or the field, and there are times when we want to work with a group of controls.

Frame57

Frame33

    1. Click the field of the top left control to be moved, to select it.

    2. Move the cursor to just above and to the left of the selected control.

    3. Drag the cursor to the bottom right of the group of controls and release the mouse button.

As you drag the cursor, a dashed box appears showing what is contained in your selection. Make sure it is big enough to include the entire length of all the controls.

When you release the mouse button, a border with its green handles appears around the controls you selected.

Frame58

Move the cursor over one of the fields. It changes to a drag icon. Drag the group of controls to where you want them.

Tip

When either changing size or moving a control, two properties of the Form Design toolbar should be selected: Snap to Grid, and Guides when Moving. Your controls will line up better, and an outline of what you are moving moves as the cursor moves.

You should also have both rulers active (View Ruler).

Step 1: Change the Date field.

  1. Control+click the Date field to select it.

  2. Move the cursor over the middle green handle on the right side. It should change to a double-headed arrow.

  3. Hold the left mouse button down as you drag the cursor to the right until the length is 6 cm. The vertical dashed line is lined up with the 6. Release the mouse button.

  4. Click the Control icon in the Form Controls toolbar. The Properties: Date Field window opens. Each line contains a property of the field.

Frame41

Tip

To see what the Date field will look like, click the Form Mode On/Off icon (the second icon from the left in Figure 20). You can do this any time you want to see the form with the changes you have made.

Step 2: Shorten the width of some fields.

All of the fields with a label containing the word payment are too wide. They need shortening before the controls are moved.

  1. Control+click the BPayment field.

Frame28

  1. Move the cursor over the middle green handle on the right. The cursor becomes a double-headed arrow.

  2. Drag the cursor to the left until the field is 2.5 cm wide (1 inch).

    Tip

    If you have the Snap to Grid and Guides when moving icons selected in the Design Format toolbar, you will see how wide the field is as you shorten it.

  3. Repeat these steps to shorten these fields: Lpayment, SPayment, SnPayment, Mpayment, and MiscPayment.

Step 3: Move the controls to group them by category.

We want to move the controls so that they look like Figure 22.

Frame42

  1. Click the first control you want to move. A border appears around the control with eight green handles.

  2. Move the cursor over the label or field of the control. It change shape to a drag icon.

  3. Drag and drop the control to where you want it.

    Caution

    Do not use Control+click when moving a field. It moves either the field or the label but not both. To move both, use a mouse click and drag to the desired spot.

  4. Use the same steps to move the rest of the controls to where they belong.

Step 4: Change the label wording.

Field names have been single words for some time. However, the labels for the fields in a form can be more than one word. So, we will change them by editing the text in the label.

Note

The table wizard in Base contains suggested fields which are single words, often consisting of two separate words. Table names in the wizard have been that way also. However, you can use multiple words as the name of a field, label, or table. We will do so when we create a list box for each of the payment type controls.

  1. Control+click the SnackNo label. Do one of the following:

  1. The dialog that opens is labeled Properties: Label Field. It contains all of the properties of the selected label.

  1. Use the same procedure to change these labels as well: BPayment to Payment, LPayment to Payment, SPayment to Payment, Miscellaneous to Misc., SnackCost to Snack Cost, MPayment to Payment, MiscPayment to Misc. Payment, and MiscNotes to Misc. Notes.

Tip

You can modify all of the listings in the Properties window. For example, if you change the Alignment from Left to Center, the word or words in the label are centered within the label. When you have some time, you might want to experiment with different settings just to see the results you get.

Step 5: Change the widths of the labels and fields.

We want the following controls to be 2 cm wide (0.8 inches): Breakfast, Lunch, Supper, Odometer, Snack No., Tolls, Snack Cost, Motel, and Misc. All of the payment fields were changed in step 2, but Misc. Payment needs to be changed to 3 cm (1.2 inches).

  1. Right-click Breakfast and choose Position and Size. On the Position and Size dialog, change Width to 2 cm.

  2. Repeat for the other listed controls, using 3 cm for Misc.Payment.

Caution

When changing the position or size of an entire control, use the Position and Size dialog or the drag and drop method.

When working with either the label or the field (but not both at the same time), you can use the Properties dialog to make these changes when you want to be exact. However, you need to be careful not to accidentally select the entire control for use with the Properties dialog or you will apply the exact same values to both the label and field. For example, if you enter the values for a new position, both the field and the label moves to the same position and the field is positioned on top of the label. Then you have to move each of them to where you want them.

Note

To open the Properties window, right-click a control and select Control from the pop-up menu. Or, you can click the Control icon in the Form Controls toolbar. Just be careful, and use Control+Z to undo any mistakes you may make. Detailed instructions on how to use the Properties window will be in the Base Guide.

Step 6: Replace fields with other fields.

We want to replace the PaymentType field with a List Box. Then we can choose the type of payment from the Payment Type table rather than having to manually enter the type. In my case, each of my payment types begins with a different letter. If I enter the first letter of the payment type, the rest of the word automatically appears. I can then go to the next field.

  1. Control+click the Payment field for Breakfast. The green handles appear around the field but not around the Label.

Frame48

  1. Right-click within the green handles and select Replace with List Box.

  2. Click the Control icon in the Form Controls toolbar to open the Properties: List Box dialog.

  3. On the General tab, scroll down to the Dropdown selection. Change the No to Yes.

Frame50

  1. Click the Data tab.

Frame51

SELECT "Type", "Type" FROM "Payment Type"

Frame52

Note

What you wrote is called an SQL command. The words SELECT and FROM are written in capital letters because they are commands. When the command SELECT is used, it requires a field name within quotation marks and then the field’s alias, also within quotation marks. In this case, the field and its alias are the same. The FROM command requires the name of the table which contains the field. Single-word table names do not require quotation marks, but multiple-word table names do.

Frame53

Step 7: Change the Misc. Notes field.

We want the Misc. Notes control, which has a field type of Memo, to have a vertical scrollbar for additional text space if desired.

  1. Control+click the Misc. Notes field. The green handles should surround the field but not its label.

  2. Click the Control icon to open the Properties window (Figure 28).

  3. Scroll down to the Scrollbars setting. Change the selection from None to Vertical in this dropdown list.

Frame61

  1. Close the Properties window.

  2. Lengthen the Misc. Notes field by moving the cursor over the middle green handle at the bottom of the field and dragging down until the length is 6 cm (2.4 inches).

Step 8: Change labels and fields in the subform.

The subform is located at the bottom of the form. We want to widen the Date column, change the field in the PaymentType column to a list box, and change the label for the PaymentType column to two words.

To widen the Date column, move the mouse pointer over the dividing line between the Date and FuelCost columns. When the pointer changes shape, click and drag to move the divider to the right.

To change the PaymentType column:

  1. Right-click the label PaymentType and choose Replace with List box from the menu.

  2. Again right-click the label PaymentType to open the menu. Select Column to open the Properties dialog (Figure 29).

  3. In the Label box, change PaymentType to Payment Type.

  4. Click the Data tab.

  5. From the Type of list contents dropdown list, select Sql.

  6. Type the following exactly as it is written:

SELECT "Type", "Type" FROM "Payment Type"

  1. Close the Properties dialog.

Frame63

Step 9: Add headings to groups.

This step is easier to do if you have end-of-paragraph markers visible. Choose View Nonprinting Characters to turn them on.

  1. Make sure the cursor in in the upper left corner. If it is not, click in that corner to move it there.

  2. Press the Enter key to move the cursor down to the space between the Date field and the Breakfast field.

  3. Change the Apply Styles dropdown list from Default to Heading 2.

Frame46

  1. Use the spacebar to move the cursor to where you want the heading to start.

  2. Type the heading Meals.

  3. Use the spacebar to move the cursor to the center of snack area.

  4. Type the heading Snacks.

  5. Use the Enter key to move the cursor between the Supper control and the subform.

  6. Use the spacebar to move the cursor to the center of the subform.

  7. Type the heading Fuel Data.

Note

If you know how to use styles, you can open the Styles and Formatting window using F11. Right-clicking the Heading 2 paragraph style allows you to modify the appearance of all three headings. See Chapter 6 of the Writer Guide for details.

Step 10: Change the background of a form.

The background for a form can be a color, or a graphic (picture). You can use any of the colors in the Color Table at Tools Options LibreOffice Colors. If you know how to create custom colors, you can use them. You can also use a picture (graphic file) as the background.

If you choose a dark background, you many need to change many of the labels and headings so they can be seen.

  1. Select the labels of the top row of controls.

    1. Control+click the Date label.

    2. Control+shift+click the rest of the labels of the top row. The border will gradually grow to the right as you do this until all the labels are enclosed in it.

Frame65

    1. Click the Control icon in the Design Form toolbar to open the Properties dialog.

    2. Change the Background color selection from Default to Light cyan. (This is a dropdown list.) Close the Properties dialog.

  1. Select the other labels in the same way and then change their background color.

  2. To change the font color for the headings:

    1. Open Styles and Formatting using the F11 key. Make sure the Paragraph icon is selected at the left end of the icons in Error: Reference source not found.

Frame8

    1. Right-click the Heading 2 style to open a pop-up window and select Modify from this menu.

    2. In the Paragraph Style Heading 2 dialog, click the Font Effects tab. Change Font color from Automatic to Light Cyan.

    3. Click OK to close the dialog.

Tip

Learning how to use styles can be very helpful at times. By using styles, we changed the font color for all three headings at one time. There are other methods of changing the font color, but they require repeating the same steps for each heading.

  1. To add a graphic to the background:

  1. Right-click on the background and select Page from the pop-up menu.

  2. On the Page Style dialog, click the Background tab, change the As dropdown list from Color to Graphic.

  3. Click Browse to find and select the graphic file you want to use, and click Open. In the Type section of the Background tab, select Area.

  4. Click OK to close the dialog. The form should look like Figure 33.

Frame18

Step 11: Change the tab order.

The Tab key moves the cursor from field to field. This is much easier to do than to click each field to enter data into it. It also 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.

  1. Control+click the Date field.

  2. Click the Form Design icon in the Form Controls toolbar to open the Form Design toolbar (Figure 34), or choose View Toolbars Form Design to open this toolbar.

  3. Click the Activation Order icon (circled).

Frame72

  1. Rearrange the order of the fields in the Tab Order window.

    1. Find the txtMPayment listing near the bottom of the list and click it.

    2. Click the Move Up button until txtPayment is just below fmtMotel.

    3. Use the same two steps to put the fields in the same order as in Figure 35. Click OK.

Frame74

  1. Save and close the form.

  2. Save the database.

Creating forms and subforms in Design View

This method requires using the Form Controls and Form Design toolbars extensively. These techniques are beyond the scope of this document. Instructions for creating forms using Design view will be described in the Database Guide.

Accessing other data sources

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

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

  1. File New Database opens the Database Wizard window.

  2. Select Connect to an existing database. Click the arrow next to the Database type field and select the database type from the dropdown list. Click Next.

  3. Click Browse and select the database. Click Next.

  4. Accept the default settings: Register the database for me, and Open the database for editing. Click Finish. 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. Choose File New Database.

  2. Select Connect to an existing database. Select Spreadsheet as the Database type.

  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. If the spreadsheet requires a users name, enter it. If a password is also required, check its box. Click Next.

Note

Using this method of accessing a spreadsheet, you cannot change anything in the spreadsheet. 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, using Calc. After modifying the spreadsheet and saving it, you will see the changes in the database. If 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 LibreOffice2.x or later are in the *.odb (OpenDocument Base) format. Other programs can also produce databases in this format. Registered a *.odb database is simple:

  1. Choose Tools Options LibreOffice Base Databases.

  2. Under Registered databases, click New.

  3. Browse to where the database is located.

  4. Make sure the registered name is correct.

  5. Click OK.

Note

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

Using data sources in LibreOffice

Having registered the data source, 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

Open a document in Writer or Calc. To view the data sources available, press F4 or select View Data Sources from the pull-down menu. This brings up a list of registered databases, which will include Bibliography and any other database registered, such as the Automobile database created earlier in this chapter.

To view each database, click on the + to the left of the database’s name (see Figure 36). This brings up Tables and Queries. Click on the + next to Tables to view the individual tables created. Now click on a table to see all the records held in it.

Frame29

Editing data sources

Some data sources can be edited in the View Data Sources dialog. A spreadsheet can not. A record can be edited, added, or deleted.

The data is displayed on the right side of the screen. Click in a field to edit the value.

Beneath the records are five tiny buttons. The first four move backwards or forwards through the records, or to the beginning or end. The fifth button, with a small star, inserts a new record.

Frame3

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 to remove the selected row.

Frame69

Launching Base to work on data sources

You can launch LibreOffice Base at any time from the View Data Source pane. Just right-click on a database or the Tables or Queries icons and select Edit Database File. 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 source 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.

Tip

Choosing Tools Mail Merge Wizard or clicking on the Mail Merge icon on the View Data Source pane launches the Mail Merge wizard which steps you through creating a mail merge document. See Chapter 11 in the Writer Guide.

Writer documents

To insert a field from a table opened in the data source window into a Writer document, click on the field name (the gray square at the top of the field list) and, 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, to enter the cost of meals and who paid for them on a certain date of a vacation:

  1. Open the list of data sources (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 source window and drag it to the right of the word On. The result: On <Date>. If you have Field shadings turned on (View Field shading), <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:

Frame16

Adding data in table format is a little easier and takes perhaps fewer steps. Some of the steps will be quite similar.

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

  2. Control+Click the gray box to the left of each row of the data source that you want to be a row in your table when 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 40). (The Data to text icon is to the left of the Data to Fields icon in Figure 39.)

  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.

Frame60

  1. Select the settings for your table. Use the default settings as in Figure 40.

  2. Click OK. Save the document.

Calc spreadsheets

There are two ways to enter data in a Calc spreadsheet. One enters the data into the spreadsheet cells. The other creates records in the spreadsheet just like they are done in creating a form in a database. While you can directly access the data in the spreadsheet cells, you can only see the data in the records created in the spreadsheet.

Entering data directly to the spreadsheet cells uses the Data to Text icon as we did to make a table in a Writer document. But differences exist in these two situations.

The steps are straightforward.

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

  2. Use F4 to open the database source window and select the table whose data you want to use.

  3. 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 Source window open, your spreadsheet open, and the table you want to use selected.

  1. Click the gray box above the field name for the table's ID field.

  2. Drop and drag the gray box for the table's ID field to where you want the record to appear in the spreadsheet.

  3. Repeat until you have moved all of the fields you need to where you want them.

  4. Name and save the spreadsheet.

  5. Click a row of the table in the Data Source window.

  6. Drag the data in the ID field in the selected row onto the ID field in the spreadsheet. The Save icon should activate.

  7. Click the Edit File button to make the spreadsheet read only. Click Save when asked if you want to save the file.

  8. The Data Source window goes blank, the fields in the spreadsheet are populated with data from the row you selected, and the Form Navigation toolbar appears at the bottom of the spreadsheet.

Frame71

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

Entering data in a form

Records are used to organize the data we enter into a form. They also organize the data we enter into a subform.

Each type of field allows a different method to enter the data. In many if not all cases, more than one method can be used.

The first step to entering data in a form is to open it from the main database window (Figure 1).

  1. Click the Forms icon in the Database list.

  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 the arrow that opens the dropdown calendar (Figure 42). Then click the day the you want. Then press the Tab key to go to the Odometer field.

Frame70

The Odometer, Tolls, and Motel fields are numerical fields. Enter values directly into them, or use the up and down arrows. When the value is entered, use the Tab key to go to the next field.

The Motel’s Payment field is a dropdown list. If as in my case, all of the elements of the list start with different letters, typing the first letter selects the desired entry.

The rest of the fields of the main form are either numerical fields or dropdown lists until we reach the Misc. Notes field. It is a text field. Type anything you desire in this field just as you would any simple text editor.

Note

Since the Tab key is used to move between fields, it can not be used in a text field. All spacing must be done by the spacebar. Finally, the Enter key only acts as a line break to move the cursor to the next line. While the Enter key will move from non-text fields to non-text fields, it will not do so from a text field. Use the Tab key instead.

If we did not have a 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 on the second record.

Since we have a subform form, using the Tab key 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 FuelCost, FuelQuantity, and Odometer fields are numerical fields. The Payment field is a dropdown 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 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.

To move to another record when the form has a subform, click any of the fields of the main form. In this case, click the Date field of the main form. Then use the directional arrows at the bottom. There are four of them from left to right: First Record, Previous Record, Next Record, and Last Record (Figure 41). To the right of these arrows is the New Record icon.

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

Tip

The number in the Record box is the number of the record whose data is shown in the form.

If you know the number of the record you want, you can enter it into the record box and then press Enter to take you to that record.

Figure 43 is a record with data inserted in its fields.

Frame76

Creating queries

Queries are used to get specific information from a database. Query results are special tables within the database.

To demonstrate the use of queries, we will use two different methods:

Using the Wizard to create a query

Queries created by the wizard provide a list or lists of information based upon what one wants to know. It is possible to obtain a single answer or multiple answers, depending upon the circumstances.

In the main database window (Figure 1), click the Queries icon in the Databases section, then in the Tasks section, click Use Wizard to Create Query. The Query Wizard window opens (Figure 44). The information we want is what albums are by a certain musical group or individual (the album’s author). We can include when each album was bought.

Note

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 Vacation table used in a query has the name Vacation.Lunch.

Step 1: Select the fields.

  1. Select the CD-Collection table from the dropdown list of tables.

  2. Select fields from the CD-Collection table in the Available fields list.

    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.

    3. Click Next.

Tip

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

Frame19

Step 2: Select the sorting order.

Up to four fields can be used to sort the information of our query. A little simple logic helps at this point. Which field is most important?

In our query, the artist is most important. The album title is less important, and the date purchased is of least importance. Of course, if we were interested in what music we bought on a given day, the date purchased would be the most important.

Frame77

  1. Click the first Sort by dropdown list.

  1. Click the second Sort by dropdown list.

  1. Repeat this process for CD-Collection.DatePurchased.

  2. Click Next.

Step 3: Select the search conditions.

The search conditions allow us to 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 or not.

Note

These conditions apply to numbers, letters (using the 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 list and is equal to as the Condition.

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

Step 4: Select type of query.

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

Note

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.

We want the default settings. Click Next.

Step 8: Overview.

Name the query (suggestion: Query_Artists). To the right of this are two choices. Select Display Query. Click Finish.

Using the 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 creating two queries, with the first query used as part of the second query.

Step 1: Open the first query in Design View.

Click Create Query in Design View.

Step 2: Add tables.

Frame20

  1. Click Fuel to highlight it.

  2. Click Add. Click Close.

Tip

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

Frame21

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.

The table at the bottom of the query window should now have three columns.

Frame23

Step 4: Set the criterion for the query.

We want the query’s FuelID to begin with the numeral 1.

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

  2. Click the Run Query icon in the Query Design toolbar.

Frame22

Figure 50 contains the Fuel table with my entries and the query results based upon the Fuel table: the query results are in the right table.

Frame80

Step 5: Save and close the query.

Since this query contains the ending odometer reading for our calculations, name it End-Reading when saving it. Then close the query.

Step 6: Create the query to calculate the fuel economy.

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

  2. Add the Fuel table to the query just as you did in step 2: Add tables But, do not close the Add Tables window.

  3. 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.

Frame24

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

We are going to calculate the fuel economy. To do this we need the FuelQuantity and distance traveled. Since the FuelQuantity we want to use is at the ending odometer reading, we will use the End-Reading query to get it. We will also use the Odometer field from the Fuel table and End-Reading query.

Frame25

  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.

Frame81

Step 8: Enter the FuelID difference field.

We want the difference between the FuelID value of the Fuel table and FuelID value of the End-Reading query to equal one (1).

  1. Type "End-Reading".FuelID - Fuel.FuelID in the field to the right of the Odometer field of the Fuel Table.

Type the numeral 1 (one) in the Criterion cell of this column.

Frame82

  1. Calculate the distance traveled:

Frame83

  1. Calculate fuel economy:

Type ("End-Reading".Odometer – Fuel.Odometer)/"End-Reading".FuelQuantity in the next column to the right of the word Field.

Frame84

Note

When entering fields for these calculations, you must follow this format: table or query name followed by a period follow by 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 56.

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.

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

  1. Click the Run Query icon in the Design Query toolbar (Figure 49). The results are in Figure 57.

Frame85

Notice that not all of the last column label is visible because some of the labels are long. We can fix this problem by using an alias for many of the fields. The labels are replaced by their aliases.

  1. Add Aliases:

Type in the aliases as they are listed in Figure 58.

Frame87

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

Frame86

We really do not need the column showing the difference between the FuelID fields from the table and query, so we will hide it. While it will not be visible, it will still be used in the calculations.

  1. Hide a field that does not need to be seen.

Remove the check in the box of the Visible cell as in Figure 60.

Frame88

  1. Rerun the query (Figure 61).

Frame89

Step 10: Close, save, and name the query.

My suggestion for a name is Fuel Economy.

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 payments types.

Note

To fully use queries requires a knowledge of set operations (unions, intersections, and, or, complements, and any combinations of these). Having a copy of the Hsqldb User Guide, available from http://hsqldb.org/, is also extremely useful.

Creating reports

Reports provide information found in the database in a useful way. In this they are similar to queries. Reports are generated from the database’s tables or queries. They can contain all of the fields of the table or query or just a selected group of fields. Reports can be static or dynamic. Static reports contain the data in the selected fields at the time the report was created. Dynamic reports can be updated to show the latest data.

For example, a report on expenses for a vacation in the past should probably be a static report because it is based upon specific data that does not change. However, a report on the fuel data should probably be a dynamic report, because this report depends upon data that does change.

Caution

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. The report will be identical before and after you make the change.

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

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. So this report requires creating a query.

Creating a static report

We will create a report on vacation expenses. Certain questions need to be asked before creating the report.

The expenses for our vacation are motel, tolls, miscellaneous, breakfast, lunch, supper, snacks, and fuel. One possible report would simply list the totals of each of these expense groups. Another possible report would list the expense totals for each day of the vacation. A third possible report would 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.) The best way to create reports like these is to create the queries needed to gather the needed data, insert the query data into a spreadsheet, and use the necessary Calc functions on this data.

We will create two reports, one listing the expenses each day other than fuel and the second listing the fuel costs each day.

The fields we need for the first report from the Vacations table are: Date, Motel, Toll, Breakfast, Lunch, Supper, SnackCost, and Miscellaneous. This report will not require an additional query.

The second 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 the vacation.

Vacations table report

To create a new report.

  1. Click the Reports icon in the Database list in the Automobile – LibreOffice Base window (Figure 1).

  2. In the Tasks list, click Use Wizard to Create Report. The Report Wizard opens.

Step 1: Field selection

  1. Select Table: Vacations in the Tables or Queries dropdown 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 SnackCost. Click Next.

Frame90

Step 2: Labeling fields

Change any field labels you wish. We will shorten Miscellaneous to Misc. Click Next.

Step 3: Grouping

Since we are grouping by the date, use the > button to move the Date field to the Grouping list. Click Next.

Frame79

Step 4: Sort options

We do not want to do any additional sorting. Click Next.

Step 5: Choose layout

Use default settings for the layout. Click Next.

Step 6: Create report

Note

If you feel adventurous, try selecting some of the other layout choices. After selecting a choice, drag the Report Wizard window out of the way so that you can see what you have selected. (Move the cursor over the Title Bar of the window, and then drag and drop.)

Vacation fuel report

  1. Create a query containing only fuel bought on the days of the vacation.

    1. Open a query in Design View.

    2. Follow the steps for adding tables in “Add tables” on page 39 to add the Fuel table.

    3. In the Fuel table, double-click Date and FuelCost to enter them in the table at the bottom of the query.

    4. In the Criterion cell of the Date field, type the following: BETWEEN #5/25/2007# AND #5/26/2007#

Frame92

  1. Save, name, and close the query. (Suggestion: Vacation Fuel Purchases.)

    Tip

    When using dates in a query, enter them in numerical form MM/DD/YYYY or DD/MM/YYYY depending upon your language's default setting for dates (my default setting is MM/DD/YYYY).

    Each date must have a # before and after it. Hence, May 25, 2007 is written #05/25/2007# or #25/5/2007# depending upon your language’s default setting for dates.

  2. Open a new report.

Note

When a new report is opened in this way, the query used to open it is automatically selected in the Tables or queries dropdown list in the Report Wizard.

  1. Create the report.

Use >> to move both fields from the Available fields list to the Fields in report list. Click Next.

  1. Label fields.

Add a space to FuelCost to make it Fuel Cost (two words). Click Next.

  1. Group fields.

Click Date to highlight it. Use > to move the Date field to the Groupings list. Click Next.

  1. Choose layout.

We will be making no changes in the layout. Click Next.

  1. Create report (final settings).

Creating a dynamic report

Now we will create a report with some statistics on our fuel consumption. To do this, we have to modify two queries: End-Reading and Fuel Economy. We will be adding the FuelCost field to the End-Reading query. Then we will add the FuelCost field from the End-Reading query to the Fuel Economy query.

Tip

When opening a query to edit it, it might appear as in Figure 65. If you move your cursor over the black line (circled), it becomes a double headed arrow. Drag it to a lower position.

Frame34

  1. Add the FuelCost field to the End-Reading query:

    1. Right-click the End-Reading query and select Edit from the pop-up menu.

    2. In the Fuel table list, double-click to add FuelCost to the bottom table (Figure 66).

    3. Save and close the query.

Frame27

  1. Add the FuelCost field from the End-Reading query to the Fuel Economy query:

  1. Add a calculation field to the right of the FuelCost field.

Note

If you use the metric system, cost per km is the appropriate alias.

  1. Save and close the query.

  2. Open a new report.

Right-click the Fuel Economy query and select Report Wizard.

  1. Select fields.

Move all the fields from the Available fields to the Fields in report list. Use the >> to do so. Click Next.

  1. Label fields.

Change FuelCost to Fuel Cost by placing a space between the words. Click Next.

  1. Group fields.

Use > to move the Date field to the Groupings list. Click Next.

  1. Sort options: the wizard skipped this one.

  2. Choose layout: accept the default. Click Next.

  3. Create the report.

Modifying a report

At the end of the last section, we left the Fuel Statistics report open in the edit mode (Figure 67). We will be working on that report. These same steps can be used with any report that you open for editing.

Frame93

The Author is the name you listed in Tools Options LibreOffice User Data. The date is not correct. The columns need to be moved to the left to give a better appearance. None of the numbers are correct, but their only purpose is to show the number of decimal places.

Step 1: Change the date.

  1. Click to the right of the date (4/26/20) so that the cursor is next to the field. Use the Backspace key to erase the date.

  2. Insert Fields Date. This places today’s date where the original date was.

  3. Change the date formatting:

    1. Double-click the date field you just inserted. The Edit Fields: Document window opens.

Frame94

    1. Since this is a dynamic report, change the Select field from Date (fixed) to Date.

    2. Change the Format to what you desire. (I use the Friday, December 31, 1999 choice.) Click OK.

Step 2: Change the column widths.

The column widths can be changed by moving the cursor over the right border of each column so that it becomes a double-headed arrow. Then drag and drop it to where you want it. This has to be done for each column in each table in the report. This can also be done with the last column on the right even though there is no black border. It should now look something like Figure 69.

Frame95

Step 3: Change the number formatting in the cells.

The fuel quantity should have three decimal places. The Begin, End, and Distance should have one decimal place. Fuel Cost should be currency and have two decimal places, and Cost per mile should have three decimal places.

  1. Right-click the cell below Quantity and select Number format. (The cell is circled in Figure 69.)

  2. In the Options section (Figure 70),

    1. Change the number of Decimal places to 3.

    2. Click the green checkmark. Click OK.

Frame96

  1. Change the Cost per mile field.

    1. Right-click in the cell below Fuel Cost.

    2. Select Number Format.

    3. In the Category list, select Currency. Click OK.

  1. Change the Fuel Cost field.

    1. Right-click in the cell below Cost per mile.

    2. Select Number Format.

    3. In the Category list, select Currency.

    4. In the Option section:

    1. Click OK.

Step 4: Save and close the report.

Double-click the report. It should now look like Figure 71.

Frame98

More ways to create reports

An extension is available to assist in report creation. Report Builder creates stylish, complex database reports. You can define group and page headers, group and page footers, and calculation fields. It is installed by default with Libreoffice.

To install this extension, follow these steps:

  1. Choose Tools Extension Manager from the menu bar. In the Extension Manager dialog, click Get more extensions online....

  2. The LibreOffice extensions page opens in your browser. Find and select the extension you want and follow the prompts to install it. During installation, you will be asked to accept a license agreement.

  3. When the installation is complete, the extension is listed in the Extension Manager dialog.

For more about extensions, see Chapter 14, Customizing LibreOffice.