Day 18: MICROSOFT ACCESS – CHAPTER 3 CONTD.

44
DAY 18: MICROSOFT ACCESS – CHAPTER 3 CONTD. Akhila Kondai [email protected] October 21, 2013

description

Day 18: MICROSOFT ACCESS – CHAPTER 3 CONTD. Akhila Kondai [email protected] October 21, 2013. Exercise. Go to the lecture notes page and Download Music_extended.accdb - PowerPoint PPT Presentation

Transcript of Day 18: MICROSOFT ACCESS – CHAPTER 3 CONTD.

Page 1: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

DAY 18:MICROSOFT ACCESS – CHAPTER 3 CONTD.

Akhila [email protected] 21, 2013

Page 2: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

EXERCISE

• Go to the lecture notes page and Download Music_extended.accdb

• Note the added records to complete the English band Def Leppard and also the addition of the Canadian band Rush…

Page 3: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

EXERCISE

• Create a new query in design view• Add 3 tables: Bands, Countries, &

Members…

Page 4: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

SPECIFY AN EXACT MATCH CONDITION

• An exact match condition only shows records that match your criteria in the “Criteria” row of the query design grid

• The field values of the records for the field the criteria is specified in must match the condition exactly to be shown in the query results

Page 5: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

EXERCISE

• Add the following fields from these tables:• [Bands] BandName• [Member] FirstName, LastName• [Countries] Country

• Run the Query to see the unrestricted results of all the 16 records

Page 6: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

ADDING CRITERIA

• Return to Design View• In the criteria row for Country, type

England to have it show us a Dynaset with only band members from England

• Rerun the query• Change England to America and rerun• Try Portugal to see that nothing returns!

Page 7: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

USING COMPARISON OPERATORS… “AND” / “OR”

• When using multiple conditions for a query, you can use the logical operators to combine conditions– The “And” logical operator specifies that

both conditions must be met – The “Or” logical operator specifies that

one or the other of the conditions must be met

Page 8: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

USING “AND” AND “OR”

• When you enter two conditions in the same row of the query design grid, an “And” condition is created

• If you enter two conditions that are on separate rows, an “Or” condition is created

Page 9: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

ILLUSTRATION OF “AND” & “OR” LOGIC

Page 10: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CREATING “AND” CONDITIONIN THE DESIGN GRID…

Page 11: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

LET’S TRY AN “AND”• Clear any existing criteria• Add the field [Members] Living• Type England in the Country row and

create the AND condition by placing Yes in the Living row to return all living band members from England

• Run it• Go back and change Yes to No and

rerun

Page 12: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

LET’S DO AN “OR”

• Remove the old criteria• We will set the criteria to show all

members from Rush OR anyone from any band that is living at all from the list of members…

• First lets put “Rush” in the [Bands] Name field and run to see the results

Page 13: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CREATE THE “OR”

• We wish to not only see the members of Rush, but also in addition to them, anyone that might be alive in the members table.

• In Design view, type “Yes” in the OR row for [Members] Living

• You should now see a list of 10 additional records (13 total)

Page 14: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

SORTING RESULTS BY DATA TYPE…

Page 15: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

SORT EXERCISE

• Open the RockTrivia Query in Design View• In the Sort row for the BandName field,

have it sort the records in Ascending Order• Run the Query

Page 16: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

TWO-LEVEL SORT• Leave the existing sort in place and move

the last name field so that it is before the first name field in the Design view…

• Click once to select it… let go and then click and drag to move it over

• Add a secondary sort by having it do Last Name in Ascending order

• Run it. Last names are now show alphabetically inside the band names

Page 17: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

WE CAN ALSO USE “WILDCARDS”

Page 18: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

WILDCARD EXAMPLE

We want to see all members whose last names begin with “E”…

• Remove all existing sort options• In the last name field, type the letter E in

Criteria and run the query.• (No results show as no one has just the

letter E for a last name)• Make it read E* and rerun to get any other

characters…

Page 19: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

WILDCARDS CONTINUED

• Change the criteria to show people whose last names end in “N”• *N

• Run it and we should see 7 records.• Remove all criteria.

Page 20: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

USING FIELDS WITHOUT SHOWING

• It is possible to use a field in searching but not show that field in the query results

• Let’s say we want to see all band members whose bands begin and end in “d”, but not see the band name

• Remove the “Show” row checkmark for BandName

• Create the criteria and run !

Page 21: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

OPERATORS

Note: This technique is similar to what part of a query asks for in HW4 !

Page 22: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

DATE FIELD ENTRIES FOR BIRTHDAY• Note Born field in Members table is completed

• Close Members table

Page 23: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CRITERIA & OPERATORS EXERCISE

• Go to Design view for RockTrivia, add the Born field, and clear criteria, re-show BandName

• In the criteria row for born, add: >1/1/1960 • Note we get 3 records• Try making it: >=1/1/1950 AND <=1/1/1960• We get 9 Records• Change it to: <1/1/1960• We get 13 records

Page 24: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CALCULATIONS IN A QUERY…

• Mathematical calculation can be done as part of the query..

• We often do this for things that we do not need to store and take up space with

• Expressions can be entered into the query design grid as if they were regular fields

• Done on data type fields treated as numbers

Page 25: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CALCULATIONS IN A QUERY…

• To perform a calculation in a query, you must created a calculated field in query design.

• Make certain that you are following the rules of precedence !(Please Excuse My Dear Aunt Sally)

• Two options for entering expressions: – Enter the expression directly into the field text box– Enter the expression in the Expression Builder

Page 26: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CREATING A CALCULATED FIELD

• Remove the existing criteria • Click in the next available blank field

where the name would be, and then click the magic wand “Builder” icon above

• The expression builder shows up…

Page 27: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

THE EXPRESSION BUILDER…

Page 28: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

LET’S MAKE A QUERY TO TRY THIS…

• We want to figure out how many years they are alive. It would be inefficient to store this as we would have to update it all the time.

• Build or type the following:– Age: (Date()-[Members]![Born])/365

Note: If you see <<expr>> be sure to remove it ! ! !

• Run it !• Expand the width of the column if you see ####

Page 29: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CHANGING DECIMALS PRECISION

• Note: Similar to what is needed for to set decimal places on query results in homeworks!!

YOU MUST RUN THE QUERY ONCE FIRST !• In design view, right click Age field• Click Properties• Select Fixed for Format and 0 for Decimal Places• Re-run it to see it without the decimal places

Page 30: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

TOTAL QUERIES

• “Totals” in a query can group field values and also perform basic computational functions on them

• To make the Total row appear, you need to click the Sigma symbol “Totals” button in query design view !

Page 31: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

PREMISE

We are running an investment firm and have some consultants working for us who have clients for which they manage investments.

We want a query that will show us how many clients each consultant has and the total amount of investments each consultant is handling…

• Save locally and open Total_example.accdb

Page 32: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

STRUCTURE OF TABLES & RELATIONSHIP

This Database has not yet had a relationship created. We will do it in the query for this one. Sometimes under special circumstances you will create them here instead of in the relationships window. Generally it is done in the relationships window though…

Page 33: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

BUILDING THE TOTAL QUERY

• Create ribbon > Other group > Query Design

• Add both tables• Create relationship inside query by

dragging ConsultantID fields together

Page 34: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

BUILDING THE TOTAL QUERY

• Add the field [Consultants]LastName• Add the field [Clients]LastName• Add the field [Clients]Assets• Run it without Total options > back to design

view• Click the button on the Query Tools / Design

ribbon to insert a “Total:” row• Note that the default for each is “Group By”

Page 35: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

BUILDING THE TOTAL QUERY

• We will Group By the last name of the consultants to show each of them singularly

• We will Count the last names of the clients as they are tied to the consultants to show how many clients each has !

• We will Sum the assets of the clients to show how much all clients of each consultant have together

Page 36: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

SETTING UP THE TOTAL ROW

• Have [Consultants] LastName as Group By

• Have [Clients] LastName as Count• Have Assets as Sum

• Run the Query !

Page 37: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

ALIASING THE FIELD NAMES

• Sometimes for a Field, a Calculated field, or a Total field, we like to make the display name more specific in the query results

• To do this, simply change the field name in design view by adding a new name and an “:”

• An example for the clients LastName field:# of Clients: LastName

Page 38: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

ALIAS THE 3 FIELDS IN DESIGN VIEW

• Consultant: LastName• # Clients: LastName• Total Assets: Assets• Run the Query and note the field names in

the dynaset !

Page 39: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

DOING MULTIPLE THINGS TO 1 FIELD

• Sometimes you may need to do multiple total query-type actions to a single field.

• Download and open the file CSDemo.accdb from the site

Page 40: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

BACKGROUND FOR TOTAL QUERY

• Look at the contents of the Sales table We want to display our:• Number of sales• Total dollar amount of sales• Average dollar amount sale

Page 41: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

CREATING THE TOTAL QUERY

• Create a new query in design view• Add the Sales Table• Add the Amount field 3 Times ! ! !• Click button to show the Total row• Set them to Count, Sum, and Average

• Run it !• Close Access

Page 42: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

IMPORTING XML DATA TO TABLES

• Download and save Bands.xml file on the desktop

• External Data Ribbon > Import group > XML File

• Browse for the xml file• Select Open & OK

Page 43: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

IMPORTING XML FILES

• Verify Structure and Data is checked• This brings in the table & its records• Later in the course we will use other options

• Click OK and Close• Open the table in the database !

Page 44: Day 18: MICROSOFT  ACCESS –  CHAPTER  3 CONTD.

Questions ?