Getting Started Guide
 

Chapter 8  
Getting Started with Base

Relational Databases in LibreOffice

Copyright

This document is Copyright © 2017 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 (http://www.gnu.org/licenses/gpl.html), version 3 or later, or the Creative Commons Attribution License (http://creativecommons.org/licenses/by/4.0/), version 4.0 or later.

All trademarks within this guide belong to their legitimate owners.

Contributors

Dan Lewis

Jean Hollis Weber

Hazel Russman

Ron Faile Jr.

Olivier Hallot

 

Feedback

Please direct any comments or suggestions about this document to the Documentation Team’s mailing list: documentation@global.libreoffice.org

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

Acknowledgments

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

Dan Lewis

Magnus Adielsson

JiHui Choi

Iain Roberts

Jean Hollis Weber

 

Publication date and software version

Published 16 February 2017. Based on LibreOffice 5.2.

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 or Linux

Mac equivalent

Effect

Tools > Options menu selection

LibreOffice > Preferences

Access setup options   

Right-click

Control+click and/or right-click depending on computer setup

Open a context menu

Ctrl (Control)

⌘ (Command)

Used with other keys

F5

Shift+⌘+F5

Open the Navigator

F11

⌘+T

Open the Styles and Formatting window

 

Contents

Copyright

Contributors

Feedback

Acknowledgments

Publication date and software version

Note for Mac users

Introduction

Planning a database

Creating a new database

Creating database tables

Using the Wizard to create a table

Creating a table by copying an existing table

Creating tables in Design View

Defining relationships

Creating a database form

Using the Wizard to create a form

Modifying a form

Creating forms and sub forms in Design View

Entering data in a form

Quickly populate a table with data from a spreadsheet

Creating queries

Using the Wizard to create a query

Using the Design View to create a query

Creating reports

Creating a report: Example

Report wizard vs Report Design View

Vacations table report

Report Builder: another way to create reports

Accessing other data sources

Accessing a spreadsheet as a data source

Registering *.odb databases

Using data sources in LibreOffice

Viewing data sources

Editing data sources

Launching Base to work on data sources

Using data sources in Writer and Calc

Writer documents

Calc spreadsheets

 

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.

This chapter covers creating a database, showing what is contained in a database and how the different parts are used by LibreOffice.

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.

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 within it. Forms are for data entry into the fields of one or more tables which have been 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 from the fields of a query into a document according to your requirements.

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

To use Base, you need to use a Java Runtime Environment (JRE). Please go to Tools > Options > LibreOffice > Advanced to select a JRE from those installed on your computer.

If a JRE is not already installed, you will need to download and install one. For Windows, you need to get Java from www.java.com. For Linux, you can download it from the same website or you can use openjdk-7-jre, available from the repository of your Linux version. Mac OS X users can install a JRE from Apple Inc.

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 a database works and how to to use it, we will create one for automobile expenses.

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 write the answers later. 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 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). 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.

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, an identifier unique to each record. In some tables, a suitable 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, select 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 drop-down 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.

In Writer, the F4 key opens and closes the Data Source window containing the list of registered databases. In Calc, press Ctrl+Shift+F4 to open the Data Source window. If a database is not registered, this window will not contain it, so you cannot access the database in Writer or Calc.

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

 

Figure 1: Creating database tables

 

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.

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. This makes it part of the database in memory. But it is only when you save the database file that the table is written to disk.

Database files in Open Document Format are stored with the *.odb extension. This file format is actually a container of 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 PostgresSQL database server in your network.

Creating database tables

In a database, a table stores information in 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 must have at least one table and may have several.

Each field of a table contains information of a single type. For example, the Phone field of an address book would only contain phone numbers. Similarly, a price list table could 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, 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 merely 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.

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 and Employees in the Business category to select the fields we need.

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

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

  3. 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. 4)  Selected Fields from another sample table. Click Business as the Category. Select Employees from the drop down 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. 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. 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. 7)  Click Next

 

Figure 2: Selecting fields for the table

 

Step 2: Set field types and formats

 

Figure 3: Changing field types

 

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.

If any of these fields requires a mandatory entry, set Entry required to Yes. A blank field 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.

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 VARCHAR as the field format for text fields. This format uses only the actual number of characters in a field up to the limit set, so a field containing 20 characters will occupy only 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.

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. 1)  Create a primary key should be checked. 

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

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

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

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. 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. 2)  Leave the option Insert data immediately checked. 

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

  4. 4)  Click the Save button at the top of the main 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. 1)  Click on the Tables icon in the Database pane to see the existing tables. 

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

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

  4. 4)  Change the table name to CD-Jazz and click Next

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

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

  7. 7)  Click the Save button at the top of the main database window. 

Once tables have been created using the wizard, and data has been entered, editing a table should be very limited. You can add or delete fields, but adding a field requires you to enter the data for that one field for every existing record with 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.

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. 1)  Click Create Table in Design View (which opens the Table Design dialog). 

  2. 2)  FuelID field: Type FuelID as the first Field Name. Press the Tab key to move to the Field Type column. Select Integer [INTEGER] as the Field Type from the drop down list. (The default setting is Text [VARCHAR].) 

A shortcut for selecting from the Field Type drop down 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.

  1. a)  Change the Field Properties in the bottom section.
    Change AutoValue from No to Yes

  2. b)  Set FuelID as the Primary key.
    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 4

 

Figure 4: Defining the primary key field

 

Certain of the Integer filed types (Integer and BigInt for example) have an AutoValue Field Property. When using one of these field types, your selection of Yes for the AutoValue value automatically makes the 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 only one purpose: to identify each record uniquely. Any name can be used for this field. We have used FuelID for convenience, so we know to which table it belongs.

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

  1. a)  Type the next field name in the Field Name column. 

  2. b)  Select the Field Type for each field. 

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

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

 

Figure 5: Changing field properties

 
  1. e)  To access additional formatting options, click the ellipse button (…) to the right of the Format example field. 

Description can be any of the categories listed in the figure below, or can be left blank.

 

Figure 6: Format example options

 
  1. 4)  To save and close the table, select File > Save. Name the table Fuel. Close the Fuel table. 

  2. 5)  In the main database window, click the Save button. 

 

Figure 7: Fields in Vacations table

 

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

Making Date the primary key has to be done in a different way because this field’s field type is Date, not Integer.

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

  2. 2)  Select Primary Key in the context menu. 

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

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

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

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

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

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

  2. 4)  Define the relationship between the Vacations and Fuel tables:  click the New Relation icon. This opens the Relations window (Figure 9). Our two tables are listed in the Tables involved section. 

  1. a)  In the Fields involved section, click the drop-down list under the Fuel label. 

  2. b)  Select Date from the Fuel table list. 

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

  4. d)  Select Date from the Vacations table list. It should now look like Figure 9

  5. e)  Modify the Update options and Delete options section of the Relation window (Figure 10). 

  1. i)  Select Update cascade

  2. ii)  Select Delete cascade

 

Figure 8: Designation for a 1:n relationship

 
 

Figure 9: Selected fields in a relationship

 
The primary key can contain more than one field. (Its foreign key1 will contain the same number of fields.) If this were the case in Figure 9, 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.
 

Figure 10: Update options and Delete options section

 
  1. f)  Click OK

  2. g)  Save the Relation Design dialog if the Save button is active. 

  3. h)  Close the Relation Design dialog. 

  4. i)  Click the Save button at the top of the main database 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 which has been modified. It also permits you to delete a field from the table without causing inconsistencies.

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.

A simple form consists of the fields from a table (Figure 11). More complex forms can contain much more, including additional text, graphics, selection boxes, and many other elements. Figure 12 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 choice of options. It saves you from having to type data by hand, and ensures that invalid options are not entered.

 

Figure 11: Fields of a simple form

 
 

Figure 12: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, we first need to create a small, separate table containing the options. This is then linked to the corresponding field in the form. The topic is dealt with in detail in the Base User Guide and will not be pursued further here.

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 13). Simple forms require only some of these steps, while more complex forms may use all of them.

Step 1: Select fields

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

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

 

Figure 13: Form Wizard steps

 

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 need to be done in step 4.

  1. 1)  Click the box labeled Add Subform. 

  2. 2)  Click Subform based upon existing relation. 

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

 

Figure 14: Adding a subform

 

Step 3: Add subform fields

This step is similar to step 1. The only difference is that not all of the fields will be used in the subform.

  1. 1)  Fuel is preselected under Tables or queries

  2. 2)  Use the >> button to move all the fields to the right. 

  3. 3)  Click the FuelID field to highlight it. 

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

  5. 5)  Click Next

 

Figure 15: Selecting fields of a sub form

 

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.

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.

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.

Step 5: Arrange controls

A 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 relative to 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. 1)  Arrangement of the main form: Click the second icon (Columnar—Labels on top). The labels will be placed above their field. 

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

 

Figure 16: Arrange controls

 

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. 1)  Select the color you want in the Apply Styles list. (I chose the beige which is Orange 4 in the Color table.) 

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

  3. 3)  Click Next

Step 8: Set name

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

  2. 2)  Click Modify the form

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

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

 

Figure 17: A selected control

 
 

Figure 18: Selecting a field of a control

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

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

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

 

Figure 19: Selecting multiple controls

 

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

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. 1)  Ctrl+click the Date field to select it. 

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

  3. 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. 4)  Click the Control icon in the Form Controls toolbar. If it is not visible, select View > Toolbars > Form Controls. The Properties: Date Field window opens. Each line contains a property of the field. 

 

Figure 20: Form Controls toolbar

 

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 to be shortened before the controls are moved.

 

Figure 21: Selecting a field

 
  1. 1)  Ctrl+click the BPayment field. 

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

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

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

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.

Step 3: Move the controls to group them by category

We want to move the controls so that they look like Figure 22 (a and b).

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

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

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

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

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

 

Figure 22a: Positioning of controls (left side of form)

 
 

Figure 22b: Positioning of controls (right side of form)

 

Step 4: Change the label wording

Field names are required to be single words. 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.

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

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

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

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 fields of 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. 1)  Right-click Breakfast and select Position and Size. On the Position and Size dialog, change Width to 2 cm. 

  2. 2)  Repeat for the other listed controls, using 3 cm for MiscPayment. 

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 exactly the 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 will have to move each of them to where you really want them.

To open the Properties dialog, 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 Ctrl+Z to undo any mistakes you may make. Detailed instructions on how to use the Properties window are given in the Base Guide.

Step 6: Change the Misc. Notes field

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

 

Figure 23: Scroll bar selections in the Properties window

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

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

  3. 3)  Scroll down to the Text type property with single-line as the default value. 

  1. 4)  Scroll down to the Scrollbars setting. Change the selection from None to Vertical in this list. 

  2. 5)  Close the Properties window. (Esc key) 

  3. 6)  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 7: Change labels and fields in the subform

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

 

Figure 24: Properties

 

Step 8: Add headings to groups

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

 

Figure 25: Apply Styles list

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

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

  3. 3)  Change the Apply Styles drop down list from Default to Heading 2

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

  5. 5)  Type the heading Meals

  6. 6)  Use the spacebar to move the cursor to the center of snack area. 

  7. 7)  Type the heading Snacks

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

  9. 9)  Use the spacebar to move the cursor to the center of the subform. 

  10. 10)  Change the Apply Styles drop down list from Default to Heading 2

  11. 11)   Type the heading Fuel Data

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 9: 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.

To add a color to the form background:

  1. 1)  Right-click the form to open a context menu. 

  2. 2)  Select Page

  3. 3)  Make sure the Area tab has been selected. (It should have a white background while the other tabs have a gray one.) 

 

Figure 26: Page style dialog

 
  1. 4)  Select Color from the drop-down list below Fill. (List contains None, Color, Gradient, Hatching, and Bitmap.) 

  2. 5)  Scroll down through the colors and then Click Cyan 2

  3. 6)  Click Apply to see the effects of adding the color. OR, click OK to close the dialog. 

To create other form backgrounds:

  1. 1)  Perform steps 1-3 for adding color to the background. 

  2. 2)  Select the type of background from the drop-down list below Fill

  3. 3)  Scroll down to background you want from the list of backgrounds based upon your choice in 2) above. 

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

  2. 5)  Change if necessary. 

  3. 6)  Click OK to select your final decision. 

If you selected Bitmap and Sky from the Bitmap list, the form should look like Figure 27.

 

Figure 27: Finished form

 

On the left side of the form window are four icons (Figure 28). You can use the Gallery as a source for backgrounds. Click it and click Backgrounds (Figure 29). Right-click the background you want to use. Select Insert as Background > Page.

 

Figure 28: Often used items

 
 

Figure 29: Open Gallery

 

 

You can create custom Gradients, Hatchings, and Bitmaps using the Draw component of LibreOffice. See the Draw Guide for information how to do this.

Step 10: 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.

 

Figure 30: Form Design toolbar with Activation Order icon circled

 
  1. 1)  Ctrl+click the Date field. 

  2. 2)  Select View > Toolbar > Form Design to open this toolbar. 

  3. 3)  Click the Activation Order icon (circled). 

  4. 4)  Rearrange the order of the fields in the Tab Order window. 

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

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

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

  1. 5)  Save and close the form. 

  2. 6)  Save the database. 

Creating forms and sub forms 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.

 

Figure 31: Tab order for the main form

 

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.

Different types of fields allow different methods of data entry. In many 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. 1)  Click the Forms icon in the Database list. 

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

  3. 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 drop down calendar (Figure 32). Then click the day the you want. Then press the Tab key to go to the Odometer field.

 

Figure 32: Calendar drop down

 

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

The Motel’s Payment field is a drop-down 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 numeric fields or drop-down 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.

Since the Tab key is used to move between fields, it cannot be used in a text field. All spacing must be done by the spacebar. Furthermore in text fields, the Enter key acts only as a line break to move the cursor to the next line. While the Enter key will move between 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, 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 numeric fields. The Payment 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 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; from left to right: First Record, Previous Record, Next Record, and Last Record. To the right of these arrows is the New Record icon.

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

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 33 is a record with data inserted in its fields.

 

Figure 33: Sample record of the Vacation form and sub form

 

Quickly populate a table with data from a spreadsheet

If you have data in a spreadsheet document that represents the layout of a database table, and you want to copy it into a table of your database, then it is possible to load the sheet contents quickly by dragging and dropping the sheet into a database table:

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

  2. 2)  Open the spreadsheet in LibreOffice Calc. Select the sheet you want to insert in the Gdatabase. 

  3. 3)  Place the two windows side by side on your desktop. 

  4. 4)  Drag the sheet tab in the bottom of the Calc window into the table list of the database file. The mouse pointer shows a square with a + sign. 

  5. 5)  Drop the sheet by releasing the mouse button. 

  6. 6)  The Copy table wizard appears to help you migrate the content to the database table. 

  7. 7)  On the first page of the wizard, select the options of the copy operation and name the database table. Each option is explained in the Help (F1). 

  8. 8)  On the second page of the wizard, select the sheet columns you want to copy into the table. 

  9. 9)  On the third page of the wizard, define the data type of each column of your table. 

  10. 10)  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

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 Database section, then in the Tasks section, click Use Wizard to Create Query. The Query Wizard window opens (Figure 34). 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.

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.

 

Figure 34: First page of the Query Wizard

 

Step 1: Select the fields

  1. 1)  Select the CD-Collection table from the drop down list of tables. 

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

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

  2. b)  Move the AlbumTitle and DatePurchased fields in the same manner. 

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

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.

 

Figure 35: Sorting order page

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

  1. a)  Click CD-Collection.Artist to select it. 

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

  1. 2)  Click the second Sort by drop down list. 

  1. 3)  Repeat this process for CD-Collection.DatePurchased. 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 results or not.

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

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

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

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.  Instead, aliases can be made containing two words each (Album Title and Date Purchased, respectively).

  1. 1)  Change AlbumTitle to Album Title. 

  2. 2)  Change DatePurchased to Date Purchased. 

  3. 3)  Click Next

Step 8: Overview

  1. 1)  Make sure that the query conditions listed in the Overview list are the ones you wanted. (There are only two that you need to check.) 

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

  3. 3)  Then use the Next button to move to step 8. 

  4. 4)  Name the query (suggestion: Query_Artists).  

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

  6. 6)  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

Step 2: Add tables

  1. 1)  Click Fuel to highlight it. 

  2. 2)  Click Add

  3. 3)  Click Close

 

Figure 36: Add Tables or Query dialog

 

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

 

Figure 37: Fuel table in query

 

Step 3: Add fields to the table at the bottom

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

  2. 2)  Double-click the Odometer field. 

  3. 3)  Double-click the FuelQuantity field. 

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

 

Figure 38: Query table

 

Step 4: Set the criterion for the query

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

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

  2. 2)  Click the Run Query icon in the Query Design toolbar. (Circled in Red.) 

 

Figure 39: Query Design toolbar

 

Figure 40 contains the Fuel table with my entries. The query results based upon the Fuel table are in Figure 41.

 

Figure 40: Fuel table

 
 

Figure 41: Query of Fuel table

 

Step 5: Save and close the query

Since this query contains the final odometer reading for our calculations, 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. 1)  Click Create Query in Design View to open a new query. 

  2. 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. 3)  Add the End-Reading query to this query. 

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

  2. b)  Click End-Reading. 

  3. c)  Click Add, and then click Close

 

Figure 42: Selecting queries to add to another query

 

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 the final odometer reading, 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 queries.

 

Figure 43: Tables in this query

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

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

  3. 3)  Double-click Odometer in the Fuel table. 

 

Figure 44: Added fields to the query

 

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. 1)  Type "End-Reading"."FuelID" - "Fuel"."FuelID" in the field to the right of the Odometer field of the Fuel Table. (Figure 45

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

  3. 3)  Leave the Visible cell of this column unchecked. 

  4. 4)  Calculate the distance traveled: 

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

  2. 6)  Type Fuel Economy as the alias. 

 

Figure 45: Typing in calculation of fields

 
 

Figure 46: Field for distance traveled calculations

 
 

Figure 47: Fuel economy calculation field

 

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

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.

 

Figure 48: Result of running the fuel economy query

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

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

  1. 2)  Add Aliases: 

Type the aliases as they are listed in Figure 49.

 

Figure 49: Query table with aliases added

 
  1. 3)  Run the query again. The results are in Figure 50

 

Figure 50: Query run with aliases

 

Step 10: Close, save, and name the query

My suggestion for a name is Fuel Economy.

  1. 1)  Click the Save icon. 

  2. 2)  Name the query. 

  3. 3)  Close the query. 

  4. 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 HSQLDB User Guide, available from http://hsqldb.org/doc/guide/, is also extremely useful.

Creating reports

Reports provide information found in the database arranged in a useful way. In this respect, they are similar to queries. They are different in that they are designed to be distributed to people. Queries are only designed to answer a question about 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 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 or view. Then you can create a report from this.

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

Creating a report: Example

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 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 create two reports, one listing 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, Toll, Breakfast, Lunch, Supper, SnackCost, and Miscellaneous. This report only requires the Vacation 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 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 vacation periods.

Report wizard vs Report Design View

  1. 1)  When you open the Report Wizard, the Report Builder also opens. 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. 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

To create a new report.

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

  2. 2)  In the Tasks list, click Use Wizard to Create Report. The Report Wizard and then the Report Builder opens. 

Step 1: Field selection

  1. 1)  Select Table: Vacations in the Tables or Queries drop down list. 

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

 

Figure 51: Adding fields to a report

 

Step 2: Labeling fields

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

 

Figure 52: Giving aliases to fields

 
  1. 1)  Shorten Miscellaneous to Misc. 

  2. 2)  Add a space to separate SnackCost into Snack Cost. 

  3. 3)  Click Next

Step 3: Grouping

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

 

Figure 53: Selecting fields for grouping data

 

Step 4: Sort options

We do not want to do any additional sorting.

Step 5: Choose layout

Use Columnar, three columns for the layout.

  1. 1)  Select Columnar, three columns for the Layout of data. 

  2. 2)  Layout of headers and footers has no possible selections. 

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

  4. 4)  Click Next

 

Figure 54: Report Builder template determined by the Report Wizard

 

Step 6: Create report

  1. 1)  Label the report: Vacation Expenses. 

  2. 2)  Select Dynamic report

  3. 3)  Select Create report now

  4. 4)  Click Finish

The report (Figure 55) has been created, but it needs some editing. The date could be formatted better, and all the numbers need to be formatted as currency. It could use 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.

 

Figure 55: Report without modifications

 

Report Builder: another way to create reports

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

When we used the Report Wizard, we created a template in Report Builder for our report. If we edit our report, we open Report Builder 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 Handbook.

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. 1)  File > New > Database opens the Database Wizard window. 

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

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

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

One of the choices available when you select Connect to an existing database is LDAP Address Book. Beginning with LibreOffice 4.0.0, this option no longer works.

Accessing a spreadsheet as a data source

Accessing a spreadsheet is similar to accessing other databases:

  1. 1)  Choose File > New > Database

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

  3. 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. 4)  If the spreadsheet requires a user’s name, enter it. If a password is also required, check its box. Click Next

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 LibreOffice are in the *.odb (OpenDocument Base) format. Other programs can also produce databases in this format. Registering a *.odb database is simple:

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

  2. 2)  Under Registered databases, click New

  3. 3)  Browse to where the database is located. 

  4. 4)  Make sure the registered name is correct. 

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

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 arrow to the left of the database’s name (see Figure 56). This brings up Queries and Tables. 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.
 
 

Figure 56: Databases

 

Editing data sources

Some data sources (but not spreadsheets) can be edited in the Data View 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 Source 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 tiny 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.

 

Figure 57: View Data Sources navigation buttons

 

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.

 

Figure 58: Deleting a row in the Data View window

 

Launching Base to work on data sources

You can launch LibreOffice Base at any time from the Data Source window. Just right-click on a database or its 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.

 

Figure 59: Toolbar for the Data Sources window

 

Choosing Tools > Mail Merge Wizard or clicking on the Mail Merge icon (circled in red) in the Data Sources window 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. 1)  Open the list of data sources (F4) and select the Vacations table in the Automobile database. 

  2. 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. 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. 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. 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. 6)  In the same way, fill in the rest of the fields in the sentence. 

  1. 7)  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. 8)  Add data to the fields of the sentence: 

 

Figure 60: Selected row in data source window

 

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

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

  2. 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. 3)  Click the Data to text icon to open the Insert Database Columns dialog (Figure 61). (The Data to text icon is to the left of the Data to Fields icon in Figure 60.) 

 

Figure 61: Insert Database Columns dialog

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

  1. 5)  To start over, click the double arrow pointing to the left. 

  2. 6)  Select the settings for your table. Use the default settings as in Figure 61

  3. 7)  Click OK. Save the document. 

Calc spreadsheets

There are two ways to transfer data into a Calc spreadsheet. One enters the data into the spreadsheet cells. The other creates 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.

Entering data directly to the spreadsheet cells uses the Data to Text icon as when making a table in a Writer document. But there are certain differences.

The steps are straightforward.

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

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

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

  1. 4)  Click the Data to text icon to insert the data into the spreadsheet cells. 

  2. 5)  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. 1)  Click the gray box containing the field name for the table’s ID field (the column header). 

  2. 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. 3)  Repeat until you have moved all of the fields you need to where you want them. 

  4. 4)  Name and save the spreadsheet. 

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

  6. 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. 7)   Click the Edit File button to make the spreadsheet read-only. Click Save when asked if you want to save the file. 

          1. The Data Sources 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.

  8. 8)  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. 

 

Figure 62: Navigation arrows of a form

1A field in a table that stores values of the primary key of records in another table.

Impressum (Legal Info) | Privacy Policy | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.