Vous êtes sur la page 1sur 6

Lecture V MICROSOFT OFFICE ACCESS 2007 Queries

Query is an object that visualizes data from different tables based on different criteria. For creating a query, Microsoft Access features a user friendly interface, of QBE type (Query By Example). The result of a query is a dynamic response sheet named Dynaset, which is volatile (it disappears when the query is closed). A query can group data from one or more tables, recognizing the relationships previously built, can create new calculated fields or can perform the following actions: creating a new table, appending records from another table, updating the value of a field or delete records. Queries are usually created using Query Design. In order to see the result of a query, we can switch to Datasheet View or press the Run button from the ribbon, from the Design menu option. There is another way to create a query using SQL View. This method requires SQL knowledge (Structured Query Language). We will talk about this method after we present the visual part. Queries are of different types: SELECT displays data from one or more tables, based on selection criteria, calculate fields, and sorts data based on an Ascending or Descending order; TOTAL displays data from one or more tables, grouped by different fields, on each group a functions being applied to the data, such as: SUM, MIN, MAX, COUNT, AVERAGE; CROSSTAB multidimensional data analysis; Action queries (MAKE TABLE, APPEND, UPDATE, DELETE); Special ones (UNION, PASS THROUGH, DATA DEFINITION). The most common queries are Select queries.

1. SELECT queries
To create a query, from the ribbon we chose, Create, then Query Design.

Fig. 1. QBE Windows

Microsoft Access 2007

Page 1

Lecture V
In the Show Table window, the data source is being selected. For each table we will be double click the name of the table fig. 1. If we select two tables that are not directly connected, we must select all the tables in between, like in the fig. 1, for Suppliers and Products, we also selected Invoices and InvoiceLines. After closing this window, if we want to open the window to add another table, we select the button with the same name Show Table from the ribbon, Design option menu. If we accidentally added a table, we can delete it by right clicking on it and choose Delete. After the tables were selected, the Show Table windows should be closed. The next step is selecting the fields from the tables. We can select fields by double-clicking the name of the field. Lines Field and Table will be completed automatically. The Sort line orders the data Ascending or Descending. If we want to impose a criteria on a field but hide it from the final visualization, we can uncheck the check-box from the Show. Criteria and Or lines impose criteria. The criteria should be written on Criteria line. The operator between all the criteria is AND. If the operator is OR, the second criteria will be written on the Or line. In order to see the results, from the View button from the ribbon, we can select Datasheet View. From the operators used for constructing criteria we outline: + (adding), - (subtracting), * (multiplying), / (division), MOD (reminder of the division between two numbers), ^ (power of, for example x2 can be written x^2), & (concatenate two strings), = (equality), <> (different), < (lower than), > (greater than), <= (lower than or equal to), >= (greater than or equal to), LIKE (for example LIKE mask, for example SupplierNume LIKE A* will display all the names starting with letter A, but LIKE A?? will display only words that start with the letter A has three characters in length), IN (general format IN (value_list), for example: sex IN (m,f) or [client_type] IN (private,legal person)), BETWEEN (Between min_value AND max_value, for example, if we want to display the students with grades from 9 to 10, we will use BETWEEN 9 AND 10), NOT, AND, OR, NOT NULL, Date() (returns the current date), MONTH (displays the month from a date, for example MONTH(#27.12.2011#) will return value 12), YEAR (displays the year from a date, for example YEAR(#21.11.2011#) will return value 2011), IIF (IIF (logical_criteria, value_if_true, value_if_false)), for example, for the students whos grade is greater than or equal to 9,50 we will display merit, for those between 8 and 9,50 scholarship, and the others nothing, we will write the function: IIF ([student_grade]>9,5; merit; IIF([student_grade] BETWEEN 8 AND 9,50;scholarship;)) ) and so on. The symbol semi-column; with divide the functions arguments depends on the Regional Settings from Control Panel. In this document, we use Romanian settings (Control Panel, Clock Language and Region, Change Location, Location page, Current Location: Romania). Therefore, the date has the following format: dd.mm.yyyy and the decimal point is the comma. If we choose a different location, the symbol that divides the function, the currency monetary symbol and the date format will be changed in consequence. If we want to add a field calculated by a formula, we will select the first blank column, in Design View, and on the Field line we will type the heading name (the caption), followed by semi-column (:) and the calculation formula. Field names should be written between square brackets. For example: ProductValue: [Productquantity]*[ProductPrice]. In order to display the monetary format, from Design View we can right-click on the field name from the Field line, and select Properties, point to Format and choose Currency. Parameters query We can use parameters when creating a SELECT query. If we want to input the values from a field in an interactive way, in order to verify the validity of a criteria and display data from a table, we can create a parameter query. If we want to create a parameter, on Criteria line we will insert a text Microsoft Access 2007 Page 2

Lecture V
between square brackets, for example [Insert invoice date]. This object will be interpreted as a parameter, the environment building an input box that allows us to insert a value from the keyboard. This criteria will be evaluated with this value, and the corresponding records will be displayed, the ones that verify the condition [invoice date]=the value we inserted into the input box.

2. TOTAL Queries Select query can be transformed into a Total query by selecting the button Totals from the ribbon. As an immediate effect, a new line named Total will be displayed, completed with the Group by value. In order to create a Total query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. we select the fields that group the data and the fields that need to be calculated (for example, if we want to display the grade of the entire year of students, we choose fields year and grade. WE can add more than one grouping fields and more than one calculated fields; 4. we select the button Totals. The Totals line will be filled in with Group By values used to define the groups of records; 5. for the calculated fields, instead of Group by we will choose the appropriate function (SUM, MIN, MAX, COUNT, AVG, etc) or WHERE that allows adding additional criteria. If we select Where, we can use all the operators described above; 6. if we want to hide some fields, we uncheck the correspondent check box from the Show line; 7. to display the result, from the View button, select Datasheet View.

3. CrossTab Queries CrossTab query is specific to multidimensional data analysis, similar to pivot tables in Microsoft Excel. In order to create a CrossTab query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. from Design menu tab, we will select the CrossTab button, so Total and Crosstab lines will be displayed; 4. we will select the fields that we want to be displayed as a result; 5. we will select the grouping field(s) using Group By, and the function in the Total line; 6. in the Crosstab line, we will select Row Heading (one or more fields) those fields that will be displayed as row heading, Column Heading for the field that will be displayed on the vertical, (we are not allowed to add more than one field as a Column Heading) and Value for the values that will be displayed at the intersection from lines with columns (we are not allowed to add more than one field as a Value).

Microsoft Access 2007

Page 3

Lecture V
4. ACTION queries Action queries dont display any result, they just manage data from different tables. The operations that can be performed with an action query are: Make Table, Append, Update i Delete. We have to set the security level on a lower setting, choosing Options from Security Warning toolbar, then selecting the option Enable this content.

4.1 MAKE TABLE query Queries that create a new table are used to link the tables together. In order to create a Make Table query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. we can select fields by double-clicking the name of the field. Lines Field and Table will be completed automatically; 4. we can add criteria using the line with the same name; 5. we can verify the result from the View button we can select Datasheet View, and then return to Design View; 6. we transform the select query into an action query by pressing the Make Table button from the ribbon. In the Make Table window we will type the name of the new table that will be created fig.1; 7. save the query; 8. execute it by pushing the Run button from the ribbon.
After we close the query, each time we double-click the name of the query, in order to open it, nothing opens, but the action of creating the table is performed, and the new table is rewritten. After we close a query, if we want to open it in edit mode, right-click on the name of the query and choose Design View.

Fig. 1. Make Table Query We can notice that the icon next to the Make Table query is different than the one of a Select query. Microsoft Access 2007 Page 4

Lecture V
4.2 UPDATE query Update queries manage updating the values from one or more fields (for example, raining he price of bread with 2%). In order to create an Update query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. select the field whos value should be updated and the one that limits the update to certain records (in our case, Product name and Product price. For the product name we will insert a criteria on the Criteria line (=bread); 4. save the query; 5. we can verify the result from the View button we can select Datasheet View, and then return to Design View; 6. we transform the select query into an action query by pressing the Update button from the ribbon. In the QBE window, a new line named Update To will be displayed. On that line, under the field Product price, we will write to calculation formula: [Product Price]*1,02; 7. save the query; 8. execute it by pushing the Run button from the ribbon.
After we close the query, if we double click on the name of the query, the price of bread will raise again and again, even though nothing will be displayed on the screen. After we close a query, if we want to open it in edit mode, right-click on the name of the query and choose Design View.

We can notice that the icon next to the Update query is different than the one of a Select query.

4.3 APPEND query Append queries add records from other tables with the same data type fields. In order to create an Append query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. select the fields that contains the values that we want to transfer to the other table, The fields from both the source and the destination tables should be of the same data type; 4. save the query; 5. we can verify the result from the View button we can select Datasheet View, and then return to Design View; 6. we transform the select query into an action query by pressing the Append To button from the ribbon. We will select the destination table; a new line named Append to will be displayed. If the correspondent fields have different names, this line will remain blank, so we will have to choose the correspondent name; 7. save the query; 8. execute it by pushing the Run button from the ribbon.

Microsoft Access 2007

Page 5

Lecture V
4.4 DELETE query This type of query is used to automatically delete records from one or more tables. In order to create a Delete query we must: 1. create a query: Create/Query Design; 2. select the data source (from the Show Table window, we can select tables by doubleclicking their names), then we close the Show Table window. Attention!!! All tables should be linked!!!; 3. select the field(s) on which we will impose the deletion criteria; we can also select Criteria or Or; 4. we can verify the result from the View button we can select Datasheet View, and then return to Design View; 5. save the query; 6. we transform the select query into an action query by pressing the Delete button from the ribbon. On the Delete line the Where clause will be displayed, and we can create the delete condition; 7. save the query; 8. execute it by pushing the Run button from the ribbon.
When dealing with more than one table, the delete operation can be prevented by the using of the referential integrity rules (if in the Relationship window, we double click the relationship, and select Enforce Referential Integrity and we dont check Cascade Delete Related Records).

Microsoft Access 2007

Page 6

Vous aimerez peut-être aussi