home *** CD-ROM | disk | FTP | other *** search
/ Computer Based Training: …, Windows 95 & NT Basics / a1454501.iso / mo97a03 / AA01.TXT < prev    next >
Encoding:
Text File  |  1997-05-14  |  16.5 KB  |  336 lines

  1. With Microsoft Access 97 you can easily define @Rqueries@r in your databases.
  2. |
  3. A query is a database object that provides a custom view of data from one or more tables, or from other queries.
  4. |
  5. Queries hold the key to the power of a relational database, enabling you to bring related data together in different ways.
  6. |
  7. Access uses a variant of the @RS@rtructured @RQ@ruery @RL@ranguage (SQL) to generate queries.
  8. |
  9. SQL is an acronym, sometimes pronounced "sequel".
  10. |
  11. SQL is an English-like database command language that evolved from SEQUEL (@RS@rtructured @RE@rnglish @RQue@rry @RL@ranguage) - developed by IBM in the early 1970s.
  12. |
  13. A version of SQL has been adopted as the official standard for relational databases by the American National Standards Institute (ANSI) and the International Standards Organization (ISO).
  14. |
  15. However, most software companies have implemented variations of the standard to handle specific features of their products.
  16. |
  17. Access complies with the ODBC (@RO@rpen @RD@rata@Rb@rase @RC@ronnectivity) standard.
  18. |
  19. The ODBC standard is a @RC@rommon @RL@ranguage @RI@rnterface (CLI), aimed at enabling different relational database systems to communicate directly with each other in a standard way.
  20. |
  21. This enables it to "talk" to the many other database applications that now also support the ODBC standard, provided you install the appropriate database drivers.
  22. |
  23. To install or modify your ODBC setup, you double-click the @U32bit ODBC@u icon in the Control Panel, and add the required driver on the User DSN tabbed page.
  24. |
  25. Each query you create in Access is stored as a SQL command.
  26. |
  27. You do not need to be familiar with SQL to define queries in Access - you can use a design grid or a Query Wizard.
  28. |
  29. However, you also have the option of creating or editing queries by entering statements directly in SQL.
  30. |
  31. For more information on SQL see the CBT Systems course @RSQL@r @RProgramming@r.
  32. @@
  33. There are three main types of query you can define in Microsoft Access:
  34.  
  35. ò select queries
  36. |
  37. ò crosstab queries
  38. |
  39. ò action queries
  40. |
  41. A @Rselect@r query is one that retrieves information from the tables or other existing queries in your database.
  42. |
  43. These are useful for sifting through table and retrieving records, or combinations of records, that meet particular criteria.
  44. |
  45. Access creates a @Rrecordset@r of the selected data at the time that you run the query.
  46. |
  47. You can work with a recordset in much the same way as you would with a table.
  48. |
  49. But unlike a table, a recordset is not actually stored in your database - it is generated each time you open or run a query.
  50. |
  51. A @Rcrosstab@r query displays calculated values (such as sums, counts, or averages) from a field in a table, cross-tabulated according to entries from other fields.
  52. |
  53. These field entries are listed down the left-hand side and across the top of the datasheet resulting from the query.
  54. |
  55. With an @Raction@r query you can make changes to many records in one operation.
  56. |
  57. There are four kinds of action query:
  58.  
  59. ò delete queries
  60. |
  61. ò update queries
  62. |
  63. ò append queries
  64. |
  65. ò make-table queries
  66. |
  67. A delete query deletes a group of records from one or more tables in your database.
  68. |
  69. With an update query you can make global changes to a group of records in your tables.
  70. |
  71. An append query enables you to add fields or records to existing tables.
  72. |
  73. You can create a new table from all or part of the data in your other tables using a make-table query.
  74. @@
  75. Suppose you have a table called "Customers" in your database.
  76. |
  77. The table contains information about the companies that are your customers.
  78. |
  79. You wish to create a select query to extract all the records from the table, but display only certain fields.
  80. |
  81. For example, it may be useful to extract only the Company name, Contact name, Telephone, and Fax fields from the table.
  82. |
  83. To create a simple query based on a single table, you select the table (on the Tables tabbed page in the Database window) and choose @UQuery@u from the @UNew Object@u button's drop-down list.
  84. |
  85. If the last new object you created was a query, the @UQuery@u icon will be displayed on the button, and you can click the button itself (without using the drop-down list).
  86. |
  87. In the New Query dialog box, you click @UOK@u to create a new query without using a wizard.
  88. |
  89. This opens the Query window in Design view.
  90. |
  91. In this view, the top part of the window shows field lists for the tables (or queries) you chose for the new query.
  92. |
  93. The bottom part of the window is the design grid that you use to design a query.
  94. |
  95. Each column in the grid represents a field (from the chosen table) that you work with in the query.
  96. |
  97. These fields could be fields you want in the resulting recordset, fields you want to sort, or fields you want to test for values.
  98. |
  99. You can also generate custom field names (to display in the recordset) or use complex expressions to generate a calculated field.
  100. |
  101. You can select a field from your chosen table using the drop-down list in the first column.
  102. |
  103. When you do this, the name of the table is inserted automatically below the field name.
  104. |
  105. When you click the @UField@u row in the second column, the drop-down list button becomes available again.
  106. |
  107. You can use it as before to select a second field for your query from the Customers table.
  108. |
  109. You repeat the process until the fields you require are shown in the design grid.
  110. |
  111. Should you wish to sort the records, you can choose the sort order based on a particular field from the drop-down list in the Sort row of the relevant column.
  112. |
  113. If you do not choose to sort the records, they will be displayed in the recordset in the order in which they are retrieved from the database table.
  114. |
  115. You can sort on multiple fields as well - in which case Access implements your sorting criteria from left to right in the design grid.
  116. |
  117. When you are satisfied with the design of your query, you can click the @URun@u button to see the resulting recordset.
  118. |
  119. Alternatively, you can select the @UDatasheet@u @Uview@u of your query from the drop-down menu on the @UView@u button.
  120. |
  121. Notice that the @UView@u button's menu shows the Design view to be the active view.
  122. |
  123. Also, the @UView@u button itself shows the @UDatasheet View@u icon. In this case, you could simply click the button (without using the drop-down menu) to see the Datasheet view.
  124. |
  125. The Datasheet view shows the results of your select query.
  126. |
  127. The column headings in the list are the four fields you selected from the Customers table.
  128. |
  129. And the records are listed in alphabetical order of Company name (according to the ascending sort order you chose for that field).
  130. |
  131. You can format the Datasheet view of the query just as you would any other database object.
  132. |
  133. You can return to the Design view of your select query by clicking the @UView@u button (which shows the @UDesign View@u icon).
  134. |
  135. Finally, you click the @UClose Window@u button from the current view of the query.
  136. |
  137. You click @UYes@u to save the design of your select query.
  138. |
  139. Then you type a name for the query in the Save As dialog box, before clicking @UOK@u.
  140. |
  141. You are returned to the Database window, where you can click the @UQueries@u tab to see a list of your queries.
  142. |
  143. Your new select query is the only one you have created in this database.
  144. @@
  145. The easiest way to create a select query based on more than one table is to use the Simple Query Wizard.
  146. |
  147. You click the @UNew@u button on the Queries tabbed page to open the New Query dialog box.
  148. |
  149. Alternatively, you can use the @UNew Object@u button.
  150. |
  151. Then you select @USimple Query@u @UWizard@u from the list, and click @UOK@u.
  152. |
  153. The first step is to choose the fields you want in your query.
  154. |
  155. These can be from any existing tables or queries in your database.
  156. |
  157. You move a highlighted field from the Available Fields list to the Selected Fields list by clicking the right arrow button.
  158. |
  159. Then you can choose another table to provide fields for the query.
  160. |
  161. Again, you select the fields you require from the table (or query) using the arrow buttons.
  162. |
  163. When you are satisfied with the selection, you click @UNext@u to continue.
  164. |
  165. You wish to create a detail query, so you click @UNext@u again.
  166. |
  167. Access suggests a name for the query which you can accept or type over with a name of your choice.
  168. |
  169. The final choice given by the Simple Query Wizard is between opening the query and modifying its design.
  170. |
  171. If you click the bottom radio button, the wizard will take you into the Design view of the query.
  172. |
  173. You wish to open the query, so you click @UFinish@u.
  174. |
  175. The results of your new query are displayed in Datasheet view.
  176. |
  177. The @UView@u button shows the @UDesign@u @UView@u icon, so you can click it to see the field lists and design grid for the query.
  178. |
  179. All the tables you selected fields from, using the Wizard, are shown in the top part of the screen.
  180. |
  181. You will see that there are lines joining fields in the different tables.
  182. |
  183. These join lines are created automatically by Access if the relationships were previously defined.
  184. |
  185. If relationships are not already defined, you can create join lines yourself by dragging a field from one table to the equivalent field in another table.
  186. |
  187. The symbols above the lines show the relationships to be the one-to-many type.
  188. |
  189. These symbols are displayed only if referential integrity is enforced.
  190. |
  191. You can see the properties of a join by double-clicking the join line, or by right-clicking the join line and selecting @UJoin Properties@u.
  192. |
  193. Right-clicking a join line also gives you the option to delete the join.
  194. |
  195. The first option in the Join Properties dialog box is the default setting, and represents an @Rinner@r @Rjoin@r.
  196. |
  197. This type of join selects only those records that have matching values in the joined fields from both tables (or queries).
  198. |
  199. You can change the join type by selecting either the second or third option, before clicking @UOK@u.
  200. |
  201. The arrowhead on the join line indicates that this is now an @Router@r @Rjoin@r.
  202. |
  203. In this case, the Sales table contributes all of its records, even if there is no matching "Product ID" field in the Products table.
  204. |
  205. With an outer join, all records from one of the tables are returned. Where there are no corresponding records in the other table, the recordset will contain empty cells in the relevant field.
  206. |
  207. If you are interested in seeing (or editing) the SQL statements Access has stored with your query, you can select @USQL View@u from the @UView@u button's menu.
  208. |
  209. Another way of doing this is to choose @USQL View@u from the @UView@u menu.
  210. |
  211. This gives you a useful way of learning about SQL - you can create a query using the design grid or a Wizard, and then switch to SQL View to see how Access interprets the query in SQL statements.
  212. |
  213. Now you can move back to other views of the query, or click the @UClose@u @UWindow@u button to close the query.
  214. |
  215. You click @UYes@u to save your changes and return to the Database window.
  216. @@
  217. Suppose you now wish to design a select query based on a number of tables, and with multiple criteria.
  218. |
  219. First you click the @UNew@u button on the Queries tabbed page (or click the @UNew@u @UObject@u button displaying the @UQuery@u icon).
  220. |
  221. The design grid gives you more flexibility than the Simple Query Wizard when creating select queries, so you click @UOK@u.
  222. |
  223. The Show Table dialog box allows you to place the tables or queries you want to use in the top part of the screen.
  224. |
  225. You click the @UAdd@u button to show the selected table.
  226. |
  227. When you have chosen the tables you require, you click @UClose@u.
  228. |
  229. Now you use the drop-down lists as before to select fields in the design grid.
  230. |
  231. Suppose you then wish to apply selection criteria to filter the records returned in the query.
  232. |
  233. For example, you may be interested in the sales of products having Product IDs 2, 3, or 4 in Boston and San Francisco.
  234. |
  235. First you type @RBoston@r at the Criteria row for the City field.
  236. |
  237. When you click the next or row, Access adds the required quotation marks around the text you typed for the first criterion.
  238. |
  239. If you are testing a text field, quotation marks are required.
  240. |
  241. <f>Now you type @RSan@r @RFrancisco@r at the or row in the City field.
  242. |
  243. To select Product IDs 2, 3, and 4, you type the clause @RBetween@r @R2@r @RAnd@r @R4@r at the Criteria row in the Product ID field.
  244. |
  245. You can see the results of the query in Datasheet view by clicking the @UView@u button.
  246. |
  247. The resulting recordset contains records with Boston @Ror@r San Francisco in the City field, @Rand@r Product IDs between two and four inclusive.
  248. |
  249. When you click the @UClose Window@u button, you are prompted to save the query by clicking @UYes@u.
  250. |
  251. Then you type a name for the query before clicking @UOK@u.
  252. |
  253. Remember that this saves only the query - and not the recordset.
  254. @@
  255. Sometimes it is useful to base a new query on an existing query in your database.
  256. |
  257. For example, an existing query might already contain all the fields you need for your new query.
  258. |
  259. This saves you from having to select fields from individual tables when you design the query.
  260. @@
  261. It's easy to make changes to existing queries in Microsoft Access.
  262. |
  263. You select the query you want to edit or customize, and click the @UDesign@u button.
  264. |
  265. This opens the Design view of the query, where you can make the changes you require.
  266. |
  267. Suppose you wish to add a new field to the query.
  268. |
  269. First you highlight the column to the right of where you want the new field to be, and select @UInsert - Columns@u.
  270. |
  271. You need to add another table to the top part of the screen, so you click the @UShow Table@u button.
  272. |
  273. You select the table you require (@UProducts@u) and click @UAdd@u.
  274. |
  275. You can also add a table or query to a query by dragging the table or query name from the Database window to the top part of the query in Design view.
  276. |
  277. Then you click the @UClose@u button to return to the design grid.
  278. |
  279. The field you wish to add to the query is now available from the drop-down list in the Field row of the new column.
  280. |
  281. Sometimes it is useful to apply a filter to a field, without showing that field in the recordset; you can do this by clearing the appropriate @UShow@u checkbox.
  282. @@
  283. Now suppose you would like to calculate a new field for the query from existing fields in the database.
  284. |
  285. For example, you could create a field that shows the result of multiplying the Unit Price and Units sold fields from the Sales table.
  286. |
  287. You place the cursor in an empty column in the design grid.
  288. |
  289. Then you click the @UBuild@u button to create a new calculated field.
  290. |
  291. This opens the Expression Builder dialog box, which gives you an easy way of entering expressions.
  292. |
  293. You open the Sales table folder and select a field to include in the expression.
  294. |
  295. You click the @UPaste@u button to place the selected field in the expression.
  296. |
  297. The field is inserted (with the correct syntax) in the top window.
  298. |
  299. Now you click the @U*@u button to include a multiplication operator in the expression.
  300. |
  301. Then you select @UUnits sold@u and click @UPaste@u to add that field to the expression.
  302. |
  303. When the expression is complete, you click @UOK@u.
  304. |
  305. Access inserts the expression in the Field row of the chosen column.
  306. |
  307. When you press @UEnter@u you will see that Access has named the new field "Expr1".
  308. |
  309. This name will be shown as the field name when you run the query, or view it in Datasheet view.
  310. |
  311. To change the name of a field, you click any row in that field's column in the design grid, and click the @UProperties@u button.
  312. |
  313. In the Field Properties dialog box you can type a description of the field, and a caption for the field.
  314. |
  315. When you close the dialog box, the caption is not reflected in the expression name.
  316. |
  317. However it is shown as the field name when you switch to the Datasheet view of the query.
  318. |
  319. Notice that the Product ID field (to which a filter criterion is applied) is not displayed, as its @UShow@u checkbox was cleared in the design grid.
  320. |
  321. To save the query with the changes you have made to it, you select @USave As/Export@u from the @UFile@u menu.
  322. |
  323. You click @UOK@u to retain the query's current name.
  324. |
  325. If you wish to print the results of the query, you click the @UPrint@u button.
  326. |
  327. You can return to the Database window by clicking the @UClose Window@u button.
  328. @@
  329. Suppose you want to make changes to an existing query in your database.
  330. |
  331. For example, the current database has two fields called "Name" - one in the Products table, and another in the Categories table.
  332. |
  333. If one of the "Name" fields is retrieved by the query, it might be useful to change the field caption in the recordset to avoid any ambiguity.
  334. @@
  335. @@
  336.