IT Training Microsoft Access 2010 Jane Barrett, IT Training & Engagement Team Information System Services Version 3.0
Scope Learning outcomes Learn how to navigate around Access. Learn how to design and use a variety of database mechanisms such as tables, queries, forms and reports. Understand the relationship between tables. Table of Contents Scope... 2 Table of Contents... 2 Introduction... 3 General information... 3 Databases... 3 Tables and records... 3 Fields... 3 Data types... 3 Key fields (or Primary Key fields)... 5 Constructing a database... 5 Creating a database in Access... 7 The starting window... 8 Tables... 8 Creating a table (Table Design View)... 8 Designing a table... 9 Data types and field properties... 9 Caution... 9 Entering data in a table... 10 Editing Data... 10 Importing data from elsewhere... 10 Importing from Excel... 11 Importing from a text file... 11 Copying, renaming or deleting tables... 11 Change appearance of tables... 11 Queries... 14 Wild Cards... 14 AND and OR... 15 Queries involving more than one table... 15 Other Query types... 15 Forms... 20 Creating Simple Forms... 20 Change the Layout of a Form... 20 Reports... 23 Contacts... 25 For queries or help... 25 Useful resources... 25 Training videos... 25 Booking other Access training courses... 25 Online training... 25 Your feedback... 25 Help us improve your training... 25
Introduction This course shows you how to create a database and how to design and use a variety of mechanisms such as tables, queries (including editing queries), forms (including the use of command buttons) and reports. It will also explain about using relationships between tables. To supplement this introductory course, we recommend the ECDL (European Computer Driving Licence). The ECDL consists of seven modules (including a database module) that will demonstrate your competence in a range of computer skills. For more information visit: www.essex.ac.uk/it/training/ecdl Note: These course materials are design to accompany face-to-face training and some exercises may be difficult to complete out of the IT Training Lab. General information Databases A database is a collection of related data from which information is drawn. The total data can usually subdivided into areas of similar information. For example a database containing information about a school would contain data about students, teachers, courses etc. There will be many instances of each entity eg many students but the data kept about each member would be similar, There will also be relationships between the different entities eg Student A is taught Course B by Teacher C. Tables and records The information about each entity or relation is stored in a separate table. The information about a single instance of an entity (or relation) is stored in a record. A record is all the relevant information about the entity. Therefore, a table is made up of many records. Fields The record will consist of a number of facts, each separate fact eg FirstName, LastName, department is stored as a field. Thus a number of fields form a record; a number of records (all having identical fields) form a table and a collection of tables form a data base. Data types Data in fields can be of various types: Setting Text Memo Type of data (Default) Text or combinations of text and numbers, as well as numbers that don't require calculations, such as phone numbers can be any number from 1-255 characters. You should generally use the smallest possible FieldSize setting because smaller data sizes can be processed faster and require less memory. Lengthy text or combinations of text and numbers up to 63,999 characters. 3
Number Numeric data used in mathematical calculations, whole or decimal places. If the DataType proper is set to Number, the FieldSize property settings and their values are related in the following way: Setting Description Byte Stores numbers from 0 to 255 (no fractions). It occupies 1 byte. Integer Stores numbers from -32,768 to 32,767 (no fractions). It occupies 2 bytes. Long Integer Stores numbers from -2,147,483,648 to 2,147,483,647 (no fractions). It occupies 4 bytes. The data type AutoNumber is a Long Integer. Single Stores numbers with 7 digits of precision. Double Stores numbers with 15 digits of precision. The default default value for numbers is 0. Date/Time Currency AutoNumber Yes/No OLE Object Hyperlink Attachment Lookup Wizard Note: unless there are good reasons otherwise, choose Long Integer for whole numbers and Double for numbers with a decimal part. Date and time values for the years 100 through 9999. In the field property section choose Format to select which type of date and/or time you require. Whilst all dates are stored in the same way, probably the safest format to use for dates is Medium Date eg 23-Apr- 10 so there is no confusion between the American and English date formats. Currency values and numeric data used in mathematical calculations involving data with one to four decimal places. Accurate to 15 digits on the left side of the decimal separator and to 4 digits on the right side. Currency fields use a faster fixed-point calculation that avoids rounding errors. There are a number of possible display formats include one with the sign. The default value is 0. A unique sequential (incremented by 1) number or random number assigned by Microsoft Access whenever a new record is added to a table, used where a guaranteed unique value is required. This type is not editable. AutoNumbers are often used for Key fields. Yes and No values and fields that contain only one of two values (Yes/No, True/False, or On/Off). The default is No or equivalent. OLE data types includes pictures/graphics, documents spreadsheets etc. Text or combinations of text and numbers stored as text and used as a hyperlink address. A hyperlink address can have up to 4 parts for example http://www.essex.ac.uk/it/training/dates.aspx Any supported type of file. You can attach images, spreadsheet files, documents, charts, and other types of supported files to the records in your database, much like you attach files to e-mail messages. You can also view and edit attached files, depending on how the database designer sets up the Attachment field. Attachment fields provide greater flexibility than OLE Object fields, and they use storage space more efficiently because they don't create a bitmap image of the original file. Creates a field that allows you to choose a value from another table or from a list of values by using a list box or combo box. Clicking this option starts the Lookup Wizard, which creates a lookup field After you complete the wizard, Microsoft Access sets the data type based on the values selected in the wizard. Sometimes the value of a field is not available in which case it is entered as NULL. Note: that this is different from 0 for numbers and the empty string for text. Table 1 4
Key fields (or Primary Key fields) Some records will have the same information in certain fields, eg several staff will be in the same department, but usually there is one field, or combination of fields, which is unique. This field can be set as the key field(s), thus a query searching for a specific value of this field yields only one record. Where no obvious field can be guaranteed to be unique (eg even the combination of firstname and lastname, whilst probably unique in most cases, cannot be guaranteed unique as it is obviously possible to have, for example, two John Smiths), or where the field values are too cumbersome to use, then an artificial field such as staff-id or course-no can be introduced in order to provide a key field. The value entered in this field can be a simple number eg 3527 or some more meaningful (to the human user) text eg BL234-AU. Once a field has been defined as a key field then it follows that this field can never be left blank or NULL as is possible for other fields. Access has a special field type called AutoNumber which can be used to automatically enter such codes. Constructing a database When deciding on the structure of the tables that make up the database certain points have to be considered: Redundancy Inconsistency ie having the same data entered more than once which is obviously inefficient in terms of time and space and can lead to ie having conflicting information is in the table when one or more of the entries vary from the others. For example: Name Age Subject Grade John Smith 16 French C John Smith 16 English B Bill Brown 17 Maths A John Smith 15 Maths B Each time John Smith gains a grade and entry is made which includes his age. So redundancy occurs when the fact that he is 16 years old has been stored twice. Inconsistency occurs when on the third entry when he appears to be 15. This problem can be solved by creating two tables, one about the people and one about the grades: id name age s_id subject grade 23 John Smith 16 23 French C 39 Bill Brown 17 23 English B 36 English D 39 Maths A 23 Maths B To do this it is necessary to create an extra piece of data namely the id number so that records in the two tables can be related to each other. This change avoids duplication of data and creates a single point of entry for data. However it brings with it a possible problem that of referential integrity. 5
Referential integrity means that when, in one table, you have a reference to a record in another table then this record must exist. For example who is student 36 who has taken English? When a field in one table refers to a key field in another it is said to be a Foreign Key. Access can check that no entries are made without an existing reference. Decomposable fields fields which could be split into two or more fields are said to be decomposable. For example the name field in the example above could be split in two, first_name and last_name. It is usually better to break down your data at design level as it is easier to join data in two fields than it is to split data up. When deciding what tables to create, first identify the entities that exist. These are those objects that exist in their own right. For example students exist independently of the courses. Courses exist even before students enrol for them. So the entities in this simple example might be students, courses and teachers. The information about students might be name, address, date of birth, gender etc; the information about courses might be name, location, day etc; The information about teachers might be name, address, etc. Note: that no information about what courses a student takes is stored in the student table. This type of information is referred to as a relation and a separate table is created linking students with courses. Similarly another table would link teachers and courses. Field names Although Access allows you to give fields names of up to 64 characters in length including spaces it is generally better to have shorter, meaningful names and to avoid spaces. Some field names such as first name which naturally have a space in them can be entered as first_name or firstname. Probably the best solution is to use the method of capitalisation ie FirstName where each word in the title is capitalised and then joined without spaces. Access database objects An Access database consists of a number of different objects: Tables - are the database objects which contain data; Queries - are views of this data and ways of retrieving information from the data; Forms - are a way of controlling the entry and editing of data; Reports - are ways of arranging the information for printing; Macros - are ways of combining several database actions together; Modules - are where you can create Basic code to achieve any effect you might require. This document will deal with the first four of these. 6
Creating a database in Access When access starts a window like the one shown in Figure 1, you can: Click the Blank Database option; or Select one that you have already created Open Recent Database: In the options window on the right you can: Figure 1 Enter a name for the database in the default location; or Click on the Folder Option to see the standard file window; Choose your location; and Give the database a name; then Click on Create. To open a database that is not in the recent databases list click on More. To close a database click on the Office button and chose Close or Exit. 7
The starting window When Access opens for the first time, it presents you with one empty table containing an id field. The next section shows you how to design you own table. Tables When you create a database in Access 2007 it inserts a blank table with one field (id). Creating a table (Table Design View) Figure 2 Select Create, click on Table (which will give you a new table in data view like Figure 3); or Select Table Design which gives you a blank table with no fields at all (this view gives the most control and so the rest of this section will refer to this). Note: to reach this view for an existing table right click on the table name and choose Design View. Figure 3 Note: The id field: by default when you create a table in data view, Access adds a key field called id. Not all tables require a key field or you may wish to name it something else. Unless you know that you need this field you should delete it. 8
Designing a table Enter the field name (names can be up to 64 characters long and can include spaces, it is best to keep names as short as possible whilst still remaining meaningful. Press Tab to go into the DataType column (the default type of data is text and this is automatically set for you). Clicking on the drop down arrow button which appears in this column will give you a list of all the other types of data you can use. These are listed in the table below. Figure 4 Note: here, as almost everywhere in Access, pressing F1 will bring up context sensitive help. You can, if you wish, type in a description of the field in the third column. This is for reference purposes only. Data types and field properties At the bottom of the window is an area labelled Field Properties. In these boxes you can set the field size, validation rules, display formats and default values. See Table 1 (pages 3 and 4) for a detailed explanation of Data Types and field properties. Caution Whilst the design of a table can be changed at any time if you convert a large FieldSize setting to a smaller one in a table that already contains data, you might lose data. For example, if you change the FieldSize setting for a text field from 255 to 50, data over 50 characters is truncated. If the data in a Number field doesn't fit in a new setting range, fractional numbers may be rounded or you might get a null value. For example, if you change from single to integer, fractional values are rounded to the nearest whole number and values greater than 32,767 or less than -32,768 result in Null fields. You can't undo changes to a table's design after saving it in Design View. When you have entered all the fields, click on the Control Box button and chose Close. You will be asked to name and save your table. Note: you can change the structure later. 9
Entering data in a table Select the table and click on Open. You will be presented with a spreadsheet representation of your table with one blank row. This is called Datasheet View. Where the field has a DataType of AutoNumber this will display a star in the left-hand column to indicate that this field cannot be edited in any way. Fields that can be edited will display a pencil in the left-hand column (when you place the cursor in them). Figure 5 Type in the data (shown in Figure 6), press Enter or Tab to go from field to field. When you have finished, click on the Close box. Figure 6 Editing Data With the table open you can click on any value and edit it in the normal way. Once you move from the record the values are changed in the database. Forms (following), offer another way of entering and editing data. Importing data from elsewhere If you have data already available in some form it may well be possible to bring in the data directly: Choose External Data Then choose from the Import tab. Then choose the type of source file eg Excel, text etc. Figure 7 10
Importing from Excel When you select Excel as the source the following window appears: Choose the source file; and Then say whether you are to create a new table; or Append data to an existing table. The import wizard then starts. Figure 8 As an Excel workbook can contain several sheets you are next asked to identify the required sheet. The next step is to indicate whether or not the first line contains field names. If it does then the data will be matched correctly or new fields created, otherwise data will be inserted in column order. If you are creating a new table you will be given the chance to choice your own data types rather than accepting the defaults. You will also be given a chance to choose the key field. Importing from a text file This is similar to importing from Excel but there is a choice of text file types, delimited and fixed width. It is assumed that in all text files one record is written per line. The difference between the two types is how the fields can be identified. Delimited text files are written with the values of the fields separated by a constant character for example a comma, space or a tab. In fixed width text files each field occupies the same number of characters in each record, with spaces used to pad out the field values. Copying, renaming or deleting tables Right click on the table name and chose the appropriate action. Change appearance of tables You can change the size of the cells in a table, or the font used to display the data by clicking on Home and then choosing the appropriate value form the Font section. 11
Task 1: Tables a) Create a table Create a table to hold information about students (it will require fields for first name, last name, date of birth and gender). 1. Give the table the name Students. Delete the first row called ID. 2. Enter the field names as shown below and choose the data types from the drop down list: Field Name Data Type Description FirstName LastName dob Gender Text Text Date/Time Text 3. Selecting each field in turn, move to the bottom pane and alter the field size (format property of the field): 4. Give FirstName and LastName a field size of 20; 5. Set the format of dob to MediumDate; and 6. Give gender a field size of 8; then 7. Close down the table design window, and save changes. b) Entering records 1. Double Click on the Student Table to open the table in Datasheet View, then create four records by entering values in the cells. Note: pressing Tab or Enter moves you from one cell to the next creating a new record when appropriate. 2. Close down the table and notice what happens (or more specifically what doesn't happen). c) Editing and deleting records 1. Open the table up again and alter some of the values, then select one of the records by clicking the mouse in the left hand margin of the table (the grey area) and then press the Delete key (notice you get a warning before the record is deleted). d) Create another table (see a) Create a table) 1. Create a table called Courses, (again delete the ID row), with the following fields: CourseName (text width 20); Day (text width 10); Tutor (text width 15) and Room (text width 10). Close and save the table. e) Import data from an Excel file 1. Click on the External Data Tab, then Click on the Excel Icon and Browse to select data.xls from the correct folder and press Open. 2. Check the option Append making sure the sheet Students is selected and press OK. 3. Show Worksheets should be selected, Click Next. 4. Check the box which says First row contains column headings and press Next. 5. Check Import to Table shows Students then press Finish. 6. Don t save import steps, click Close. 7. Open the Students table to see the data you added, then close the table. 12
f) Import Data from a Text File 1. Repeat Task e) Import data from an Excel file with the file Courses.txt. Select the Text File option instead of the Excel Icon. 2. Append a copy of the Courses.txt file to the Courses table and Click OK. 3. Check that Delimited has been selected and press Next. 4. Check that the delimiting character is Space and the click in the box which reads First row contains field names and press Next. 5. Check Import to Table shows Courses then press Finish. g) Alter the design of a table 1. Select the Students table and click on View, Design. 2. Add a new field called ID with data type AutoNumber. 3. Close the design and Save the table, then open it in Data View. Notice the position of the ID field, and the fact that it has been filled with values. 4. Change the first ID from 1 to 100. What do you notice? 5. In Design View select the ID field and drag it to the top of the list of fields, View Data again. 6. Repeat with the Courses Table adding an ID field and viewing the result. h) Creating a table that says which student does what course 1. Create a new table called Attends, with two fields; one called Student and the other Course, both having the data type number, then Close and Save this table. 2. Open the Students table and make a note of the ID of one of the students. 3. Open the Courses table and find the ID of the course you wish the student to take, enter the Student ID and Course ID numbers in the appropriate columns of the Attends table. i) Get more data for the attends table 1. Repeat Task e) Import data from an Excel file, this time selecting the sheet Attends and append the data to the Attends table. j) Setting Key fields 1. Certain fields are special, ie ID in Students and ID in Courses. The values in these fields are used in the Attend table to provide the link. 2. Select Students and click on Design. Select the ID field and click on the key symbol from the toolbar, then Close and Save. 3. Repeat for Courses. End task 13
Queries A query is a request for information from the stored data. Once it has been defined it can be used as if it were a table itself, and be formatted, printed, and used in forms or reports. Click on the Create tab; then on Query Design (The query design window appears with the Show Table window on top). Select the table that you wish to use in your query, and click on the Add button. Repeat for all the tables that you wish to use in the query and then Close the Show Table window. The query design window has columns in which you enter the fieldnames you wish to see. Figure 9 Click in the field box to get a drop down list of field names to choose from, then select the one you want. Double click on the field name in the box in the top half of the window to copy the field name into the next available column. The third row enables you to ask for the results to be sorted: Click in this area to get a list of three choices, Not Sorted (the default), Ascending or Descending. The fourth row contains a check box and deselecting the check box will mean that the field is used in the query but the result is not shown. The rest of the rows are used to enter the search criteria. Type the values, or range of values required and use several rows for OR searches; Criteria on the same row are assumed to be AND searches. Wild Cards In the criteria field it is possible to search for more than one value by using wild cards, and instead of entering =value you enter like value where value is a quoted string containing some combination of text and wild card. The wild cards are: * which stands for any or no characters;? which stands for one character; and It is also possible to enter comparison operators such as > or <, or the BETWEEN?? AND?? operator. 14
The fields returned by the query can be simple fields from the table or they can be expressions. For example [FirstName]& &[ LastName] creates a long string combining the two values with a space between them. AND and OR When creating criteria for queries it is important to understand how AND and OR work. For example given the following data: Record FirstName LastName 1 Jim Smith 2 John Smith 3 Jim Jones 4 John Jones 5 Jim Green The query FirstName = Jim AND LastName = Jones gives record 3. The query FirstName = Jim OR LastName = Jones gives records 1, 3, 4 and 5. All the Smiths and Greens, which is records 1, 2 and 5 is obtained by the query LastName = Smith OR LastName = Green. The Query LastName = Smith AND LastName = Green yields nothing as no one has a LastName which is both Smith and Green. Queries involving more than one table When a query involves more than one table you add all the tables required, preferably in the order that makes linking easiest. To link a field in one table to the related field in another select the field and drag it onto the related field. A line should appear to illustrate the linking. Create the query in the normal way with the increased number of fields. The second line will show the table related to the field. Other Query types Figure 10 The default type of query is the Select query which returns records or parts of records from tables but does not alter or affect the underlying data. There are some other types of query which when run will affect the data. Figure 11 15
Task 2: Queries a) Create a simple query Create simple query on one table with restricted fields, eg show just the FirstName and LastName of the students. 1. To start a new query, select the Create Tab and then click on the Query Design Icon. 2. From the table list select Students and then Add and Close the table list. 3. In the first column of the query, in the field row enter FirstName. Note: this can be done by double clicking on FirstName in the list of fields, or by dragging it from this list. 4. In the second column enter LastName. 5. Click on the Run button on the tool bar (this looks like an exclamation mark!) to see the resulting data for this query. Add criteria to select certain records, eg show females only: b) Add criteria 1. Click on the View button to get back to design. In the third column add Gender; and in the fifth row called criteria type in the word Female. 2. Click Run to see data. c) Edit data in a query: 1. Check the data, one name appears to have been given the wrong gender, change Female to Male. 2. Click on the View button twice and see if this record still appears. Sort data according to one field, eg sort alphabetically by last name: d) Sort 1. Click View to return to Design. In the LastName column in the third row labeled sort, click on the Expand button and choose Ascending. 2. Click on View to see results. e) Have non visible fields in query 1. Return to Design View and in the gender column, in the fourth row (show) click in the box with the tick in, this removes the tick. 2. Change to Data View to see effect. Note: gender is still being used by the query to select records, but its value is not being shown. 3. Close query and save it as Query1. f) Create query with comparison criteria eg > < between 1. Create a new query. From the table list select Students and then Close the table list. 2. Get the new query to show the columns FirstName, LastName, dob and Gender and we will sort on LastName by adding a criteria. 3. In the criteria row for LastName enter > k and then View data. 4. Change the criteria to <h and then View data. 5. Change the criteria to between h and k and then View data. 6. Close query and save it as Query2 16
g) Create query with multiple criteria (AND) 1. In the previous query clear the criteria for LastName. 2. In the criteria row enter Male in the gender column and > 1/1/70 in the dob column. 3. View data, this should be all males born after 1st Jan 1970. h) Create query with multiple criteria (OR) and wildcards 1. Clear all criteria, or create a new similar query. 2. In the criteria row for LastName type m* and then View data. 3. Return to Design view and notice how the criteria have been changed by access. 4. In the row below like m* type s* and then View data. What has been returned? 5. Move the s* from the LastName column to the FirstName column, leaving it in the second criteria row and then View data. What has been returned? 6. Move s* to the first criteria row of FirstName and then View data. What has been returned? End task Update Queries This type of query enables you to make changes to a group of selected records. First create a Select query that chooses the records that you wish to update, then change the query to an update query and enter in the update to line the new values that you wish to appear in the table. Then run the query by clicking on the Run button. Delete Queries This is similar to an update query but enables you to delete records. Make Table Queries This type of query actually writes the results into a table (Normal queries return data from other tables). This is useful for taking a snapshot of data from a constantly changing table. Append (update) Queries Like a Make Table Query, this writes to records in an existing table. Statistical Queries (Totals) This type of query is used to find totals for groups of data. In the example the results have been grouped by CourseName and for each course a count of the number of students is given. Query Parameters If you have a series of queries which are identical except for one value you can reduce them to one query and use a parameter to replace this value. The actual value is inserted at runtime. If you had a query where the criteria in the subject field was Maths and another where the criteria was English, then in the criteria field enter [subject] (the square brackets are necessary) and the word subject must not be an existing field name. When you run the query you will be prompted for the value of subject and can enter Maths, English or whatever subject you require. 17
For example: A criteria of [Please enter the course code] has been added in the Design View. This will result in the Enter Parameter Value window appearing to request a course code. Figure 11 Task 3: Update Queries a) Create action query (update) 1. Create a query to return all females. 2. Change the query to an Update query using the icon in the ribbon ****. 3. In the Update To row for the Gender column type F 4. Run the query and click Yes to accept changes and click on the Select icon to return from Update mode. 5. Open the Students table from the table collection and see what effect this query has had. 6. Repeat changing Female to F and Male to M. 7. ***These tasks will need explanation. b) Create statistical query, counting records 1. Create a query from the Students table with gender in the first column and LastName in the second column. 2. Click on the Totals button from the toolbar. 3. In the LastName column, change group by to count (use drop down list) and then Run and view the data. c) Create a query with results other than table entries. 1. Create a new query based on the Students table. In the first column type [FirstName] & " " & [LastName] (Note: there is a space between the two quotes). 2. Run the query. What has been returned? What title has been given to this column of data? 3. Return to Design View. 4. Change expr1 to name (note don't delete the colon) and then review the data. 5. In the second column type DateDiff("yyyy",[dob],Now()) and then view the data. Explanation: DateDiff is a function with three parameters that returns the difference between two dates. The first parameter, the unit "yyyy", stands for years, the other two parameters are the two dates. [dob] refers to the date of birth field in the student table. Now() is a function which returns the current time and date. DateDiff returns the number for years between the date of birth of the student and now ie their age. 18
6. Alter the column heading appropriately. 7. Sort the results alphabetically by Name, see Task d) (Sort). 8. View data. Have they been sorted in the conventional way ie alphabetically by LastName? 9. Turn off sorting in the first column, add a third column with LastName, sort this column by ascending and uncheck the show box, see Task e) Have non visible fields in query. What this means is your data will be sorted by LastName, but you won t see LastName in retrieved results. 10. Click View data or Run. Has the data sorted correctly? Hint: look at the Smiths. 11. Repeat previous step adding a fourth sorted column with FirstName. d) Create a query linking two (or more) tables 1. Open a new query, select the Student table and click Add then repeat for the Attends table and the Courses table, then click Close. 2. Click on the ID field of the Student table and drag it onto the Student field of the Attends table. A line should appear joining these two fields. This indicates that they are related. 3. Repeat for the Course field in the Attends table and the ID field in the Courses table. 4. In the first field of the query put FirstName, in the second field put LastName (both from the Students table) and in the third put CourseName from the Courses table then View the data. 5. Add fields as appropriate, then sort data by CourseName, and alphabetically within each course. e) How many people take each course 1. Start a new query, add the tables Course and Attends and link as before. 2. Show CourseName from Courses and Course from Attends. 3. Click on Totals button and change the value in the course column to count, then View the data. f) Creating Relationships Rather than having to tell Access each time you write a query about how the tables are related, it is possible to store this information permanently. 1. Choose Database Tools Tab from the menu line and then Relationships. 2. Select the tables Courses, Attends and Students. 3. As before drag a line from ID in Courses to Course in Attends. A window appears. 4. Click on Force referential integrity and then on Create. 5. Repeat for ID from Students and Student in Attends. 6. Close and Save. 7. In the Tables group on the sidebar, Open the Attends table and click on * (new record button). 8. Try entering student 500 on course 20. What happens? Why? g) Write queries to return the following records 1. Create a query to return all people under 21. 2. Create a query to return all males over 30. 3. Create a query to return all people whose LastName begins with a vowel. 4. Create a query to return all males born in 1978. End task 19
Forms Creating Simple Forms Forms can be used to search for, edit or delete records or add new records. Presenting the data in column form, one record at a time can be useful for tables with a large number of fields. The quickest way to create a simple form, accepting all the defaults is to select the table which forms the basis of the form, then select the Create tab and the click the Form icon in the Forms section. To have more control on what goes into the form: Select the Create tab; then More Forms from the Forms section; and then Form Wizard and follow the steps. Change the Layout of a Form Figure 12 To change the layout of the form, open it in Design View. Forms consist of a number of objects called Controls. The two main types of controls are Labels (left hand column in diagram) which never change and Text Boxes which contain the data from the underlying query (right hand column). When the form is created by a wizard the default id for all the controls in the detail section to be Stacked and considered as one object thus a change made to one of the controls affects them all. To deal with individual controls you need first to remove the stacking. Figure 13 Choose the Arrange tab and then Remove from the Control Layout section. Figure 14 20
Most controls come in pairs, a Label and a Text Box. Selecting a control highlights it with sizing boxes at the corners and edges with a large box at the top right. The associated Control is also selected but without the sizing buttons. To resize, click and drag one of the buttons, the mouse pointer will change to a double headed arrow. To move a control and its associate click in the control and drag, the mouse pointer is a four-headed arrow. To move a control independently from its associate click and drag the large sizing button at the top left of the control. Properties of individual objects may be changed in the properties window obtained by right clicking on the object and selecting Properties button (Figure 15). Some values can be entered directly, some chose from drop down lists which appear when you click on a particular attribute. To add extra controls to the form select them from the Tool bar and drag out a shape on the form. In many cases a wizard will run to help you set the values needed. Change any other properties as required. Figure 15 Task 4: Forms a) Return to tables 1. Click on the External Data Tab, then Excel Icon in the Import and link ribbon section. 2. Browse and select Address Data.xls and press Apply Import to new table, click OK. 3. Make sure the sheet Students address is selected and press Next. 4. Check the box which says First Row Contains Column Headings and press Next. 5. Press Next, then; 6. Select No Primary Key and press Next then press Finish. 7. Close the Save import steps 8. You should now have a table called Students address. 21
b) Create a simple form using Form Wizard 1. Click on the Create Tab and then on the Form Wizard icon; 2. From the Tables/Queries drop down select Students address from the list of Tables; 3. Click the double-arrowed button to select all fields and then click Next. 4. Select Columnar and then click Next. 5. Click on Finish. c) Move controls and delete unnecessary ones 1. Switch to Design View; 2. As the id field can t be edited it has no value on the form, so select and delete it. 3. Move and resize the other controls. d) Change the properties of controls 1. By using the Themes section of the design ribbon, change Fonts and Colours to improve the design of the form. e) Add controls to form (Exit) 1. From the Controls section of the design ribbon, select the Button icon (4 th from the left). Drag out a small rectangle on your form and then a Wizard should appear. From the Categories List choose Form Operations, and from the Actions List choose Close Form. Click on Next. 2. You then have a choice of either text on your button or one of two pictures. Make your choice and then click Next. 3. You are then asked for a name for the button, accept the default and press Finish. 4. Save your form and then open it, click on the button. What happens? f) Add controls to form (Add new record) 1. Repeat Task e) Add controls to Form (Exit) choosing Record Operations and Add New Record. When asked for a name for the button, call it addrec. Then save and test. g) Causing an action automatically 1. In Design View, open the properties window and click in the dark area to the right or below your form. The properties window should be headed Form. Click on Event. Choose On Open. 2. Click on the button with three dots and then choose Macro Builder. 3. Click on the Action Catalog icon and expand the In this database branch on the right pane until the macros become visible. 4. Select the addrec.onclick by double clicking on it. 5. Close and save changes. 6. Close the window, save your form and then run it. What happens now? End task 22
Reports To obtain a printout of results of queries or tables it is possible to open them and click on File Print. Note: changes to page layout are achieved in File Print Setup. However if you wish to have greater control over the layout of your results then the answer is to use a report. Although reports can be designed from new probably the best way to start is to use one of the report wizards and then to modify the result as required. Choose the Create tab then select Report Wizard from the Reports section Select the table or query that your report is going to be based on from the Tables/Queries list. Select those fields from your table or query you require. Click Next. Note: clicking the double arrow selects all fields. The next window allows you to group data so that all values for one Key appear together. Where relationships between tables have been established Access might make suggestions about groupings. Figure 16 Make your selection, and then click Next. The next window allows you to sort the results in the order required. Figure 17 Add the required fields, then click Next. Figure 18 23
Then comes a window asking which layout you would like. Note: the picture on the left of the window shows what each layout looks like. (Outline is probably the best for grouped data). Choose your preferred layout. Press Next. Enter a title for your report. Figure 19 Editing reports is done in the same way as editing forms. When you press Finish an automatically generated report is show in Print Preview. You can close print preview and it will take you back to Design View. Figure 20 Task 5: Reports a) Create a new query called Class list 1. Create a new query from courses, attends and students containing the following fields: CourseName, Tutor, Day, Room, FirstName, LastName and dob. 2. Call the query class list. b) Create a report to show Class list 1. Click on Create Tab and Report Wizard. 2. In the Report Wizard dialogue box, Select the query Class list, from the dropdown menu. 3. Click on the double arrow to select all fields and then click Next. 4. Accept Viewing by CourseName by clicking Next. 5. No extra grouping is needed, so click Next. 6. Sort as ascending by LastName and FirstName, then click Next. 7. Choose Block and click Next. 8. Choose Preview the Report and click Finish. c) Make the report presentable 1. Using the same methods as for Form Design, change the design of this report in order to make it more presentable. 24
d) Creating labels 1. Click on the Students Address table in the left hand pane. 2. Select the Create Tab and the Label icon, a Label Wizard dialogue box should appear. 3. Change the Units of Measure to Metric and choose Avery type J8169 although if you have different labels then obviously choose the sort nearest to the ones you wish to use. 4. Click Next and choose the font and size you wish to use (Arial 10 is good enough for this exercise) 5. Create your label by typing in any text you wish or clicking on the arrow to get the field name which will be filled in with data when the report runs. Press Next. 6. Select any sorting you require and click Next. 7. Click Finish and view the labels. Contacts For queries or help e-mail: ittraining@essex.ac.uk Useful resources Training videos http://www.youtube.com/uniessexit Booking other Access training courses www.essex.ac.uk/it/training/courses/access.aspx Online training End Task Microsoft Access 2010 - training and support in transitioning to Access 2010 with Microsoft Tutorials. Your feedback Help us improve your training Your feedback is important to us. You will receive an email asking you to complete an online feedback survey. Please take a few minutes to tell us what you think. 25