Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro, to your M: drive. To do the second part of the prelab, you will need to have available a database from that folder. Creating a new Database with a Table: 1. Launch Microsoft Access: Windows Start Button > All Programs > Microsoft Office 2013> Access 2013. You should get a screen like below: select Blank desktop database (if not already selected) In the current window, under File Name, rename your database to MyPhoneBook and press on the folder button next to the name to save your database somewhere under your CS130 folder, (preferably in this PreLab folder.) Finally, press on the Create button 2. The first screen capture below shows what Access should display after creating the database. This screen allows you to create a new table in the database by inserting data directly which is not very convenient. This is called the Datasheet View. On the left-side, press on the View button and select the Design View instead. Access will ask you to rename your table; name your table Phonebook and press OK.
3. Once you do that, you ll end up with the screen above. Under column Field Name, you should include the name of each of your table columns with the corresponding data type specified under column Data Type. The properties for each of your columns can be modified by selecting the desired column under Field Name and changing the properties at the bottom of the page under Field Properties. 4. Every table needs to have a key field which has a unique value for each record. Access inserts a default field named ID as that key field when you create a table. For your database, change the name of the key field to ContactID and leave the data type set to AutoNumber, which will give each record a unique identifying number. 5. Now add the fields (or columns) listed below, along with the desired properties for each, to your Phonebook Access table. ContactName of type Short Text. In the properties window, change the Required property to Yes. PhoneNumber of type Short Text. In the properties window, change the Required property to Yes. Now put your mouse cursor inside the Input Mask property, press on the button at the end of Input Mask property (access will ask you to save the table so select Yes) and select the Phone Number mask. Change the Placeholder character to the pound symbol, #, and then the mask should look like (###) ###-####, where the # is a placeholder for integer numbers. Check the option box to store the data with the symbols in the mask and then click Finish. Address of type Short Text. City of type Short Text. In the properties window, change the Default Value property to Saint Cloud. State of type Short Text. In the properties window, change the Default Value property to MN. Zip of type Short Text. Follow the steps used in the PhoneNumber field but now use the Zip Code mask instead. Email of type Short Text. AnnualIncome of type Number. In the properties window, change the Field Size property to Double and the Format property to currency. BirthDate of type Date/Time. For this field, we want to limit phone numbers to people older than 16 years old; in the properties window, change the Validation Rule property to <#1/1/1998# and the Validation Text to Too young to have a phone number!
6. Save the database and press on the View button (on the left-side) to switch back to the Datasheet View. Input at least 5 rows into your table and observe how Access enforces the properties you set for your fields. Make sure you try sample records that are not consistent with the properties in your table definition. 7. When done, save your work and close Access. Working with an Existing Database 1. Inside this PreLab folder, you ll find an Access database called CleanGargoyles.accdb. Double-click on it. This is a sample database that contains a number of tables (Tables are listed under All Access Objects on the left). In addition, it contains many sample queries (Queries are also listed under All Access Objects on the left). 2. To create a new Select query, press on the Create tab at the very top of your Access window and press on the Query Design button in the Queries area. Next, select the desired table(s) and press Add. When done adding table, press Close. The following screen capture shows you what Access displays if you only select the Employee table: 3. In order to retrieve the desired data from table Employee, we need to specify our criteria in the query window at the bottom of the screen. Here are some guidelines: Field: Press on the drop-down menu to select the table fields that you want to use in your query. Each included field must appear under a separate column. Table: Specify which tables the fields will come from. Access should complete this one for you. Sort: Specify whether output should be sorted on this field (if so, choices are Ascending and Descending) Show: Check all the fields that should be included in the output of the query. Criteria: Specify your selection conditions for each field separately. Or: Use this to specify an OR of conditions on different fields. Here is a first example: The following query displays the LastName, FirstName and Department for all employees who work in the Management or Sales departments. Output is sorted first by FirstName in ascending order and then on department in descending order.
And here is a second example: The following query displays the LastName, FirstName, Salary, Department and FullTime status for all employees who either work in the Management or are fulltime employees. Output is sorted by LastName in ascending order.
4. To run a query, press on the Run button next to the View button in the upper left corner of your screen. Access will then display the result of your query in table format. You can go back to the query Design View by selecting this option from the View button. 5. Some notes: Derived fields: In the field row, we are not limited to only fields from our tables. We can easily derive new ones using arithmetic expressions or String concatenations (VB syntax style). For example, to display the full name of an employee formatted as Last name, first name we can write the following expression into one of the field columns: Full Name: [LastName] & ", " & [FirstName] We need to specify the name of the derived field before the colon and then enclose any of the used table fields between square brackets. As in VB, & can be used to concatenate strings. Parameter queries: For queries that require input from the user we can specify a message inside square brackets in the criteria row of the desired field. For example, [Enter a department name] in the criteria for field Department will display an input box for the user with the prompt Enter a department name and will then use the value input by the user as a search criteria to find employees working in a matching department. Approximate string matching: Access has two special characters: the? (question mark) character which stands for any single characters and the * (asterisk) character which stands for any number of characters (i.e. 0 or more characters). Those special characters can be used to search for strings/text fields that approximately match your search criteria. They should appear inside double quotes preceded by the LIKE operator. For example: - To find employees whose first names start with the letter A, we can write the following criteria under field FirstName: LIKE A* - To find employees whose first name contain the letter A as a second letter, we can write the following instead: like?a* Date functions: - The Year(datefield) function extracts the year portion of a given date field - The Month(datefield) function extracts the month portion (a value between 1 and 12) of a given date field - The Day(datefield) function extracts the day portion (a value between 1 and 31) of a given date field - The Now() function can be used to get the current date and time. This is very useful in search criteria that are based on today s date such Find students who are older than 20 years as of today. In order to find rows matching this criteria, we need to utilize Access s current date and time via the Now() function (otherwise we will have to change the condition every day we run the query). To be older than 20 years as of today, the student s birth date field must satisfy the following criteria: < Now() 20*365 (where 20 is the number of years and 365 is number of days per year Access uses days when applying arithmetic operators to dates) 6. In the remainder of this pre-lab, you will create, test and save the following queries: Use table Employees to retrieve the last name and first name for employees who work for either the Management or Sales departments sorted by department. Save your query as Access_PreLab9_Q1. Use table Employees to retrieve the last names for employees who are either fulltime or who earn more than $20000. Save your query as Access_PreLab9_Q2. Use table Employees to retrieve the last and first names for employees who work for departments with last names ending with the letter g. Save your query as Access_PreLab9_Q3. PS: Use Like operator with an * Use table Employees to retrieve names of employees working for a department whose name is input by the user. Save your query as Access_PreLab9_Q4. PS: This is a parameter query
Use table Purchase Orders to retrieve the purchase order number, item number and order date for orders made more than 2 years ago. Save your query as Access_PreLab9_Q5. PS: Use Now() function