Access 2000 Queries Tips & Techniques Query Basics The query is the basic tl that Access prvides fr retrieving infrmatin frm yur database. Each query functins like a questin that can be asked immediately r saved t be asked later. Althugh this questin will generally remain unchanged (unless yu chse t edit the query), the changing nature f yur database means that the answer Access prvides will generally be different each time the query is run. Access expresses this answer as a table created n the fly. This temprary table, displayed as a datasheet, cntains a subset f the data in the table (r tables) serving as the surce f yur query. This subset can be cmpsed f a select grup f fields in each database recrd, r it can be cmpsed f recrds that meet a certain search criterin. Althugh Access queries are pwerful tls fr retrieving data, a query is perhaps the easiest database bject t create. Access prvides a wizard t assist in the creatin f simple queries, and an easy-t-use Design view fr mre cmplex queries. 1
Creating a query using the query wizard The query wizard makes it simple t set up a query that will retrieve select fields frm every recrd. T begin, g t the Queries area in the Database windw. Next, dubleclick n Create query by using wizard. Next, select the surce f yur query frm the Tables/Queries drp-dwn menu. Queries can draw data frm a table r frm anther query. Once yu have selected yur data surce, Access will display a list f the fields available in that data surce. T select a field t include in yur query, dubleclick n its name in the list f Available Fields. Once yu have dne s, that field will vanish frm the list f Available Fields and appear in the Selected Fields bx. (T remve a field that yu selected, dubleclick n its name in the Selected Fields bx.) Once yu have finished selecting fields, click n Next. 2
T finish yur query, give it a name. Use sme srt f naming cnventin s that yu can easily tell frm its title that yur query is nt a table. Many database develpers designate their queries with a qry_ prefix befre its name. Fr example: Qry_PayRates Once yu have named yur query, click Finish. Yur query will then pen and display the data retrieved frm yur database. Creating a query in design view Design view is useful fr creating mre cmplex queries and fr revising already created queries. In the Queries area f the Database windw, dubleclick n Create query in Design view. Frm the Shw Table windw, dubleclick n any data surce t add it t the query. Tables can be added frm the Tables tab, and already-existing queries can be added frm the Queries tab. After yu have added yur data surce(s), click Clse. 3
T add a field t yur query, dubleclick n its name in ne f the table windws at the tp f the Query windw. Each field will be added t the design grid at the bttm f the Query windw. Click n the Save buttn n the tlbar, and name yur query. Click OK. Click n the View buttn at the tp left f the tlbar t switch frm Design mde t View mde and see the results f yur query. Once yu are in View mde, clicking n this buttn will return yu t Design mde. 4
Making changes t a query Once a query has been created, Access allws yu t repen that query in Design view s that can revise yur query t retrieve a different set f data frm yur database. In the Queries area f the Database windw, click n the query yu wish t alter and click n the Design buttn at the tp f the Database windw. T delete a field frm the query: First, select that field by clicking n the gray bx abve the field s name in the design grid at the bttm f the query windw. Once the field is selected, it will be highlighted in black. Next, delete the field by typing delete n yur keybard. T add a new field t the query frm a table that is already displayed at the tp f the query windw, dubleclick n the field s name inside that table s windw. T add a new field frm anther table: Click n the Shw Table buttn n the tlbar at the tp f the Access windw. Frm the Shw Table windw that appears, dubleclick n any data surce t add it t the query. After yu have added yur data surce(s), click Clse. T add a field frm the new table, dubleclick n the field s name in the new table s windw at the tp f the Query windw. The new field will be added t the design grid at the bttm f the Query windw. 5
Filtering query results Filtering the data returned by yur query allws yu t view nly data that meets a certain cnditin r set f cnditins. One r mre filtering criteria can be added t each field in the query t retrieve nly the recrds that yu want. T create a filter: Open an existing query in Design mde. Decide which field t use in yur filter, and chse a criterin that must be met by any data that is retrieved. In the example belw, qry_test asks the questin What students d I emply, and what is each ne s rate f pay? Use f a filtering criterin might prduce a mre restrictive set f data that answers the questin Which f my students are paid mre than $8 per hur? In this case, the chsen filtering criterin is greater than $8/hur. Other criteria wuld prduce different sets f data. In the design grid, click in the Criteria cell fr the field that will receive a filtering criterin. 6
Next, type the filtering criterin in the Criteria cell fr yur field. Filters can be created using the fllwing peratrs: > greater than < less than = equal t >= greater than r equal t <= less than r equal t Click n the Save buttn n the tlbar. Click n the View buttn at the tp left f the tlbar t switch frm Design mde t View mde and see the results f yur query. Once yu are in View mde, clicking n this buttn a secnd time will return yu t Design mde. Srting query results Access allws yu t srt the data retrieved by a query s that it will be displayed in a lgical rder. Open an existing query in Design mde. Decide which field t use in srting yur data, and find that field s clumn in the design grid In the example we just used, qry_test asks the questin What students d I emply, and what is each ne s rate f pay? One reasnable way t srt the retrieved data in decreasing rder by rate f pay. Click n the dwn-facing arrw t the right f the Srt cell fr the field that will be used t srt the data. 7
Frm this Srt menu, chse the srt rder yu want. An ascending srt rder will srt yur data frm the smallest value in the srt field t the largest value. A descending srt rder will srt yur data frm the largest value in the srt field t the smallest value. Click n the Save buttn n the tlbar. Click n the View buttn at the tp left f the tlbar t switch frm Design mde t View mde and see the results f yur query. Creating a parameter query Parameter queries are interactive filters that allw yu t specify a different value fr a filter criterin every time yu pen the query. Open an existing query in Design mde. Decide which field t use in yur filter. In the design grid, click in that field s Criteria cell. If yur parameter query requires an peratr, type that peratr in the Criteria cell. Cmplete yur filter expressin with a text phrase in square brackets. This bracketed phrase shuld describe the variable value that yu ll be asked t supply every time the query is pened. Click n the Save buttn n the tlbar. Click n the View buttn at the tp left f the tlbar t switch frm Design mde t pen yur query in View mde. When yu d, Access will prmpt yu t enter the numeric value t be used in yur filter. After yu enter this value, Access will retrieve yur data frm the database. 8
Creating calculated cntrls Calculated cntrls allw yu t make a mathematical calculatin r jin multiple strings f text n the fly when yu run yur query. Numeric calculatins T create a new calculated cntrl, first click in an empty Field cell. Next, type the name f the newly-calculated result, a cln, and the frmula t be calculated. Refer t any fields in yur calculatin by entering their names surrunded by square brackets. Yu can use the fllwing mathematical peratrs: * (multiplicatin) / (divisin) + (additin) - (subtractin) Fr example, t calculate the amunt f mney paid t each student emplyee per week, type: WeeklyPay:[Pay Rate]*[Hurs per week] Save yur query, and use the View buttn t check the results. Trublesht yur calculatin as necessary. 9
Cncatenatin T create a new calculated cntrl, first click in an empty Field cell. Next, type the name f the newly-calculated result, a cln, and the frmula t be calculated. Refer t any fields in yur calculatin by entering their names surrunded by square brackets. Incrprate any text that wn t change (including spaces) inside qutatin marks. Use an ampersand character (&) t jin any tw strings f text in yur expressin (ne fields with anther, the cntents f a field with text in qutatins, etc.) In this example, type the fllwing t cncatenate the First Name and Last Name fields. (Nte that space within the qutatin marks.) FullName:[First Name]& &[Last Name] Save yur query, and use the View buttn t check the results. Trublesht yur calculatin as necessary. 10