ACCESS (CHAPTER 2-3 continued) Naman Kohli October 16,
UPCOMING DEADLINES 21 st October – MyITLab Lesson C 1 st November – Homework 4 (published) 4 th November – MyITLab Lesson D 8 th November – Homework 5 2
MID SEMESTER GRADES Those not doing well. Can be seen on STAR. Out of 280 – Exam, Homework # 1,2,3 Still Bonus points left. Focus on MyITLab and Exam 2. 3
PREVIOUS CLASS 4
TABLE RELATIONAL STRUCTURE 5 MembersBands Countries ∞ ∞ 1 1
USING “LOOKUP” TYPE FIELDS They allow for clicking a drop down arrow when entering field values while in datasheet view to select predefined items as opposed to having to type them. Eg:‘Year’ is a Lookup Type Field Two types of lookup fields: 1.Lookup references uses field values in another table 2.Lookup references items you manually enter in advance in the same table We will now do both types… 6
LOOKUP FROM OTHER TABLE EXAMPLE Create the third table for our database called Countries Make these fields: IDAutoNumber(Primary Key) Country Text Flip to Datasheet view and save the table … 7
LOOKUP FROM OTHER TABLE EXAMPLE Create the following Records: 1,England 2,America Close the table … 8
LOOKUP FROM OTHER TABLE EXAMPLE Return to the “Members” table in Design View Add a 5 th field called Origin of type Lookup Wizard Use “I want the lookup column to look up values in a table or query” Select “Table: Countries” … 9
LOOKUP FROM OTHER TABLE EXAMPLE Add the Country field to Selected fields Next… Next… Finish Save if prompted Return to Datasheet View in “Members” Click in the Origin file to assign these: Assign “England” to The Who members Assign “America” to Van Halen members. 10
SAME TABLE LOOKUP EXAMPLE (LIKE STEP 5H) In the Members table of our example, return to Design View Create a filed called Instrument of type Look- up Wizard… Select I will type the values I want. Enter the following: Vocals, Drums, Guitar, Bass Save, Return to Datasheet view to enter them! 11
SAME TABLE LOOKUP EXAMPLE (LIKE STEP 5H) 12 Members BandNumFirstNameLastNameOriginInstrument 1JohnEntwhistleEnglandBass 1PeteTownsendEnglandGuitar 1RogerDaltryEnglandVocals 1KiethMoonEnglandDrums 2David LeeRothAmericaVocals 2EdwardVan HalenAmericaGuitar 2AlexVan HalenAmericaDrums 2MichaelAnthonyAmericaBass
“YES/NO” FIELD TYPE EXAMPLE Add a field called “Living” Make it of Type Yes/No Save the table and mark all records except Keith Moon and John Entwhistle as Living members * We will come back to this 13
DATE FIELD EXAMPLE Add a field called Born of Date/Time datatype 14 Members Born 10/9/1944 8/23/1946 3/1/1944 5/19/ /10/1954 1/26/1955 5/8/1953 6/20/1954
CREATE A RELATIONSHIP EXAMPLE Close the tables Open Relationships Note that lookup wizard created one already! Right click, Show Table, add Bands. Drag [Bands/BandNum] to [Members/BandNum] Enforce referential, Create, and save. … 15
QUERY EXAMPLE We want to use the relationships we created between the tables to show the names of the artists, their band names, and their countries of origin despite only having a band number in the Member table and a lookup for country. We can use the relationship to seamlessly query between fields in the three tables and give the desired results… 16
QUERY EXAMPLE Create query in design view Create Ribbon > Query Design button Add all tables (note the relationships showing) From Bands table double click to add: BandName From Members table add: FirstName, LastName, Instrument, Living From Countries table add: Country Run the Query ! Close and Save Query as RockTrivia 17
18 CREATING “AND” CONDITION IN THE DESIGN GRID…
19 LET’S TRY AN “AND” Clear any existing criteria 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 …
20 LET’S DO AN “OR” Remove the old criteria We will set the criteria to show all members from The Who OR anyone from any band that is living at all from the list of members… First lets put “The Who” in the [Bands] Name field and run to see the results …
21 CREATE THE “OR” We wish to not only see the members of The Who, 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 4 additional records (8 total)
22 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 …
23 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
24 WE CAN ALSO USE “WILDCARDS”
25 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…
26 WILDCARDS CONTINUED Change the criteria to show people whose last names end in “N” *N Run it and we should see 3 records. Remove all criteria.
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 ! 27
28 CALCULATIONS IN A QUERY… Queries can be built to perform a calculation 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 Numeric data type fields
29 CALCULATIONS IN A QUERY… To perform a calculation in a query, you must add a calculated field to the query design. Make certain that you are following the rules of precedence ! Two options for entering expressions: –Enter the expression directly into the field text box –Enter the expression in the Expression Builder
30 CREATING A CALCULATED FIELD Remove the existing criteria In design view, click in the next available field, where the name would be, and then click the magic wand “Builder” icon above. The expression builder shows up…
31 THE EXPRESSION BUILDER…
32 LET’S MAKE A QUERY TO TRY THIS… We want to figure out how many years they have/would been alive. It would be inefficient to store this and we would have to update it all the time too ! Build or type the following: Age: (Date()-[Members]![Born])/365 Note: If you see > be sure to remove it ! ! ! Run it ! Expand the width of the column if you see #### …
33 CHANGING DECIMALS PRECISION 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