Getting Started Guide 24.8
Chapter 8,
Getting Started with Base
Relational databases in LibreOffice
This document is Copyright © 2024 by the LibreOffice Documentation Team. Contributors are listed below. You may distribute it and/or modify it under the terms of either the GNU General Public License (https://www.gnu.org/licenses/gpl.html), version 3 or later, or the Creative Commons Attribution License (https://creativecommons.org/licenses/by/4.0/), version 4.0 or later.
All trademarks within this guide belong to their legitimate owners.
Please direct any comments or suggestions about this document to the Documentation Team’s forum at https://community.documentfoundation.org/c/documentation/loguides/ (registration is required) or send an email to: loguides@community.documentfoundation.org.
Note
Everything you send to a mailing list or forum, including your email address and any other personal information that is written in the message, is publicly archived and cannot be deleted.
Published August 2024. Based on LibreOffice 24.8 Community.
Other versions of LibreOffice may differ in appearance and functionality.
Some keystrokes and menu items are different in the macOS version of LibreOffice, and the most important differences between macOS and the Window/Linux versions are listed in the table below. For a detailed list, refer to LibreOffice’s online help.
Windows or Linux |
macOS equivalent |
Result |
Tools > Options |
LibreOffice > Preferences |
Access setup options |
Right-click |
Control+click or right-click depending on computer setup |
Open a context menu |
Ctrl (Control) |
⌘ (Command) |
Used with other keys |
Alt |
⌥ (Option) or Alt |
Used with other keys |
F11 |
⌘+T |
Open the Sidebar Styles deck |
A data source, or database is an organized collection of information that can be accessed or managed by software. When you manage a database in LibreOffice, you will do it through the Base module.
For example, a list of names and addresses could be turned into a database that could help you create a mail merge letter, or a business stock list could be a data source managed through LibreOffice.
Base serves as a database front end that can create and store embedded databases (Figure 1) or connect to external databases (Figure 2).
Figure 1: Local database
Figure 2: Remote database server
This chapter explains how Base can create databases and reads what is contained in a database, and it also describes how the different parts of the module are used.
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, a spreadsheet or text document holding data, or an address book.
Tables
Forms
Queries
Reports
Caution
Some features of the Base module, such as the HSQLDB database engine, and the ability to create reports) need the Java Runtime Environment (JRE). The JRE is not installed in Base by default. Please go to Tools > Options > LibreOffice > Advanced to select a JRE from those installed on your computer.
Base can create both flat and relational databases, and it can create databases where fields have relationships with each other.
Flat databases
Relational databases
For example: Consider a database that lists a CD music collection. It will contain fields for the names of the artists and a field that holds the CD title. While there is an obvious relationship between the artist and the CD they have created, the CD collection may contain more than one CD by the same artist. These are one-to-many relationships: many artists are linked to more than one CD, so this database contains multiple one-to-many relationships. However, the record company database can contain the names of the artist plus other fields like artist agent. The relationship between artist and agent is one to one: only one agent for each artist.
Tip
If you are acquainted with mathematical sets, a relational database can easily be explained in terms of sets: elements, subsets, unions, and intersections. The fields of a database are the elements and the tables are subsets. Relationships within databases are defined in terms of unions and intersections of the subsets (tables).
Databases exist to organize data for easy and accurate retrieval. Database tables are much stricter to define and create, therefore planning in advance is necessary. In this chapter, we will show you how to plan a database. In our example, we will create a database that contains automobile expenses.
The first step in creating a database is to define it by determining what fields are needed. In this case, we will ask a series of questions about the data:
What are the user output requirements?
What input data is required for the output?
What fields fit the fuel purchases area?
What fields fit the maintenance area?
What fields fit the vacations area?
What fields fit into the food category?
What are the fields that are common to more than one area?
How will these fields be used to get the required information?
Tip
After the fields for the database tables have been listed, we will need a field for the primary key, which is an identifier that is unique to each record. Some of the tables already have a suitable field for the primary key but an additional field for the primary key must be created for other fields (like payment type).
If a table does not have a primary key field, any data that is added, deleted, or modified must be done with the Execute SQL Statement dialog (Tools > SQL…).
Figure 3: LibreOffice Base window
To create a new database, you should start with the Database Wizard. The wizard can be started by doing one of the following:
Selecting File > New > Database on the Menu bar
Clicking the arrow next to the New icon on the Standard toolbar and selecting Database in the drop-down menu.
Clicking the Base Database button in the Create area of the LibreOffice Start Center.
If you are already working in Base, you can also press Ctrl+N.
Once the Wizard is started, the first page of the wizard appears (Figure 4). Select the HSQLDB Embedded entry is selected in the Embedded database drop-down menu, select Create a new database, then click Next. (Look at the Base Guide for information about using alternative embedded database engines or existing external databases).
Figure 4: Creating a new database
Next, the second page of the Database Wizard appears (Figure 5) with two questions. For the first question, make sure the database is registered. And for the second question, click the Open the database for editing checkbox. Click Finish. The Database Wizard will prompt you to select a location to name and save the database.
Figure 5: Registering the new database
Note
To open and close the Data Sources window that contains the list of registered databases Writer and Calc, select View > Data Sources or press Ctrl+Shift+F4. If a database is not registered, they will not appear in this window. and you cannot access it in Writer or Calc if you need to use data from it.
Save this new database with the name Automobile and the Automobile.odb – LibreOffice Base window appears (Figure 3).
Tip
Every time the Automobile database is opened, the Automobile.odb – LibreOffice Base window opens. Changes can then be made to the database. The title for this window is always <database name> – LibreOffice Base.
Caution
When you create a database, you should save your work regularly. This means you should save the whole database as well.
For example, when you create your first table, you must save it before you can close it. This makes it part of the database in memory. But only when you save the database file that the table is written to disk.
Note
All database files in Open Document Format are stored with the *.odb extension. Files in that format contain all elements of the database, including forms, reports, tables, and the data itself. The same format can also store a connection to an external database server instead of the local data, for example, to access a MySQL or PostgreSQL database server in your network.
Database tables store information in a group of things called fields. For example, a table will use fields to hold the data in an address book, a stock list, a phone book, or a price list. A database must have at least one table and will probably have have multiple tables.
Each field can hold data of a single type. For example, an address book that contains phone numbers would contain phone numbers in fields that are the Phone type. Similarly, a price list table might contain two fields: Name and Price. The Name field would contain the names of the items; the Price field would contain the amount of each item.
To work with tables, click the Tables icon in the Database list, select View > Tables, or press Alt+A. The tasks that you can perform on a table are in the Tasks list (see Figure 1).
This Base wizard allows you to create two types of tables: business and personal. Each category contains sample tables from which to choose. Each table has a list of available fields.
The Table Wizard provides sample tables as a starting point to create your own table.
Click Use Wizard to Create Table. This opens the Table Wizard (Figure 7).
Figure 6: Creating a table using Wizard
Step 1: Select fields
First, we will use the wizard to generate a table in the Personal category. We will borrow a table from the supplied CD-Collection:
Category: Select Personal and a list of sample tables for personal use appears.
Sample tables: Select CD-Collection and a list of available fields appears in the Selected fields window..
Selected fields: By selecting each field and using the > button, move the following fields from the Available fields list to the Selected fields list: CollectionID, AlbumTitle, Artist, DatePurchased, Format, Notes, and NumberofTracks.
Figure 7: Selecting fields from the Personal category
Next, we will use the wizard to create a table from the Business category:
Category: Select Business as the Category. Select Employees in the drop-down list of sample tables.
Figure 8: Selecting fields for the table
Use the > button to move the Photo field from the Available fields list to the Selected fields list. It will be at the bottom of the list, directly below the NumberofTracks field. If you make a mistake in selecting fields, click on the field name in the Selected fields list and use the < button to move it from the Selected fields list back to the Available fields list.
If necessary, click the field name and use the up and down arrows on the right side of the Selected fields list to move it to the correct position.
Click Next.
Tip
You can double-click on a field to transfer it from from the Available fields list the Selected fields list or double-click a field in the Selected fields list to transfer it back to the Available fields list.
Step 2: Set field types and formats
Once the wizard has helped you to select the fields for your database, it will allow you to assign properties to each field. (See Figure 10.) In this example, set each field’s properties as follows:
Note
If any of these fields requires a mandatory entry, set Entry required to Yes. A blank field will then not be allowed. By default, the Entry required property is set to No. (Because AutoValue has been set to Yes. Figure 10 does not display the Entry required field.)
CollectionID: Since this field must be filled, change AutoValue from No to Yes.
AlbumTitle:
Entry required: If all of your music is in album format, set Entry required as Yes. If not, set this property to No.
Length: Do not change this property unless you have titles that exceed 100 characters counting the spaces.
Artist: Set Entry Required to Yes unless you do not always use this field.
Date Purchased: Field type: default setting. In this case, Entry required should be No unless you want to add this information to all database entries.
Format: Change the Entry Required setting from No to Yes.
Notes: No changes are required.
NumberofTracks: Change the Field type to Small Integer [ SMALLINT ], which allows for 32768 tracks.
Photo: Use the default settings.
Note
Since Base requires you to specify the maximum length of each field on creation, make sure you make each field as large or maybe larger than necessary. By default, Base uses the VARCHAR format for a text field, so it will only use the number of characters necessary for the field even if it takes up less space than than the limit. So when a field uses VARCHAR, a field that contains 20 characters will only take up 20 characters in the database, even if the limit for the field is set at 100. In contrast, a text field with the CHAR text format has a fixed length and that field will be 100 characters long even if the actual length is 1 or 100.
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.
Tip
Since up and down arrows are available on this page (not shown in Figure 10), you can use these to reorder the list of fields. Also + and – buttons are available and you can use these to remove fields from the list or to add new fields.
When you have finished, click Next.
In this example, Create a primary key should be checked.
Next, select option Use an existing field as a primary key.
In the Fieldname drop-down list, select CollectionID.
Select Auto value if it is not already checked. Click Next.
Note
A primary key field contains a unique number that can be used to identify a specific entry in a database. In this case, CollectionID will contain a number that is assigned automatically by Base to each record of this table.
Step 4: Create table
At this point, the wizard will allow you to rename the table. For this example, make no changes.
Leave the option Insert data immediately checked.
Click Finish to complete the Table Wizard. Close the Table Data View window created by the Table Wizard. You are now back to the main window of the database, but a table named CD-Collection is now listed in the Tables portion of the window.
Click the Save button on the Standard toolbar at the top of the main window.
If you want to create identical tables for each type of music you have, you can make copies of the original table and name each according to the type of music contained in it.
Click on the Tables icon in the Database area of the LibreOffice Base window to see the existing tables.
Right-click on the CD-Collection table icon. Choose Copy in the context menu.
Right-click, and select Paste. The Copy table dialog opens.
Change the Table name to CD-Jazz, and set Options to Definition and data and click Create.
Figure 9: Creating copies of original table
Tip
The action buttons on this page allow you to move selected fields, or all fields, in either direction. You can also double-click to move a field from one list to the other.
Click the Save button on the Standard toolbar at the top of the LibreOffice Base window.
Caution
After a table has been created in Base and data has been entered, the field properties cannot be changed. Before you create a table, make sure you have decided the correct names, length, and format for each field.
As a result, deleting a field deletes all the data once contained in that field. Changing the field type of a field after the table has been created can lead to data being lost either partially or completely.
Deleting a table removes all of the data contained in every field of the table. As a result, do not delete a table unless you are certain.
Double-click on the CD-Jazz table icon and visually verify the data before closing the table
Right-click on the CD-Jazz table icon, select Delete and confirm deletion.
Once you are familiar with creating a table with the Wizard, use Design View to create a new table. View allows you to directly enter information about each field in a table. We will use this method for the next example (the Automobile database).
Note
While the field types and formatting are different in Design View, the process is similar to the one used in the Table Wizard.
When we create the Automobile database, the first table we will generate is Fuel. Its fields will be FuelID, Date, FuelCost, FuelQuantity, Odometer, and PaymentType.
Find the Tasks area of the LibreOffice Base window and click Create Table in Design View to open LibreOffice’s Base: Table Design dialog.
Enter FuelID into the Field Name field then press Tab key to move to the Field Type column. Select Integer [INTEGER] from its drop-down field.
Tip
You can select fields from the Field Type drop-down list by pressing the key for the first letter of the choice. You can cycle through the choices for a given letter by repeatedly pressing that key.
Change the Field Properties in the bottom section of the dialog for FuelID.
Change AutoValue from No to Yes.
Set FuelID as the primary key for the Automobile database.
Click in the Field Name cell directly below FuelID. The dialog automatically sets FuelID as the primary key and places a key icon in front of FuelID (Figure 11)
Figure 10: Changing field types
.
Figure 11: Defining the primary key field
Tip
When you assign certain integer field types (Integer and BigInt for example) to a field, selecting Yes for AutoValue automatically makes that field the primary key.
Primary keys for any other field type must be selected by right-clicking the rectangle before the field and selecting Primary Key in the context menu.
Note
The primary key serves as a unique ID for each record in a database. You can use any name for this field. We have used FuelID for convenience, so we know to what function it serves.
When you are creating the Date, FuelCost, FuelQuantity, Odometer, and PaymentType fields, do the following:
Type the field name in the Field Name column.
Select the field type:
For Date use Date[DATE]. (Press the D key to select it.)
FuelCost, FuelQuantity, and Odometer use Number [NUMERIC]. (Press the N key once to select it.)
PaymentType uses Text [VARCHAR], the default setting.
FuelCost, FuelQuantity, and Odometer also need changes in the Field Properties section (Figure 12).
FuelCost: Change the Length to 5 and Decimal places to 2. Click Format Field (Figure 12). This opens the Field Format dialog (Figure 13). Use Currency as the Category and your currency as the Format.
Figure 12: Changing field properties
FuelQuantity: Change Length to 6 and Decimal places to 3. (Many fuel pumps measure fuel to thousandths of a gallon in the USA.)
Odometer: Change the Length to 10 and the Decimal places to 1.
Repeat steps a) through c) until you have entered all of the fields.
Figure 13: Format example options
To save and close the table in the Table Design dialog, select File > Save, or click the Save icon on the Standard toolbar, or press Ctrl+S. Name the table Fuel.
Close the Table Design dialog.
In the main database window, select File > Save, click the Save icon on the Standard toolbar, or Ctrl+S to save the database.
Follow the same steps to create the Vacations table. The fields, field types, and descriptions are listed in Figure 14.
Make the following changes in the Field Properties area of the window.
Odometer: As described in step 3)), bullet c)) for the corresponding field of the Fuel table.
Motel, Tolls, Breakfast, Lunch, Supper, SnCost, and Miscellaneous: Format currency the same as step 3)), bullet c)) for the FuelCost field of the Fuel table.
Making Date the primary key in this table has to be done in a different way because this field is typed as Date, not Integer.
Right-click to the left of the field name Date.
Select Primary Key in the context menu.
Following similar steps to those above, you should use Design View to create a much simpler table entitled Payment Type. This table contains two fields, named Type, with the field type set to Text [ VARCHAR ] and PaymentID (field type Integer [ INTEGER ]). Set the PaymentID field as the primary key for the table.
Figure 14: Fields in Vacations table
Now that the tables have been created for the Automobile database, what are the relationships between these database tables?
When on vacation, you will want to enter all of your expenses at once each day. Most of these expenses are in the Vacations table, but the fuel we buy is not. So we will link these two tables using the Date fields. Since the Fuel table may have more than one entry per date, this relationship between the Vacations and Fuel tables is one to many (it is designated 1:n).
Make sure you are still in the Automobile database and select Tools > Relationships. The Automobile – LibreOffice Base: Relation Design window opens and the Add Tables dialog pops up. (You can also open it by clicking the Add Tables icon on the Relation Design window if you ever need to do so.)
Figure 15: Designation for a 1:n relationship
Once the Add Tables dialog is active, use one of two ways to add a table to the Relation Design window:
Double-click the name of the table. In our case, do this for both Vacations and Fuel.
Or, for each table, click the name of the table and then click Add.
When you have added the tables you want, click Close to close the Add Tables dialog.
Next, define the relationship between the Vacations and Fuel tables by clicking the New Relation icon on the Standard toolbar or selecting the Insert > New Relation command. Either command opens the Relations dialog (Figure 16). Our two tables are listed in the Tables Involved section.
Figure 16: Selected fields in a relationship
In the Fields Involved section, click the drop-down list under the Fuel label.
Select Date in the list.
Click in the cell to the right of this drop-down list. This opens a drop-down list for the Vacations table.
Select Date in the list. It should now look like Figure 16.
Modify the Update Options and Delete Options section of the Relations dialog (Figure 17).
Figure 17: Update Options and Delete Options section
Select Update cascade in the Update options area.
Select Delete cascade in the Delete options area.
Click OK.
In the Relation Design dialog, click the Save icon or select File > Save.
Close the Relation Design dialog.
Click the Save button on the Standard toolbar at the top of the main database window.
Tip
The primary key can contain more than one field. Its foreign key (– A field in a table that stores values of the primary key of records in another table). will contain the same number of fields.) If this were the case in Figure 16, the other fields of the primary field for the Fuel table would be listed under Date. The corresponding fields of the foreign key would be listed under Vacations. Detailed information about this is in the Base Guide.
While these options are not strictly necessary, they do help. Having them selected permits you to update a table that has a relationship defined with another table which has been modified. It also permits you to delete a field from the table without causing inconsistencies.
Once you have created a database to store data, you will need a form. In the language of databases, a form is a front end for data entry and editing.
A simple form should include all of the fields from a table (Figure 18). More complex forms can contain much more, including additional text, graphics, selection boxes, and many other elements. Figure 19 is made from the same table with a text label (Fuel Purchases), a list box placed in PaymentType, and a graphic background.
Tip
A list box is useful when a field contains a fixed number of options. It saves you from having to type data by hand and having to double-check that the entered data is correct, and ensures that invalid options are not entered.
Figure 18: Fields of a simple form
Figure 19: 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.
Note
To create a list box as in Figure 19, we first need to create a small, separate table containing the name of the options. Then use the contents of the table to fill the list box. See Modifying a form below
We will use the Form Wizard to create a Vacations form, which will contain a form and a subform.
In the main database window (Figure 3), activate the Form Wizard by clicking the Forms icon in the Database area, selecting View > Forms, or pressing Alt+M. You can also click Use Wizard to Create Form on the Tasks list to open the Form Wizard (Figure 20). When the Form Wizard is activated, it displays the Database Form window and the Form Wizard dialog.
Figure 20: Selecting fields in the Form Wizard
On the Form Wizard dialog, under Tables or queries drop-down, select Table: Vacations. The Available fields list contains the fields for the Vacations table.
Move all of these fields to the Fields in the form list by selecting each field and clicking the >> button. Click Next.
Tip
Using the four action buttons in the center of this page, you can move selected fields, or all fields, in either direction. You can also double-click to move a field from one list to the other. The up and down arrows on the right can be used to re-order entries in the Fields in the form list.
Step 2: Set up a subform
Since we have already created a relationship between the Fuel and Vacations tables, we will use that relationship. If no relationship had been defined, you would have to do it later in the wizard (Step 4: Get joined fields).
Click the box labeled Add Subform.
Click the Subform based upon existing relation radio buttons.
Fuel is listed as a relation we want to add. So click Fuel to highlight it, as in Figure 21. Click Next.
Figure 21: Adding a subform
Step 3: Add subform fields
This step is similar to Step 1: Select fields, but not all the fields will be used in the subform.
Fuel is preselected under Tables or queries.
Use the >> button to move all the fields to the Fields in the form list.
Click the FuelID field to highlight it.
Use the < button to move the FuelID back to the Available fields list (Figure 22).
Click Next.
Figure 22: Selecting fields for a subform
This step is for tables or queries for which no relationship has been defined. Because the relationship has been defined in this example, the wizard will skip this step.
Note
Relationships between two tables can be based upon more than one pair of fields. For more information on this, consult the Base Guide.
Caution
When creating a relationship between fields from two tables, those fields have to have the same field type. That is why we used the Date field from both tables: both their field types are Date [ DATE ].
Whether a single pair of fields from two tables are chosen as the relationship, or two or more pairs are chosen, certain requirements must be met for the form to work.
One of the fields from the main form must be the primary key for its table. (Date would have to be used.)
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 field type.
Step 5: Arrange controls
A form’s control consists of two parts: label and field. In this step, you determine where a control’s label and field are placed relative to each other. From left to right, you can choose Columnar – Labels Left, Columnar – Labels on Top, As Data Sheet, and In Blocks – Labels Above.
In this example, we will arrange the main form by clicking the second icon (Columnar – Labels on Top). The labels will be placed above the related field.
Next, we will arrange the subform by clicking the third icon (As Data Sheet). The labels are column headings and the field entries are in spreadsheet format. Click Next.
Figure 23: Arrange controls
Step 6: Set data entry
Step 7: Apply styles
Select the color you want in the Apply styles list. In this example, use the Beige option.
Select the Field border you want. In this example, use the 3D look option. You might want to try different settings.
Click Next.
Step 8: Set name
Enter the name for the form. Use Vacations.
Click Modify the form.
Click Finish. The form opens in edit mode, Base will display the Fuel – LibreOffice Base: Database Form window.
When a form is generated with the Wizard (Figure 24), it usually needs to be reformatted. Figure 24 is not organized at all. Every control seems to have a different size and the color needs to be changed.
Figure 24: Basic form created using Wizard
These controls are different sizes because they contain different types of field data. Since Base has three types of controls (text, formatted, and date), these fields are formatted differently. The width of text controls is designed to display a large number of characters, while formatted controls are shorter because they will usually contain numbers or decimals. Date controls are fairly short, except for the date formats is day of the week, month, day, year (Thursday, October 31, 2020), which requires a larger width than 10/31/2020 or 31/10/2020.
First, we will modify the payment type controls. These fields need to be replaced with a list box, and each list box will be related to an existing field in the table, Payment Type.
Note
The following steps assume that the PaymentType table already contains three single-field records. These might be Cash, Dan, and Kevin. If necessary, you could use the Form Wizard to create a very simple form to input or edit these values.
Next, we will carry out the following steps:
Modify text controls (label field first and then the data field).
Change the background to a picture, then modify some of the labels so that they can be read clearly against this background. Change the font color of the headings.
Change tab order of fields.Figure 25 shows a section of the Form Design toolbar and key icons are identified that will be used during the following steps. Figure 33 shows the Form Navigator.
Figure 25: Part of Form Design toolbar
|
|
Figure 26: Fuel economy calculation field
Figure 27: Selecting fields for grouping data
Figure 28: Report Builder template determined by the Report Wizard
Figure 29: Report without modifications
Figure 30: Data Sources navigation buttons
Figure 31: Insert Database Columns dialog
Figure 32: Navigation arrows of a form
Figure 33: Form Navigator
Step 1): Replace fields with list boxes
Open the Control Properties and Form Navigator dialogs and click the Control Properties and Form Navigator icons. (You may need to select a control to make the Control Properties icon available)
Open the Form Navigator and click the first text box whose name contains the word “Payment”.
If both the label and text box components of the field are highlighted in the Form Navigator, double-click the text box so that only it is highlighted.
Tip
If the Form Navigator does not have the input focus, selecting a field highlights both field components (for example, label and text). If this arises, then double-clicking the required component will highlight it, and enable you to subsequently move from one component to another with a single click.
Right-click the text box in the Form Navigator, right-click to bring up the context menu, then select Replace with > List Box.
In the Properties dialog, the heading will become Properties: List Box and the General tab is selected. Scroll down to the Dropdown property and change No to Yes.
Select the Data tab.
Go to Type of list contents. Change Valuelist to Sql.
The line below this becomes List content. Click the ellipsis (…) to the right of this property to open the SQL Command – LibreOffice Base: Query Design window and the Add Table or Query dialog.
The PaymentType table has the required entries. Click it, and then click the Add button.
Close the Add Table or Query dialog.
In the SQL Command – LibreOffice Base: Query Design window, double-click Type in the table named PaymentType. This places Type in the Field row of the tabular area.
Figure 34: Selecting PaymentType
Save and close this window. The SQL code appears in the List content property. (SELECT "Type" FROM "PaymentType")
Repeat these steps for each field containing the word “Payment”, six fields in total. (See Note below before doing this.)
There are times, as in this example, when 8)) through12) ) produces the same SQL code. When this happens, copy the code. Paste the code into the Type of list contents property before doing 13)). It is a nice shortcut.
Step 2): Modifying text controls
In this step, you will modify the text labels and fields to make them more meaningful.
Select the associated control label (begins with lbl). The title of the Properties dialog should have become Label Field.
Click the General tab if it is not already selected.
Click the down-arrow at the right end of the Label property to open the drop-down. Only then rename the label to Payment Type and press Enter.
Click on another property to save this change.
Adjust the width of the label field if necessary for the new width of the label name.
Repeat a)) through f)) for each label field in the form that contains the word Payment.
Note
Base allows you to paste the title of a control in multiple control fields. Once the control title Payment Type has been entered for the first time, it can be copied. For the next label, highlight the current label name, paste the name you want, and click another property.
Resize the fields.
Make sure that you are in the edit mode and have opened the Form Navigator and Properties dialogs.
In the Form Navigator dialog, click the field whose size you want to change.
Scroll down to the Width property in the Properties dialog.
Estimate the width required based on the set of possible values for this field and change the Width property to that value. In the case of our Payment Type fields, the text box should be wide enough for the longest possible payment type value of “Kevin”.
Figure 35: Modifying listbox properties
Click either the PositionY or the Height property to save the change. Also, clicking on any other property will do the same thing.
Use these same steps to change the size for each of the remaining text controls, including txtSnackNo and all the payment type fields.
Step 3): Modify date control
With the Properties and Form Navigator dialogs open, select the datDate field in the Form Navigator dialog. The heading of the Properties dialog changes to Properties: Date Field.
In the General tab of the Properties dialog, scroll down to the Date format drop-down list, and select the date format you desire.
Scroll down to the Dropdown property and select Yes.
Is the width of the Date field inappropriate for the selected format? If so:
The Date field has a border around it.
Move the cursor over the middle green marker on the right side of the Date field. It should become a horizontal double arrow. Drag this to the right to increase the width. Drag it to the left to reduce the width. (It may take several attempts to get this right.)
Step 4): Modify formatted controls
Perform the following tasks with each field:
Revise the label of the control so it has the proper annotation. In the case of lblSnCost, change it to Snack Cost.
Click the label of the control to be modified in the Form Navigator. If the border for the label is longer or shorter than the text in the label, adjust its width as described for the Date field above.
Click this formatted field in the Form Navigator list.
Under the General tab, scroll down to the Formatting property.
Click the ellipsis (…) on the right to open the Format Number dialog.
For the Odometer field click Number in the Category list, otherwise click Currency.
Under Format, select a field.
Select the desired number of decimal places (one for Odometer, two otherwise).
Select the desired number of leading zeros.
Negative numbers red: check if yes, remove the check if no.
Thousands separator: check if yes, remove check if no.
Click OK.
If the width of the field is not correct for the selected format, adjust its width as described for the Date field above.
Repeat these steps for each field name in the Form Navigator that begins with fmt.
Step 5): Modify fields in the table
The controls in the table control must be modified. To change things, access that particular control’s properties by clicking the header.
Click the Date label (select the column named Date).
Scroll to the Date format property and select the format you desire from the drop-down list, if that format differs from the default setting.
Scroll to the Width property. Adjust the width if necessary.
Scroll to the Dropdown property. If you want to have a small calendar pop-up window, select Yes.
Click the FuelCost label.
Open the drop-down list for the Label property. Add a space between Fuel and Cost then press Enter.
Scroll to the Formatting property. Click the ellipsis (…) on the right. Select Currency from the Category column. Uncheck the Negative numbers red and the Thousands separator. Click OK.
Scroll to the Width property. Adjust the width if necessary.
Change the Alignment property if you do not want data to be to the left. Select Center to put the data in the middle of the cell.
Click the FuelQuantity label.
Open the drop-down list for the Label property. Add a space between Fuel and Quantity. Press Enter.
Scroll to the Formatting property and change to show three decimal places if needed.
Scroll to the Width property. Adjust the width if necessary.
Change the Alignment property if you do not want data to be to the left. Select Center to put the data in the middle of the cell.
Click the Odometer label.
Scroll to the Formatting property. If you wish to show one decimal place, make the change.
Scroll to the Width property. Adjust the width if necessary.
If you do not want data to be to the left, change the Alignment property and if you want to put the data in the middle of the cell, select Center.
Click the PaymentType label.
Right-click this label, then add a list box in a context menu by selecting Replace with > List Box.
Open the drop-down list for the Label property. Add a space between Payment and Type. Press Enter.
On the Data tab, change the Type of list contents property to Sql.
Paste the SQL code SELECT "Type" FROM "PaymentType" into the List content property. (This is a shortcut method – see the Tip on page 1 and the detailed instructions above it if you want more details).
Step 6): Group the controls:
Three icons need to be activated on the Form Design toolbar: Display Grid, Snap to Grid, and Helplines While Moving. If this toolbar is on the side of the form, click the double arrow (») to open a sub toolbar showing them. If the toolbar is on the top or bottom of the form, they are on the right end of it. If there is a double arrow at the end, click it to reveal them. Make sure they are highlighted as in this figure.
Figure 36: Help for moving controls
Some of the controls need to be moved in pairs so the Form Navigator needs to remain open. Close the Properties dialog now.
Tip
You can also control the Display Grid, Snap to Grid, and Helplines While Moving options using the View > Grid and Helplines menu.
Note
When moving a control, do not use the PositionX and PositionY properties to place the control, because this will place the control’s label and field on top of each other. Those properties can move a label or field to a specific location, but are not designed to move a control. Use the Position and Size icon on the Form Design toolbar to move a control or group of selected controls.
The list in the Form Navigator reveals which controls needed to be grouped based upon their names:
lblMotel / fmtMotel with lblMPayment / txtMPayment;
lblBreakfast / fmtBreakfast with lblBPayment / txtBPayment;
lblLunch / fmtLunch with lblLPayment / txtLPayment;
lblSupper / fmtSupper with lblSPayment / txtSPayment;
lblSnackNo / txtSnackNo with lblSnCost / fmtSnCost and with lblSnPayment / txtSnPayment (group these three controls together);
lblMiscellaneous / fmtMiscellaneous with lblMiscPayment / txtMiscPayment.
That is six groups of controls; each one will be moved separately.
The layout shows where each control of a group goes for each of these groups. For most of them, the first control holds the cost, and the second contains the payment type in a row. The Snacks places the number of snacks followed by the cost and then the payment type. The Miscellaneous controls contains the payment below the cost.
Before you move an entire group to a new location, move the controls within the group to where you want them to be relative to each other.
Click on the first control, placing a border around it.
Place the mouse pointer over the label or field where it changes its shape.
Drag it to an open area of the form.
Using the same method, move the second control to the correct position relative to the first control.
As soon as you begin to move it, the helplines will appear. Use them to line up the edges of the controls before releasing the mouse button.
If there is a third control, move it to the correct position relative to the second one.
Move the controls for the group to where you want them:
Click the first component in the Form Navigator list.
Use the standard Control+Click and Shift+Click mechanisms until all of the labels and fields in this group are highlighted.
Place the mouse pointer over one of the labels or fields changing the arrow.
Drag the group to where you want its controls to be.
Repeat for the other controls using these same steps. If a group is going to be below another group of controls, use the helplines to line up the left end of the two groups (for example: first control under left control).
Figure 37: Form suggested layout
Step 7): Modify memo control
The other modification is adding scrollbars. The choices are none, vertical, horizontal, or both.
To change the size of this control, follow the same steps for changing the size for any text control.
If necessary, move the control to the required position using the same steps described above for grouping the controls (Step 6)).
Click the Control Properties icon on the Form Design toolbar if the Properties dialog is not open.
Select the txtNotes control in the Form Navigator dialog.
On the Properties dialog, click the General tab if it is not selected.
Scroll down to the Text type property.
Select the Multi-line option in its drop-down list.
Scroll down to the Scrollbars property.
In its drop-down list, select the option you need (None, Vertical, Horizontal, or Both).
Close the Properties dialog.
Figure 38: Scroll bar selections in the Properties dialog
Step 8): Add headings
Make sure that the cursor is in the top left hand corner of the Database Form window.
Move the cursor with the Enter key until it aligns with the gap between the Date field and the Breakfast field.
Select the Heading 2 style from the Set Paragraph Style drop-down list.
Place the input cursor where the first heading should be. Enter the heading Meals.
Move the input cursor with the space bar to the location where the second heading will appear. Type the heading Snacks.
Press the Enter key repeatedly to move the cursor down until it aligns with the gap between the Supper field and the table.
If the style has changed, use the Set Paragraph Style drop-down list on the Formatting toolbar to reselect the Heading 2 style.
Move the input cursor to the position where you want the table heading to appear. Type the heading Fuel Data.
Tip
The Styles deck can be opened by selecting View > Styles or by pressing F11. Also, you can quickly modify the appearance of all three headings by right-clicking the Heading 2 style and selecting the Modify option in the context menu would allow. See the Writer Guide for details.
Step 9): Change the background
To add a color to the form background:
Right-click the form and select Page Style in the context menu.
The Page Style dialog (Figure 39) will appear. Make sure the Area tab is selected.
Figure 39: Page Style dialog
Select Color in the row of buttons near the top of the tab.
Select a color from the grid. If you wish to access more choices, choose a palette from the Palette drop-down menu or use the RGB or Hex boxes to create a specific color or click Pick for even more choices.
Click Apply to see what your selection will look like in your form.
Change if necessary.
Click OK to accept your color choice and close the dialog.
To add other form backgrounds:
Select the type of background from the row of buttons: None, Gradient, Bitmap, Pattern, or Hatch.
In the selected type, choose one of the selections provided or create your own. For details, see Chapter 4, Changing Object Attributes, in the Draw Guide.
Click Apply to see what your selection will look like in your form.
Click OK to accept your choice and close the dialog.
If you selected a bitmap in the list, the form might look like Figure 40.
Step Error: Reference source not found: Change the tab order
Figure 40: Finished form
Select View > Toolbar > Form Design to open the Form Design toolbar.
Click the Activation Order icon (circled in Figure 41).
Figure 41: Form Design toolbar with Activation Order icon circled
Rearrange the field order in the Tab Order dialog.
If you click the Automatic Sort button, Base will automatically generate a tab order.
You can use the Move Up and Move Down buttons to change the position of a selected control in the list.
If necessary, drag a control from one position to another in the Controls list.
Define a tab order. If you like, you can put the fields in the same order as Figure 42.
Click OK.
Save and close the form.
Save the database.
Figure 42: Possible tab order for the Vacations form
Different types of fields allow different methods of data entry. In many cases, more than one data entry method can be used.
First, open the form from the main database window (Figure 3).
Click the Forms icon in the Database list, or select View > Database, or press Alt+M.
Find the form’s name in the Forms list (Vacations).
Double-click the form’s name.
The quickest way to enter a date in the Date field is to click on the arrow that opens the drop-down calendar (Figure 43) and select the day that you want. Then press the Tab key to go to the next field.
Figure 43: Calendar drop down
The Odometer, Tolls, and Motel fields are numeric fields. Enter values directly into them, or use the up and down arrows (if you chose to display to displace the arrows when the form was created). After you have entered a value in a field, use the Tab key to go to the next field.
To increase the value, click on the up arrow. If you need to decrease the value, click on the down arrow.
The up and down arrows only change the numbers to the left of the decimal place.
If you need to change numbers to the right of the decimal place, delete them and enter the desired numbers.
The Motel’s Payment Type field is a drop-down list. If the elements of the list start with different letters, you move to a field by entering the first letter of the field’s title.
If two or more elements of the list have the same first letter, repeated typing of the first letter will cycle through these elements.
When the selection is correct, use the Tab key to go to the next field.
The form’s remaining fields are either numeric fields or drop-down lists until you reach the Notes field (which is a text field). Type anything you desire in this field just as you would any simple text editor.
Note
The Tab key is used to move between fields. However, there are some limitations where other keys.
Since text fields do not have tab positions, they can not move from one position in the field to another one. All spacing must be done by the space bar.
The Enter key can be used to move between non-text fields, but the function of the Enter key in text fields depends upon whether the text field has a single line property. If it does, pressing Enter will move the cursor to the next field. But if the text field has been assigned a multi-line property, pressing Enter moved will only move the cursor down one line. With this field, the Tab key has to be used to move the cursor to the next field.
If there was no subform for fuel data, pressing the Tab key in the last field would save all of the fields, clear them, and make the form ready to accept data for the next record.
Because we have a subform, the Tab key has a different function. It places the cursor in the first Date field of the subform with the date automatically entered to match the Date field of the main form.
The Fuel Cost, Fuel Quantity, and Odometer fields are numeric fields. The Payment Type field is a drop-down list. Enter the data just as you did in the main form, and use the Tab key to go to the next field.
When you use the Tab key to leave the Payment Type field, it goes to the Date field of the next line and automatically enters the date. Now you can enter your second set of fuel data for this day.
When the form has a subform, click any of the fields of the main form to move to another record. In this case, click the Date field of the main form. Then use the directional arrows at the bottom. Move from left to right: First Record, Previous Record, Next Record, and Last Record (all have keyboard shortcuts identified in their extended tips). To the right of these arrows is the New Record icon.
To create a new record while in the last record of the main form, click either the Next Record icon or the New Record icon.
Tip
Each record is numbered and the Record box contains the number of the record whose data is shown in the form.
If you need to go to a specific record, enter the record’s number into the record box and type Enter to take you to that record.Figure 44 is a record with data inserted in its fields.
Figure 44: Sample record of the Vacations form and subform
You can drag and drop a spreadsheet that uses the format for a database table into a database table by doing the following:
Open the database file in the LibreOffice Base window (Figure 3) and select the Table view.
Open the spreadsheet in Calc. Use a sheet that is formatted the same as the database table.
Place the two windows side by side in the desktop.
Drag the sheet tab at the bottom of the Calc window into the table list of the database file. The mouse pointer shows a square with a + sign. (Sheet tab: This has the name of the sheet on it.)
Drop the sheet by releasing the mouse button. The Copy table wizard appears to help you migrate the content to the database table.
When the first page of the wizard appears to select the options of the copy operation and name the database table. Each option is explained in the Help (F1). Next, use the second page select the sheet columns you want to copy into the table. And finally, the third page of the wizard allows you to define the data type of each column of your table.
Click Create to populate the new table with the spreadsheet data.
Note
The Copy table operation copies only values and strings from the Calc spreadsheet. It does not copy formulas.
If you need get specific information from a database, use a query. Their results are special tables within the database. This document will demonstrate two different methods for building queries by:
Use the Query Wizard to generate a list of albums from a particular artist from the CD-Collection table.
The information we might want from the Fuel table includes what our fuel economy is. We will do this using Design View. (Queries that require calculations are best created with Design View.)
Queries created by the Query Wizard provide a list of information—a single answer or multiple answers, depending upon the circumstances.
To generate a query with the Wizard, do the following:
Open the main database window (Figure 3).
Click the Queries icon in the Database section.
Select View > Queries, or press Alt+Q.
In the Tasks section, click Use Wizard to Create Query. The Query Wizard dialog opens (Figure 60). We will use the query to find albums by a certain musical group or individual (the album’s artist), including when each album was bought.
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 Vacations table used in a query has the name Vacations.Lunch.
Step 1: Select the fields
Select the Table: CD-Collection option in the Tables drop-down list.
Find the Available fields list fields in the CD-Collection table:
Click Artist, and use the > button to move it to the Fields in the Query list.
Move the AlbumTitle and DatePurchased fields in the same manner. You can also move a single field from one list to the other by double-clicking.
If you make a mistake, you can return an unwanted field from the Fields in the Query list to the Available fields list by selecting it and then typing the < button.
In other circumstances, you can use the >> and << buttons to move all fields in one list to the other list.
Click Next.
Tip
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
Figure 45: Sorting order page
Click the first Sort by drop-down list.
Select the CD-Collection.Artist option.
To list the artists in alphabetical order (a-z), select Ascending on the right.
Click the first Then by drop-down list.
Select CD-Collection.AlbumTitle and then select Ascending.
Click the second Then by drop-down list.
Select CD-Collection.DatePurchased and then select Ascending.
Click Next.
Step 3: Select the search conditions
The following options are available:
is equal to: the same as
is not equal to: not the same as
is smaller than: comes before
is greater than: comes after
is equal or less than: the same as or comes before
is equal or greater than: the same as or comes after
like: similar to in some way
not like: is not similar to
is null:
is not null:
Note
These conditions apply to numbers, letters (using alphabetical order), and dates.
Since we are only searching for one thing, we will use the default setting of Match all of the following.
We are looking for a particular artist, so select CD-Collection.Artist in the Fields drop-down and is equal to in the Condition drop-down.
Type the name of the artist in the Value box.
Click Next.
Step 4: Select the type of query
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
Change the alias for AlbumTitle to Album Title.
Change the alias for DatePurchased to Date Purchased.
Click Next.
Step 8: Checking results
Make sure that you have the query conditions listed in the Overview.
If something is wrong, use the Back button to move to the step that contains the error.
Then use the Next button to return to this step.
Name the query (suggestion: Query_Artists).
To the right of this are two choices. Select Display Query.
Click Finish.
Base displays the LibreOffice Base: Table Data View query showing any records that match the query. Close the window when you have finished with it.
Creating a query using Design View is not as difficult as it may first seem. It may take multiple steps, but each step is fairly simple.
What fuel economy is our vehicle getting (miles per gallon in the USA)? This question requires us to create two queries, with the first query used as part of the second query.
Step 1: Open the first query in Design View
Click Create Query in Design View in the Tasks area of the main window. Base displays the Add Table or query dialog (Figure 46) and the LibreOffice Base: Query Design window.
In the Add Table or query dialog, click Fuel to highlight it.
Click Add.
Click Close.
Base closes the Add Table or Query dialog and includes the Fuel table in the upper area of the Query Design window.
Figure 46: Add Table or Query dialog
Tip
Move the mouse pointer over the bottom edge of the Fuel table (Figure 47) and drag the edge to make it longer and easier to see all of the fields in the table.
Figure 47: Fuel table in query
Step 3: Add fields to the table at the bottom
Double-click the FuelID field in the Fuel table.
Double-click the Odometer field.
Double-click the FuelQuantity field.
Double-click the FuelCost field.
The query table at the bottom of the Query Design dialog should now have four columns (Figure 61).
Step 4: Set the criterion for the query
We want the query to include FuelID values that are greater than 0.
Type >0 in the Criterion cell under FuelID in the query table.
Click the Run Query icon on the Query Design toolbar(circled in red in Figure 48), or click View > Run Query, or press F5.
Figure 48: Run Query icon in Query Design toolbar
Base presents the results of the query in the area above the query table.Figure 49 shows the Fuel table with sample entries. The query results based upon the Fuel table are shown in Figure 50.
Figure 49: Fuel table
Figure 50: Query of Fuel table
Step 5: Save and close the query
Step 6: Create the query to calculate the fuel economy
Click Create Query in Design View in the Tasks area to open a new query.
Base opens the LibreOffice Base: Query Design window and the Add Table or Query dialog.
Add the Fuel table to the query just as you did in Step 2: Add tables to window but do not close the Add Table or Query dialog.
Add the End-Reading query to this query.
Click Queries to get the list of queries in the database (Figure 51).
Click End-Reading.
Click Add, and then click Close.
Base closes the Add Table or Query dialog and displays the Fuel table and End-Reading query in the upper area of the Query Design window.
Figure 51: Selecting queries to add to another query
Step 7: Add fields to the table at the bottom of the query
Figure 52: Tables in this query
Double-click FuelQuantity in the End-Reading query.
Double-click Odometer in the End-Reading query.
Double-click Odometer in the Fuel table.
Figure 53: Added fields to the query
Step 8: Enter the FuelID difference field
Type "End-Reading"."FuelID" - "Fuel"."FuelID" in the field to the right of the Odometer field of the Fuel table (Figure 62).
Type = '1' in the Criterion cell of this column.
Leave the Visible cell of this column unchecked.
Calculate the distance traveled:
Type in the next empty Field cell (Figure 63): "End-Reading"."Odometer" - "Fuel"."Odometer"
In the Alias row, type Distance.
Type > '0' in the Criterion cell.
Calculate fuel economy: Type ("End-Reading"."Odometer" - "Fuel"."Odometer")/"End-Reading"."FuelQuantity" in the next column to the right of the word Field (Figure 26).
Type Fuel Economy as the alias.
Note
When entering fields for these calculations, follow this format: table or query name. Then add a period and the field name. For hyphenated or multiple-word names (table or query), use double quotes around the table or query name. The query will then add the rest of the double quotes as in Figure 26.
Use the arithmetical symbol between the two. More than one calculation can be done by using parentheses to group the arithmetical operations.
Step 9: Run the query and make some modification
Figure 54: Result of running the fuel economy query
Click the Run Query icon on the Query Design toolbar (Figure 48). The results are shown in Figure 54.
Add Aliases: Type the aliases as they are listed in Figure 55.
Figure 55: Query table with Odometer aliases added
Run the query again. The results are in Figure 56.
Figure 56: Query run with aliases
Step 10: Close, save, and name the query
Click the Save icon.
Name the query.
Close the query.
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.
Note
To fully use queries requires a knowledge of set operations (unions, intersections, and, or, complements, or any combinations of these). Having a copy of the RDBMS manuals, is also extremely useful.
Reports retrieve information found in the database and arrange it in useful ways. They are similar to queries except that they are designed to be read by people. Queries are only designed to retrieve selected data from the database. Reports are generated from the database’s tables, views, or queries.
All reports are based upon a single table, view, or query, so first you need to decide what fields you want to use in the report. Before you use fields from different tables, you must first combine these fields in a single query or view. Then you can use this information to create a report.
For example, a report on vacation expenses includes both fuel costs and meal costs. These values are contained in fields of two different tables: Vacations and Fuel. Then this report will allow you to create a query or view.
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 (see Figure 62). The report will be identical before and after you make the change. But if you add more data to the query and run the report again, it will contain the new data. However, all data will be based upon “End-Reading”.“Odometer” – “Fuel”.“Odometer” having the value 1. No data will be present in the report for which “End-Reading”.“Odometer” – “Fuel”.“Odometer” has the value 3.
We will create a report on vacation expenses. Before we create the report, we will need to know.
What information do we want in the report?
How do we want the information arranged?
What fields are required to provide this information?
Will a query or view have to be created because these fields are in different tables?
Are any calculations required in the data before being added to 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 now create two reports. One will list the expenses each day (other than fuel) and the second listing fuel statistics. The fields we need for the first report from the Vacations table are: Date, Motel, Tolls, Breakfast, Lunch, Supper, Snack Cost, and Miscellaneous. This report only requires the Vacations table. Had the report listed the total expenses for each of these fields, we would have to create a query to provide us with these totals, which is beyond the scope of this chapter.
The next report involves the Fuel table. Since this table includes fuel purchases at times other than during the vacation, we need to create a query that contains only the fuel purchased during vacation periods.
When you open the Report Wizard, the Report Builder open as well. As you make your selections in the wizard, these appear in layout in the Report Builder. When you have finished making your selections, you save the report, name it and then close it.
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.)
Before we begin, we must create a new report:
Click the Reports icon in the Database area of the main Base window (Figure 3), or select View > Reports, or press Alt+R.
In the Tasks list, click Use Wizard to Create Report. The Report Wizard dialog and the Report Builder window open.
Step 1: Field selection
Select Table: Vacations in the Tables or queries drop-down list.
Use the > to move these fields from the Available fields list to the Fields in report list: Date, Motel, Tolls, Miscellaneous, Breakfast, Lunch, Supper, and SnCost. The other buttons (<, >>, <<, up arrow, and down arrow) and the double-click action perform similar functions to the other wizard pages as described earlier in this chapter.
Click Next.
Step 2: Labeling fields
Shorten Miscellaneous to Misc.
Change SnCost into Snack Cost.
Click Next.
Step 3: Grouping
Since we are grouping by the date, use the > button to move the Date field from the Fields list to the Groupings list.
You can use the < button to return an entry in the Groupings list to the Fields list. You can use the up and down arrows to re-order entries in the Groupings list or you can double-click to move a field from one list to the other.
Click Next.
Step 4: Sort options
Click Next.
Step 5: Choose layout
Select Columnar, three columns in the Layout of data list.
Layout of headers and footers have no possible selections.
Select Landscape as the Orientation for the page layout.
Click Next.Figure 28 shows part of the final template that has been built up in the Report Builder as you reach the end of the wizard.
Step 6: Create report
Title the report: Vacation Expenses.
Select the Dynamic report option.
Select the Create report now option.
Click Finish.
LibreOffice opens the output report in read-only mode in Writer.
The report (Figure 29) has been created. However, the date could be formatted better and all the numbers need to be formatted as currency, and the report could have a heading that includes its name, its author, and the date it was prepared. If the report had more than one page, the page numbers could be put in a footer of each page, perhaps including the total number of pages. But to do these things, you must use Report Builder.
The Report Builder allows you to create complex database reports. You can define group and page headers, group and page footers, and calculation fields. Report Builder is installed with LibreOffice.
While the Report Wizard allows us to create a template in Report Builder for our report. When we open Report Builder, we open it with this template. By modifying the template, we also modify the report. For example, we can change the Date field’s format, and it will change the format of all the dates contained in that field in the above report. Similarly, we can change the field formatting of any of the other fields and change the format everywhere that field appears in the report.
Report Builder can also create reports by itself. To do this, click Create Report in Design View. For instructions on how to use the Report Builder, see Chapter 6, Reports, in the Base Guide.
LibreOffice allows data sources to be accessed and then linked into LibreOffice documents. For example, a mail merge can link to an external document containing a list of names and addresses into a letter. One copy of the letter being generated for each entry.
To access a data source that is not a *.odb file:
Select File > New > Database, or click the arrow at the right of the New icon on the Standard toolbar and select the Database option in the menu, to open the Database Wizard.
Select Connect to an existing database. Select the required database type in the drop-down list. Click Next.
The remaining steps in the Database Wizard may vary depending on which database type you have chosen.
At some stage the wizard may prompt you to browse and select a database of interest. Normally, you will select it to register the database and open the database for editing.
Finally, click Finish to exit the wizard. Name and save the database in the location of your choice.
Accessing a spreadsheet is similar to accessing other databases:
Select File > New > Database, or click the arrow at the right of the New icon on the Standard toolbar and select the Database option in the menu, to open the Database Wizard dialog.
Select Connect to an existing database. Select Spreadsheet in the drop-down list. Click Next.
Click Browse to locate the spreadsheet you want to access. If the spreadsheet is password protected, check the Password required box. Click Next.
Make sure that the Yes, register the database for me and Open the database for editing options are selected.
Click Finish to exit the wizard. Name and save the database in the location of your choice.
Note
If you access a spreadsheet with this method, you cannot change anything in the spreadsheet using Base. You can only view the contents of the spreadsheet, run queries, and create reports based upon the data already entered into the spreadsheet.
All changes in a spreadsheet must be made in the spreadsheet itself with Calc. After modifying the spreadsheet and saving it, the changes will be in the database. When you create and save an additional sheet in your spreadsheet, the database will have a new table the next time you access it.
Databases created by LibreOffice are in the *.odb (OpenDocument Database) format. Other programs can also produce databases in this format. Registering a *.odb database is simple:
Choose Tools > Options > LibreOffice Base > Databases.
Under Registered Databases, click New. LibreOffice displays the Create Database Link dialog.
Browse to where the database is located. Make sure the registered name is correct.
Click OK.
Note
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.
Once the data source is registered, whether a spreadsheet, text document, external database or other accepted data source, you can use it in other LibreOffice components including Writer and Calc.
First, open a document in Writer or Calc and view the data sources available by pressing Ctrl+Shift+F4 or select View > Data Sources. This brings up a list of registered databases, including Bibliography and any other registered database, such as the Automobile database created earlier in this chapter.
To view each database, click the drop-down arrow for the database’s name (Figure 57). This expands to show Queries and Tables. Click the drop-down arrow for Tables to view the individual tables created. Now click on a table to see all the records held in it.
Figure 57: Databases in Data Sources window
Some data sources (but not spreadsheets) can be edited in the Data Sources window. A record can be edited, added, or deleted.
If you click on a table, its rows and columns of data appear on the right side of the Data Sources window. Editing this data requires only a click in the cell whose data should be changed, change the data, and click in the row above or below it to save the new data.
Beneath the records are five small buttons. The first four move the cursor to the beginning, to the left, to the right, and to the end respectively. The fifth button, with a small star, inserts a new record.
To delete a record, right-click on the gray box to the left of a row to highlight the entire row, and select Delete Rows in the context window to remove the selected row.
Figure 58: Deleting a row in the Data Sources window
You can launch LibreOffice Base at any time in the Data Sources window. Just right-click on a database or its Tables or Queries components and select Edit Database File in the context menu. Once in Base, you can edit, add, and delete tables, queries, forms, and reports.
Data can be placed into Writer and Calc documents from the tables in the Data Sources window. In Writer, values from individual fields can be inserted. Or a complete table can be created in the Writer document. One common way to use a data source is to perform a mail merge.
Figure 59: Toolbar for the Data Sources window
Tip
Choose Tools > Mail Merge Wizard or click on the Mail Merge icon (circled in Figure 59) in the Data Sources window to start the Mail Merge Wizard, which steps you through creating a mail merge document. See Chapter 14 in the Writer Guide.
When you need to insert a field from a table in the Data Sources window into a Writer document, click on the field name (the gray square at the top of the field list) with the left mouse button held down, drag the field onto the document. In a Writer document, it will appear as <FIELD> (where FIELD is the name of the field you dragged).
For example, if you wish to enter the cost of meals and who paid for them on a certain date of a vacation into the database:
Open the list of data sources (Ctrl+Shift+F4) and select the Vacations table in the Automobile database.
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.”
To replace (date), click the field name Date in the Data Sources window and drag it to the right of the word On. Insert an extra space if necessary. The result: On <Date>. If you have field shadings turned on (View > Field Shadings), <Date> has a gray background. Otherwise it does not.
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>.
To replace the first (name), click the BPayment field name and drag it to the right of paid by. Result: paid by <BPayment>.
In the same way, fill in the rest of the fields in the sentence.
Use <Lunch> and <LPayment> for the second set of (amount) and (name) in the sentence.
Use <Supper> and <SPayment> for the third set of (amount) and (name) in the sentence.
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>.
Add data to the fields of the sentence:
Click the gray box to the left of the row of data you want to add. That row should be highlighted like the second row of Figure 65.
Click the Data to Fields icon in the Table Data toolbar (circled in Figure 65). This should fill the fields with the data from the row you chose.
Click another row and then click this icon again. The data in the sentence changes to reflect this selected row of data.
Save the document if you want to use it as an example later.
Adding any data in table format is easier. Some of the steps are quite similar.
Navigate to the place you want to place the table and click the location.
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.
Click the Data to Text icon to open the Insert Database Columns dialog (Figure 31). (The Data to Text icon is to the left of the Data to Fields icon in Figure 65.)
Move the fields you want in your table from the Database columns list to the Table column(s) list.
To place the fields in order, click the field and use the single right arrow to move the fields in the order you desire. You can also limit the fields you use to less than all of the fields available.
To use all of the fields, use the double arrow pointing to the right to move all of them at one time (this icon may have a different appearance in some environments). The order of the fields in the table you create will be the same as in the data source table.
To remove a single field from the Table column(s) list, click the field and use the single arrow pointing to the left.
You can double-click a field in one list to move it to the other list.
To start over, click the double arrow pointing to the left (this icon may have a different appearance in some environments).
Select the settings for your table. Use the default settings as in Figure 31.
Click OK. Save the document.
There are two ways to transfer data into a Calc spreadsheet:
Enter the data into the spreadsheet cells.
Create complete new records in the spreadsheet. While you can directly access the data inserted into the spreadsheet cells, new records created in the spreadsheet are read-only.
Use the Data to Text icon to enter data directly to the spreadsheet cells as if you are making a table in a Writer document. But there are certain differences.
The steps are straightforward.
Click the cell of the spreadsheet which you want to be the top left cell of your data, including the column names.
Use Ctrl+Shift+F4 to open the Data Sources window and select the table whose data you want to use.
Select the rows of data you want to add to the spreadsheet:
Click the gray box to the left of the row (the row header) you want to select if only selecting one row. That row is highlighted.
To select multiple rows, hold down the Ctrl key while clicking the gray box of the rows you need. Those rows are highlighted.
To select all the rows, click the gray box in the upper left corner. All rows are highlighted.
Click the Data to Text icon to insert the data into the spreadsheet cells.
Save the spreadsheet.
Adding records to a spreadsheet is fairly easy. You need to have the Data Sources window open, your spreadsheet open, and the table you want to use selected.
Drag and drop the gray box containing the field name for the table’s ID field (the column header) to where you want the record to appear in the spreadsheet.
Repeat until you have copied all of the fields you need to where you want them in the spreadsheet.
Select File > Save, or click the Save icon on the Standard toolbar, to name and save the spreadsheet. Close the file.
Next time you open the spreadsheet, the fields will be populated with data from the first record of the table, and the Form Navigation toolbar will appear at the bottom of the spreadsheet.
Figure 60: First page of the Query Wizard
Figure 61: Query table
Figure 62: Typing in calculation of fields
Figure 63: Field for distance traveled calculations
Figure 64: Adding fields to a report
Figure 65: Selected row in Data Sources window
Figure 66: Giving aliases to fields
Click the arrows on the Form Navigation toolbar to view the different records of the table. (The arrows are circled in red in Figure 32.) The number in the box changes when you change the record number by clicking an arrow. The data in the fields changes to reflect the data for that particular record number.