Outils pour utilisateurs

Outils du site


issue83:libreoffice

Ceci est une ancienne révision du document !


Table des matières

1

Titre / LibreOffice Pt36: Base Views If you work with Base long enough, you will run into a situation where you need a table structured a little differently, or you’ll need the results of a query as a table you can use. The answer to these problems is ‘Views’. A view is a query which acts like a table you can use in other queries, forms, or controls. Today, I will show you an example of how this is useful when creating forms. Our Tables We are going to create a simple database with two tables. The database will track projects for a company and allow us to assign a team member to each project. Below is the structure of the tables we will create. Project Table Field|Type|Properties ID|Integer|Primary Key, Auto Increment Title|varchar(50)|Not Null Description|varchar(250) Due|Date MemberID|Integer|foreign key TeamMember Table Field|Type|Properties ID|Integer|Primary Key, Auto Increment FirstName|Varchar(25)|Not Null LastName|Varchar(25)|Not Null Shown right is the SQL to create the tables. Create a new database document, then go to Tools > SQL, and type the commands in by hand or copy and paste.

LibreOffice Partie 36 : Les vues de Base

Si vous travaillez avec Base pendant quelque temps, vous rencontrerez une situation où vous aurez besoin d'une table dont la structure est un peu différente ou vous aurez besoin des résultats d'une requête sous forme d'une table que vous pourrez utiliser. La solution à ces problèmes est « Vues ». Une vue est une requête sous forme d'une table qui pourra servir dans d'autre requêtes, des formulaires ou des contrôles. Aujourd'hui, je vous montrerai comment cela peut vous être utile dans la création de formulaires.

Nos tables

Nous allons créer une base de données simple avec deux tables. La base de données suivra des projets pour une société et nous permettra d'assigner un membre de l'équipe à chaque projet. La structure des tables à créer se trouve ci-dessous.

Project Table Field|Type|Properties ID|Integer|Primary Key, Auto Increment Title|varchar(50)|Not Null Description|varchar(250) Due|Date MemberID|Integer|foreign key

TeamMember Table Field|Type|Properties ID|Integer|Primary Key, Auto Increment FirstName|Varchar(25)|Not Null LastName|Varchar(25)|Not Null

Vous verrez à droite le SQL pour créer les tables. Créer une nouvelle base de données, puis allez à Outils > SQL et tapez les commandes à la main ou faites un copier/coller.

2

You can also get the above SQL commands on pastebin.com at http://pastebin.com/Wyb3R5Fz. The key to our task is the foreign key “MemberID” in the “Project” table, which connects to the “TeamMember” table's “ID” field. When we create our form we will create a drop-down list for selecting the team member who is responsible for the project. Notice that the “TeamMember” table provides first and last name fields. The list control allows us to use only one field in the list. We could display just the last name in the drop-down list, but what if two team members have the same last name. We will solve this problem by creating a view that will combine the first and last name into one field called “Name”. We will use our view to populate the drop-down list box. If the tables do not show in your table list after running the commands, Review > Refresh Table will populate the list. Create Query / View To create our view, we will use a two-step process. First, we create a query, then convert the query into a view. In our query, we combine the first and last name fields to create one field. We will also get the “ID” field, as we will need it to connect back to the “Project” table.

Les commandes SQL ci-dessus sont également disponibles sur pastebin.com à http://pastebin.com/Wyb3R5Fz.

La clé de cette tâche est la clé externe « MemberID » dans la table « Project », qui est relié au champ ID de la table « TeamMember ». Quand nous créons notre formulaire, nous créerons un menu déroulant permettant de sélectionner le responsable du projet parmi les membres de l'équipe. Remarquez que la table « TeamMember » contient des champs pour le prénom et le nom de famille. Le contrôle de liste nous permet d'utiliser un seul champ dans la liste. On pourrait n'afficher que le nom de famille dans la liste déroulante, mais que faire si deux membres de l'équipe ont le même nom de famille. Nous allons résoudre ce problème en créant une vue qui rassemblera le prénom et le nom de famille dans un seul champ appelé « Name ». Nous utiliserons notre vue pour établir la nouvelle liste déroulante.

Si les tables ne affichent pas dans votre liste de tables après avoir exécuter les commandes, Affichage > Actualiser les tables (Review > Refresh Table) établira la liste.

Créer une requête / vue

Nous utiliserons un processus en deux étapes pour créer notre vue. D'abord, nous créons une requête et, ensuite, nous convertissons la requête en vue. Dans notre requête, nous créons un seul champ en combinant les champs prénom et nom de famille. Nous obtiendrons aussi le champ « ID », puisque nous en aurons besoin pour nous lier à la table « Project ».

3

To create the query, we select the “Queries” option in the “Database” pane. In the “Actions” pane, select “Create Query in SQL View.” The follow SQL command will create our query: SELECT “FirstName” || ' ' || “LastName” AS “Name”, “ID” FROM “TeamMember”; If you are familiar with SQL, this looks like a standard query except for the double pipe symbols “||”. They are used to concatenate strings together. In the command, we select each “FirstName” and join it to a space, then take the result and join it to “LastName.” Finally, we name this string “Name.” We also get the “ID” as it identifies each record. You can test the query to make sure it works, but at this point your results are blank, but the query should run without error. Save the query as “QueryTeam”. Turning the query into a view is as simple as right-clicking the query name and selecting “Create as View.” Name the view “TeamView.” If you select “Tables” under the “Database” pane, you will see “TeamView” listed under the tables.

Pour créer la requête, nous sélectionnons l'option « Requêtes » dans le volet « Base de données ». Dans le volet « Tâches », sélectionnez « Créer une requête en mode SQL… » La commande SQL suivante créera notre requête :

SELECT “FirstName” || ' ' || “LastName” AS “Name”,

 "ID" FROM "TeamMember";
 

Si vous connaissez le SQL, cela ressemble à un requête standard sauf pour ce qui concerne les doubles symboles trait vertical « || ». On les utilise pour concaténer les chaînes. Dans la commande, nous sélectionnons chaque « FirstName » et l'adjoignons à une espace, puis nous prenons le résultat et l'adjoignons à « LastName ». Enfin, nous nommons cette chaîne « Name ». Nous obtenons aussi le « ID » puisqu'il identifie chaque enregistrement. Vous pouvez tester la requête pour vous assurer qu'elle fonctionne, mais, à ce stade, les résultats sont vides, mais la requête devrait s'exécuter sans erreur. Enregistrez la requête sous le nom de « QueryTeam ».

Transformer la requête en vue est aussi simple que faire un clic-droit sur le nom de la requête et sélectionner « Créer une vue ». Appelez la vue « TeamView ». Si vous sélectionnez « Tables » dans le volet « Base de données », vous verrez que « TeamView » figure dans la liste des tables.

4

Create the Forms We will create a team member form and a project form for data input. The team member form is the easiest, so let's make it first. Click on the “Forms” icon in the “Database” pane and select “Use Wizard to Create Form.” The form wizard will display. On the first screen, select “Table: TeamMember” from the drop-down box. Move the “FirstName” and “LastName” fields into the list box labeled “Fields in the form.” Click the “Next >” button. There is no subform so just click “Next >” again. On step 5, use any of the options for arranging the controls except tabular. On step 6, just accept the defaults. With Step 7 pick a style that you like. When you get to step 8, name the form “TeamMemberForm” and leave it on “Work with the form.” When you click the “Finish” button, the form will open for input. Add a few names for testing the project form when we finish it. For the project form, we need about the same thing, except use “Table: Project” from the drop-down in step 1, and select all the fields except for “ID” for inclusion in the form. Name the form “ProjectForm” and select “Modify the form” on step 8. This time, instead of the form opening for input, it opens for editing.

Créer les formulaires

Nous allons créons un formulaire pour les membres de l'équipe et un formulaire du projet, pour les données. Le formulaire « membres de l'équipe » étant le plus facile, nous allons le créer en premier. Cliquez sur l'icône « Formulaires » dans le volet « Base de données » et choisissez « Utiliser l'assistant de création de formulaire… ». L'Assistant Formulaire s'affichera.

Sur le premier écran, choisissez « Table : TeamMember » dans le menu déroulant. Déplacez les champs « FirstName » (prénom) et « LastName » (nom de famille) dans le menu déroulant appelé « Champs du formulaire ». Cliquez sur le bouton « Suivant > ». Il n'y a pas de sous-formulaire alors cliquez sur « Suivant > » à nouveau. À l'étape 5, vous pouvez utiliser n'importe laquelle des options de contrôle exceptée l'option « Comme feuilles de données ». À l'étape 6, acceptez les défauts. À l'étape 7 vous devez choisir un style qui vous plaît. Quand vous arrivez à l'étape 8, appelez le formulaire « TeamMemberForm » et laisser sur « Utiliser le formulaire ». Lorsque vous cliquez sur « Créer », le formulaire s'affichera pour l'entrée de données. Ajoutez-y quelques noms pour tester le formulaire projet quand nous l'aurons terminé.

Pour le formulaire projet nous avons besoin pour à peu près la même chose sauf qu'il faut utiliser « Table: Project » à partir du menu déroulant à l'étape 1 et qu'il faut sélectionner tous les champs sauf « ID » comme champs du formulaire. Appelez-le « ProjectForm » et, à l'étape 8, choisissez « Modifier le formulaire ». Cette fois-ci, au lieu de s'afficher pour que vous puissiez y entrer des données, il s'affiche pour que vous puissiez l'éditer.

5

When you create forms using the form wizard, the wizard groups a text box for most data types with a label for each field. In order to change the control for the data field, you have to ungroup the text box from the label. In our case, we want to change the “Member” field, so right-click the “Member” label and text box and select Group > Ungroup. Click on the form background to unselect both. Right-click on the text box and select “Delete.” From the forms toolbar, select the list box. If the form toolbar is not showing then View > Toolbars > Form Controls to display it. Once you select the list box, your cursor will become a crosshair, +. Click and drag to create the list drop-down box. When you release the mouse button the list box wizard will pop up. Select “TeamView” as your table and click “Next >”. The field we want to fill the list box is “Name”, so select “Name” and click “Next >”. Finally, we need to match the fields from the two tables. For the “Value Table” select “Member”. “ID” is the field to select for “List Table” as it is the primary key that matches up with the “Member” foreign key field in the “Project” table. Save your changes and close the design window.

6

Now, if you entered names in “TeamMemberForm”, you can open the “ProjectForm” and those names will appear in the drop-down list box we created. You will want to test creating several projects and assigning members to them to test the workability of your forms. In this article, we discussed the use of a LibreOffice Base view to create a new table from an existing table. We used this view in the construction of a form that automated the retrieval and selection of records from that view. TABLEAU drop table “Project” if exists; drop table “TeamMember” if exists; create table “TeamMember” ( “ID” integer generated by default as identity (start with 1) not null primary key, “FirstName” varchar(25) not null, “LastName” varchar(25) not null ); create table “Project” ( “ID” integer generated by default as identity (start with 1) not null primary key, “Title” varchar(50) not null, “Description” varchar(250), “Due” date, “Member” integer, constraint FK_MEM foreign key (“Member”) references “TeamMember” (“ID”) );

issue83/libreoffice.1409917891.txt.gz · Dernière modification : 2014/09/05 13:51 de auntiee