Professional Documents
Culture Documents
These handouts are available in the Blackboard course as individual files, but are provided here in one group.
Creating a New Database in Microsoft Access 2010 ................................................................................ 3 Create a Blank Database ....................................................................................................................... 3 Opening a Shared Database from the a read-only drive (like the S: drive) .............................................. 3 Opening a Previously Saved Database from Your N: Drive or Flash Drive................................................ 4 The Security Warning Dialog ..................................................................................................................... 4 Backing Up Your Database ........................................................................................................................ 4 Microsoft Access 2010 Data Types ........................................................................................................... 5 Number Types in Access ........................................................................................................................... 5 Using the Query Design Tool in Access ......................................................................................................... 6 Running Your Query .................................................................................................................................. 8 Saving Your Query ..................................................................................................................................... 8 Relational Operators in Access 2010 ............................................................................................................ 9 And .............................................................................................................................................................. 10 Or ................................................................................................................................................................ 10 Calculated Fields ..................................................................................................................................... 11 Defining a Calculated Field...................................................................................................................... 11 Text Fields and Concatenation ............................................................................................................ 12 Formatting a Calculated Column ............................................................................................................ 12 Date Functions in Access......................................................................................................................... 14 Using Format() to display dates .............................................................................................................. 15 Like and Wildcards .................................................................................................................................. 16 Wildcards: ........................................................................................................................................... 16 Totals Queries ......................................................................................................................................... 18 Counting Records and Displaying Totals ............................................................................................. 18 To create a Totals Query: ........................................................................................................................ 20 Additional Calculations ....................................................................................................................... 20 Adding Additional Criteria to a Totals Query ...................................................................................... 21
Page 1 of 36
Parameter Queries ...................................................................................................................................... 23 Creating a Parameter Query ................................................................................................................... 24 How do I know what to type in the square brackets? ............................................................................ 25 Update Queries ....................................................................................................................................... 26 Creating an Update Query .................................................................................................................. 26 Adding Criteria .................................................................................................................................... 26 Running an Update Query................................................................................................................... 27 My Update Query doesnt work! ........................................................................................................ 27 Creating a Report in Access ........................................................................................................................ 28 Creating the Report: ............................................................................................................................... 28 Grouping and Sorting: ............................................................................................................................. 30 Totals:...................................................................................................................................................... 30 Conditional Formatting: .......................................................................................................................... 30 Mail Merge .................................................................................................................................................. 31
Page 2 of 36
Opening a Shared Database from the a read-only drive (like the S: drive)
Since Access works with files differently than other Office applications, you cant just double-click on a database file on a read-only drive to open it. Copy the database file your desktop, flash drive, or N: drive first, and then double-click on it to open it. Since Access saves changes while you are working, the database you are using to practice must be writable by youyou arent allowed to save things to a readonly drive like the S: drive, so Access will complain is youre trying to work with a database there.
Page 3 of 36
This warning is to help protect you from viruses and worms that can be transmitted via Access files. If you receive Access databases from sources you do not trust (like unsolicited emails!), be wary of them! However, for this class, you can be reasonably sure that the databases I share with you are virus-free. And you can also be sure that databases youve created are! Before you can work in Access, click the Enable Content button in the warning bar. You can also click on the File button, choose Info, and then choose Enable Content next to the security warning. The security warning will disappear, and youll be able to work in Access.
Backups are a great idea if youre going to be making a lot of changes to a database, especially as you learnits the only way to return to a previous version of your data, since all changes you make are saved automatically!
Page 4 of 36
Memo Number
Hyperlink Attachment
Byte For integers that range from 0 to 255. Storage requirement is a single byte. Integer For integers that range from -32,768 to +32,767. Storage requirement is two bytes. Long Integer For integers that range from -2,147,483,648 to +2,147,483,647. Storage requirement is four bytes. Single For numeric floating point values that range from -3.4 x 1038 to +3.4 x 1038 and up to seven significant digits. Storage requirement is four bytes. Double For numeric floating point values that range from -1.797 x 10308 to +1.797 x 10308 and up to 15 significant digits. Storage requirement is eight bytes. Replication ID For storing a globally unique identifier that is required for replication. Storage requirement is 16 bytes. Note that replication is not supported using the .accdb file format. Decimal For numeric values that range from -9.999... x 1027 to +9.999... x 1027. Storage requirement is 12 bytes.
Page 5 of 36
Click on the Query Design button to begin creating a new query. NOTE: The Query Wizard tool can be helpful to start with, but you will need to enter Query Design mode to modify your queries even if you use this tool.
The first step when creating a query is to select one or more tables to use as the data source. Click on the name of the table to highlight it, then click Add to add that table to your query.
[NOTE: You can return to this item by clicking on the Show Table button while editing your Query.]
Page 6 of 36
Once youve chosen tables for your query, you can add fields to display by choosing them from the drop-down box near the bottom of the window. If you are using multiple tables, field names will be prefaced by the table name, followed by a period. For example, the Title field of the Movies table will be denoted by Movies.Title. The TableName.* item (for example, Movies.*), is a special item that represents all of the fields on that table. This is useful if you query should return all the available fields!
Each column in the design view represents a column that will be used when your query is run. If the Show checkbox is checked, that column will display in the final results. If the column is unchecked, it will not display in the query output.
In the Criteria area, enter any selection criteria that will limit the amount of data that is displayed. If no criteria are entered, all data from the table(s) will be displayed. The Criteria area requires the use of relational operators to properly define what youre looking for some operators are: = Equals > Greater Than < Less Than >= Greater Than or Equal To <= Less Than or Equal To <> Not Equal To
Page 7 of 36
Besides using the symbols above, here are some general rules for the correct formatting of your Criteria statements: The relational operator symbols (like those above) go BEFORE your criterion. (example: >50 or =USA) Any text that youd like to match should go in double-quotes (example: USA, Robert Redford Numbers should NOT go inside quotes. Commas and currency markers such as the dollar sign ($) should NOT be used. (example: >50000 NOT >$50,000 ) Dates should be formatted like this: #mm/dd/yyyy# (example: # 10/31/2011#)
Also, dont forget: Adding additional items to the Criteria row will make your query more specificthat is, it will narrow your results. (This is also called the And row.) Adding items to the or: row or below will make your query more generalthat is, it will tend to find more matches, increasing your results.
When in query view mode, you may return to the query design mode by clicking on the View button near the upper left corner of the window.
Page 8 of 36
Page 9 of 36
And
Logical And decreases the amount of results by limiting the criteria. For example a query to return Computer Science Majors And English Majors from a student database returns only double majors in Computer Science and English, that is, only records that match both criteria. The matching area below is shown in darker blue. In Access, the And keyword can be used, or criteria can be added across columns in the Query Design View.
Match
English Majors
Or
Logical Or increases the amount of results by expanding the criteria. For example a query to return Computer Science Majors Or English Majors from a student database returns all Computer Science majors, all English Majors, and all double majors in Computer Science and English. The matching area below is shown in dark blue, and includes all parts of both circles. In Access, the Or keyword can be used, or criteria can be added to the or: rows.
English Majors
Page 10 of 36
Calculated Fields
A database often contains information that can be calculated from one or more existing fields. For example, if a database for customer orders includes a PricePerUnit field, and an UnitsPurchased field, its easy to calculate total pricejust multiply PricePerUnit by UnitsPurchased. Since the data stored in the database allows us to easily calculate the total price, it would be redundant (and a potential source of errors) to have another field just to hold the total price.
But since the TotalPrice is a very useful bit of information, Access allows us to create a Calculated Field in a query. A calculated field does calculations on existing fields, and shows up as a column in a query just like a regular data field. The only difference is that the value in the calculated field is not stored in the database, it is calculated every time the query is run. This is nice behavior because if ever any of the values in a field change (for example, the PricePerUnit goes up), the calculated field will automatically update as well.
This is telling Access to make a column in our query called TotalSales that will be equal to PricePerUnit times UnitsPurchased. Access will automatically calculate this value for every row in our query.
Page 11 of 36
Make sure each field name is surrounded by square brackets, and also pay attention to spellingthe field names must match exactly. If you get errors or a pop-up window asking for a Parameter, you have most likely spelled a field name incorrectly, or forgotten the square brackets.
Text Fields and Concatenation Besides being able to use arithmetic operators on numeric data types, you can use the ampersand character (&) to concatenate text data types together. Concatenation means merging text together, end to end. For example, in a record with a FirstName value of Homer, and a LastName value of Simpson the following:
Will output: Homer Simpson the & operator takes all of the text and concatenates it together.
This operator is useful to format columns to display text fields in a certain wayfor example, in address fields or name fields.
In Query Design view, right-click on any column and choose Properties to display the Property Sheet for that columnit will appear as a pane on the right side of the Query
Page 12 of 36
Design window. Alternately, you can click once a column to select it, and then click on Property Sheet in the Query Design ribbon to display the Property Sheet.
In the Property Sheet, choose the Format you would like for the column. Some options are:
Currency (like $42.57) Fixed (a fixed number of decimal places, like 42.57) Percent (like 42.57%) Short Date (like 10/31/2011) Long Date (like Monday, October 31, 2011
Choosing an option in the Format row will force that column to appear in that format. This technique can be used for all columns in your query.
Page 13 of 36
Will give you the number of days that have passed since the HireDate. Below is a list of useful date functions:
Function Date() Now() Time() Day([datefield]) Format([datefield], <format>) Hour([timefield]) Description Gives today's date. Returns the current date and time. Returns the current time. Returns the day of the month (1 .. 31) of [datefield]. Returns [datefield] as a string of text in the format specified. (See below for examples.) Returns the hour of [timefield] as a number from 0 to 23. Note that midnight is 00:00 and that means that midnight "tonight" is actually tomorrow. Returns the minute of [timefield] as a number from 0 to 59. Returns the month number of [datefield]. Returns the seconds component of [timefield] as a number from 0 to 59. Returns the year number of [datefield]. Gives the day of the week as a number from 1 to 7.
Page 14 of 36
When using Format(), remember that the resulting value is a stringa data type for text. You'll use Format() to display dates, but don't use it when you must use the results in mathematical equations, for example, when comparing datesif you want to compare dates, use Year(), Month() or Day() instead!
Page 15 of 36
Criteria Statement Like * Like B* Like *red Like 1/1/* (for a date field) Like C??
Matches Anything Anything that begins with B, e.g. Bob, Bill, B, Breakfast Anything that ends with red, e.g. tired, Fred, red, Winnifred Dates on January 1st of any year Any three letter word starting with c, e.g. cat, car, cot. Each match MUST have three letters. Anything that begins with A, B, or C, e.g. Acrobat, Bottle, Chant Anything that begins with D or L, e.g. Donut, Lake, Discount
Page 16 of 36
Page 17 of 36
Totals Queries
In a regular query on an employee info database, we can return a list of everyone who lives in River Forest, IL by creating a query like this:
Our criteria for City and State are set, such that well return a list of employees whose City matches River Forest, and whose state matches IL. In a query like this, each matching record will be shown on one row of your query results. Counting Records and Displaying Totals As you recall, a query asks a question of our data. Sometimes, when creating queries, we dont want to see every matching record, but we want to see some summary of the data in the matching records. For example, what if we just want to know how many employees live in each city in our database, but we dont care about the individual matches? In a case like that, we create a totals query to group matches together in our results.
In the image above, on the left you see a query showing a different row for each matchone row for each record. This
On the right, you see a totals query that is summarizing the matches by counting how many times each city and state combination shows up on the list.
Page 18 of 36
means that same city and state combination appears many times.
Page 19 of 36
To create a Totals Query: 1. First, create a query containing your desired fields as you normally would. 2. Next, on the Query Design Ribbon, in the Show/Hide group, click Totals. This is a toggle on or off.
A new Total row appears below the Table row in your datasheet. Youll see that the default setting for each field in the query in the totals row is Group By 3. In the Total row, click the field that you want to count up, and then select Count from the list.
What this query design means is that for every combination of unique values in the City and State field, the number of matching records will be counted up and displayed. 4. Click the Run icon to see the results of the query:
As you see, each City/State combination appears on one line of the results, and the third column displays the number of records that have that combination in the City and State fields. From these results, we can see that 21 records match River Forest, IL. Additional Calculations In addition to counting a number of records, other options in totals queries can be selected: Count, for counting the number of records in a category Sum, for summing a column of numbers.
Page 20 of 36
Average, for averaging a column of numbers. Maximum, for finding the highest value in a field. Minimum, for finding the lowest value in a field. Standard Deviation , for measuring how widely values are dispersed from an average value (a mean). Variance, for measuring the statistical variance of all values in the column.
For example, in this query, we are grouping by the City/State again, but have selected the HourlyWage in the third column, and have chosen Avg.
This query will show the Average HourlyWage for each City/State pair.
Adding Additional Criteria to a Totals Query In order to add criteria to a totals query, for example, to limit our results to just employees in Michigan, we add a column containing the field(s) wed like to apply our criteria to, and choose Where in the Totals row. In the example below, there is a fourth column, the State field, and Where has been chosen in the Total row. The criterion MI has been entered in the Criteria row, and the Show box in UNCHECKED. In fact, if you select Where in the Total row, the show box must be unchecked or youll get an error message when trying to run the query.
Page 21 of 36
The results of our new query show the Average of the HourlyWage fields for each City/State pair, but only for those records where the State field matches MI.
Page 22 of 36
Parameter Queries
A parameter in Access is a value (e.g. a number, a date, or a text string) that is provided by the user, and which can then be used in the criteria area of a query.
When you run a query containing parameters, you will see a box labeled Enter Parameter Value pop up. In this box, you can type a number, date, or text string, and then click OK in order to run a query based on the values you provide.
In the example picture to the right, the query is asking for a year. If you enter 2011, the query will then run, using 2011 as the value for the year. EACH TIME a parameter query is run, it will prompt for information, meaning that the results of a parameter query will depend on what is entered in the box.
This behavior is very useful for creating general use queries that are very flexibleinstead of writing 50 separate queries to list people by state, you can write one parameter query to ask for a state to look for, saving yourself a lot of work!
Page 23 of 36
By putting [Enter Number of Hours] in square brackets, we are letting Access know to prompt the user for the information that will go in that space. The greater than symbol before the first square bracket indicates that, for whatever value I type in the prompt, I want the WeeklyHours field to show me all values that are greater than that value. If I type 30, my query will match all records where the WeeklyHours field is greater than 30.
Page 24 of 36
Besides not using an existing field name, you can use whatever youd likea good choice is always something phrased as a question, since Access will display that in the dialog box when asking for a response.
[Which Year?] [What is the maximum cost?] [What is the Start Date?]
Remember how Access prompts you to enter information if you misspell a field name in square brackets? Now you know why it does thatif its in square brackets, and it isnt a field name, its a parameter, and Access will ask you for a value!
Page 25 of 36
Update Queries
Queries that allow us to display information are called Select queriesbecause they allow us to select which records to display. Select queries do not modify the underlying data at all. There are other types of queries, however, that can be used to change data in various waysthese are, as a group, called Action Queries. One type of action query is an Update queryan update query updates data in one or more fields by using a formula. Just like select queries, update queries can include criteria that limit matching records. Creating an Update Query Create a new query and select the table(s) that your query should use as a data source in the same way you create a select query. In the Query Type group on the Query Design tab, click on the Update button to indicate that this query should be an Update query. When you select Update, a new row called Update to: will appear in the query designer. Use this row to indicate how a given field will change when the query is run. In the example to the left, the HourlyWage field has been selected. In the update to: row, enter a formula to calculate the new value. Use [square brackets] to surround field names, just like youd use in calculated columns, e.g. [HourlyWage] * 1.05. This formula indicates that for each matching record, the HourlyWage field should be multiplied by 1.05 (a 5% increase). NOTE: The formula in the update to: row will almost always include a reference to one or more fields! And, usually, it will contain a reference the field name for that column. Notice in the example above that the Update to: row in the HourlyWage column contains [HourlyWage] * 1.05. This is, essentially, saying: Update the current HourlyWage field, and make it equal to the current HourlyWage * 1.05. If your update formula has only numbers and no fields, then you will likely be resetting EVERY value in your database to the same thing! Adding Criteria You can add additional columns in an update query for criteriaas long as the Update To: row is blank, that field will not be changed. In the example to the right, our update query will only change records where State is IL.
Page 26 of 36
Running an Update Query You can run an update query by using the run button, just like a select query. However, since an update query changes data, there are some safeguards in place. When you run an update query, you will see a confirmation dialog:
Clicking Yes will update your database permanently, with no chance to undo the changes. Clicking No will do nothing. Notice that you are told how many matching records will be changedpay attention to this numberif it seems too high or too low, click no and recheck your query and criteria. After you click yes on this dialog, an update query runs, and you will not see any further confirmation the data in the database will have changed, however. One way to check if your update query has worked is to look at the database before and after to make sure. My Update Query doesnt work! Since an update query changes your data irreversibly, Access tries to prevent you from accidentally running them. The first defense against accidentally updating your data when you dont want to is the the confirmation dialog. The Security Warning when you first open a database also prevents you from running update queries. If your update query doesnt seem to do anything, look in the lower-left corner of the Access window for a message that says The action or event has been blocked by Disabled Mode. This means that you havent enabled dangerous content. When you first open a database, you see a Security Warning
Click on Enable Content in order to allow your Update query to work. If your update query isnt running and you dont see the Security Warning bar, save your work, then close and reopen the database, this time making sure to click Enable Content from the warning bar.
Page 27 of 36
Other Report features: Sorting: In each City group, employees should be sorted from oldest when hired to youngest when hired. Formatting: HourlyWages between $10 and $15 should be displayed with a red font, underlined. Totals: HourlyWages should be totaled by city, and also show a grand total. The average age of all employees should be displayed at the bottom of the report. The city wage totals should be labeled Total Wages City Name for each city. The report should have a page number in the form Page x of y at the top right of each page, and show your name and the report title in the top left of each page. The report should show the date at the top right of the report.
Page 28 of 36
4. The dotted lines in Layout View represent the page marginsyou can see that some of the columns are spilling over the right margin of the page. Resize the City, LastName, and FirstName columns by clicking on the column heading, then clicking and dragging right-edge of the column to make it skinnier. Do this until all of your columns fit on one page. 5. Double-click on the report title to edit itchange it to Your Name First Report. Use the Font tools on the Format tab to boldface the title, and increase the font size to 24pt. 6. Edit the column heading titles by double-clicking on them to match the titles given in the instructions above. 7. Now is a good time to save your reportuse Ctrl-S, or click on the Save button to save your report. Call it YourName-FirstReport. 8. You might see that the Age When Hired column consists of #s. Just like in Excel, a row of #s indicates that there are too many digits to display in the space given. If the Age column has digits in it, youll see that it lists the age down to 12 decimal place accuracy. Since you dont want to show the age with so many decimal places, adjust to formatting of this column by clicking in the Age column to select it, and choosing Fixed from the Formatting area. Use the decimal point buttons to set the age to display only 1 decimal place. 9. Access has provided a default image in the top left corner of the reportchange this image by clicking on the Logo button in the Design Ribbon, and choosing any digital image to replace it. (You can use the image attached in Blackboard called dominicanlogo.jpg.) 10. Access has also automatically included the date and time in the top-right corner of the report. Remove these boxes by clicking on each to select it, then clicking the Delete/Backspace key, or right-clicking and choosing Delete. Click on the Date and Time button in the Header/Footer group of the Design ribbon and add a box that displays the date in the format 6/13/2012 and does not display the time. Set the background of the date box to red and the font to white. 11. If your title box needs to be resized to show up on one line, do that now by selecting the box and dragging the edges. Almost every item in Layout View can be resized and moved in this way! 12. Add page numbers in the form Page x of y to the top of your report by clicking on the Page Numbers button. Right-align the page numbers. Youll see that the page numbers may appear on top of the column headersmake more space by clicking on any column header and dragging it downward. 13. Scroll all the way down the pageyoull see that Access has automatically added page numbers at the bottom of the report for you. (Notice how it says Page 1 of 1 as if this entire report would fit on one page? In Layout View, there are no page breaks, so you cant tell how many
Page 29 of 36
pages a report will be. To do that, use Print Preview.) Since you just added page numbers at the top of the page, remove the page numbers from the bottom of the page.
Totals:
16. Youll see that Access has automatically added a grand total for the HourlyWage column, but it doesnt show up as currency. Use the Formatting area to display this total as currency. Right-click on the grand total box and choose Set Caption to label the grand total as Hourly Wage Grand Total. 17. Besides Grand Totals, Access can display totals by groupsyou can set these options by right clicking on the HourlyWage column, and choosing an option from the Total menu. Set up your report to show a group sum and grand total for the Hourly Wage, and a grand total showing the average age of all employees when hired. Label the average age using a caption.
Conditional Formatting:
18. You can change the appearance of any single item in a record based on criteria by using the Conditional Formatting button on the Format ribbon. Click on the HourlyWage column to select it, then click the Conditional Formatting button to set conditional formatting criteria. Set HourlyWages between $10 and $15 to display in red with underline.
Thats allyouve now created a complex, well formatted report based on a query!
Page 30 of 36
Mail Merge
One very common use for data, especially contact data, is to use it to send them e-mails or letters. I'm sure you've received form letters or emails that have your name and even some other information about you inside of it--- a person isn't sitting down and typing each one of those letter individually, though. By using a tool called Mail Merge you can take data stored in a database and merge it into a word document multiple times-- for example, to generate a personalized letter to lots of people!
Performing a mail merge operation in Microsoft Word 2010 can be summarized in the following steps: 1. Create the main document The basic document for a mail merge the form letter contains the static text. 2. Connect the document to a data source The data source, a file that contains the information to be merge, can exist in a wide variety of data formatsmost often Access or Excel. 3. Add placeholders (i.e., mail merge fields) to the document Place mail merge fields, such as address blocks or greeting lines, in your main document. These fields are filled with information from your data file. 4. Preview and complete the merge operation As you merge your data into the main document, you can page through the output to verify that the operation worked as you intended. You can then either print out, email, or save copies of the merged document youve created. (steps borrowed from http://oit.wvu.edu/training/files/word2010_mailmerge.pdf )
Page 31 of 36
5. After selecting a data source, if there is more than one table or query in an Access database (or more than one sheet in an Excel database), youll be presented with a window asking you to choose which table/query or sheet to use as the source. Choose the item that has all of the information you need to include in your letter. In this example, well choose the StudentContactList table. 6. When done, click Next- Write Your Letter in the Mail Merge pane.
Page 32 of 36
In your document, youll see the merge field show up as <<AddressBlock>>. This will be replaced by individual addresses! 2. You can add any additional fields from your data source by clicking on More Items and choosing a field from the list. Each merge field will appear in your document with << and >> on either side of it. You can format merge fields (e.g. with font choices, boldface, etc.) just like regular text in Word. In the sample below, you can see weve added the AddressBlock, First_Name, Guidance_Counselor, and Previous_School merge fields into the document.
Page 33 of 36
3. Once youre done adding merge fields, click Next Preview Your Letters to be able to see how the actual data will look inside of your document.
Page 34 of 36
3. If you are printing your letters, use the Print button to print them. If youd like to save your letters as a word document, click Edit Individual Letters. In either case, youll be prompted for which records youd like to mergeselecting All with create a letter for each record in your data source, current will only generate a letter for the current record you were previewing, and the from options allow you to specify a range of records to merge. Clicking OK will complete the process, either printing or creating a new document with all of the information! 4. If you choose Edit Individual Letters, a new word document will open containing the merge each letter will appear on a new page, separated by a Next Page Section Break. Saving this new document will allow you to open it later.
Page 35 of 36
Final Notes
If you save your source document, that is, the document you added the merge fields to, whenever you open this document, Word will attempt to access your chosen data source. This means that, if youve chosen a database that is updated regularly, you can open up your document and immediately generate new letters, since the merge will already be set up! When opening a previously set up mail merge document, youll see a dialog box like this appear:
Clicking Yes will allow word to access your data source. In order to re-run the merge, go to the Mailings ribbon, and choose Finish and Merge, and then you can generate your new documents.
Page 36 of 36