Access Inter 2007

Embed Size (px)

Citation preview

  • 8/12/2019 Access Inter 2007

    1/18

    Microsoft Access 2007

    An Intermediate Guide (Level 2)

    Contents

    Introduction ..............................................................................................................1Speeding up Table Creation ................................................................................2

    Using Datasheet View ...........................................................................2Using a Table Template .........................................................................3Input Masks ...............................................................................................3Importing Tables .....................................................................................3

    Relationships .............................................................................................................4Finding Unmatched Records ...............................................................................6

    The Find Unmatched Query Wizard .................................................7Action Queries ..........................................................................................................7

    Make-Table Queries ...............................................................................7Delete Queries .........................................................................................8Append Queries .......................................................................................8Update Queries ........................................................................................9

    Crosstab Queries .................................................................................. 10

    Form Design ............................................................................................................ 10List and Combo Boxes ........................................................................ 10Check Boxes, Toggle Buttons and Option Buttons ................. 11Option Groups ...................................................................................... 12Tab Order ................................................................................................ 13Setting Controls in Tables ................................................................. 13

    Switchboards ......................................................................................................... 13Command Buttons .............................................................................. 14Text and Combo Box Buttons ......................................................... 15

    Macros ..................................................................................................................... 16Autoexec ................................................................................................. 17Macro Security Options ..................................................................... 18

    IntroductionThis document is a further guide to Microsoft Access 2007. For an introduction to Access seeMicrosoft Access 2007 - An Essential Guide.If you are still running Microsoft Access 2003, seeMicrosoft Access 2003 - An Intermediate Guide.

    1. Open the Windows startmenu and choose All Programs2. Select Microsoft Officethen Microsoft Office Access 2007

    IT Services

    http://www.reading.ac.uk/web/files/its/AccessBegin2007.pdfhttp://www.reading.ac.uk/web/files/its/AccessBegin2007.pdfhttp://www.reading.ac.uk/web/files/WWW_File_Library/Microsoft_Access_2003_intermediate.pdfhttp://www.reading.ac.uk/web/files/WWW_File_Library/Microsoft_Access_2003_intermediate.pdfhttp://www.reading.ac.uk/web/files/WWW_File_Library/Microsoft_Access_2003_intermediate.pdfhttp://www.reading.ac.uk/web/files/its/AccessBegin2007.pdf
  • 8/12/2019 Access Inter 2007

    2/18

    2

    3. Under Open Recent Database on the right, click on More4. In the Openwindow, change Look in:to User (D:) then double clickon the Trainingfolder to open it5. Click onexample.accdbfrom the list which appears and press or click on [Open]

    Note: For those using these notes on a computer not run by IT Services, the example file can be downloaded fromthe link provided at step 5above. Users are welcome to take a copy of the example file if they want to practice.

    Speeding up Table CreationIn the Beginners' Guide, you were shown how to create a new table using Design View. Here, let's see theadvantages and disadvantages of the other methods of table creation - Datasheet Viewand Table Wizard.

    Using Datasheet ViewNew tables can be created in Datasheet View; this gives you a skeleton table, with the fields labelled Field1,Field2 etc. As you type data into the fields, Access automatically sets up each field's data type and properties

    for you. Try setting up a new table:1. Move to the Createtab and click on [Table]on the far left

    Before typing in any information, it's a good idea to rename the fields you want to use so that you know whatdata to type into each column (though you can do this later, in Design View).

    2. Double clickon the second column heading and rename the fieldSurname - press 3. Rename columns3to 6, naming themFirstName,Sex,Birthday andWeight4. Press twiceand type in the first record (press to move across the fields):

    Dixon Paul Male 3 May 75

    Access automatically assigns data types and properties to the fields (in a similar way to Excel). You could now

    go on and type in more records but, when using Datasheet Viewto create a table, it's always a good idea firstto check the table design has been set up as you want it.

    5. Click on the[View] button to move to Design Viewsave the table asTable1 - press 6. Move down through the fields - note how each has been set up

    Although everything works, there are several improvements which could be made. In particular, the textfields are set to store 255 characters and the weight is stored as a long integer (for numbers >32767). Also,Access has automatically set up an ID field (which started numbering at 6!), which you may not want.

    7. Starting with the IDfield, click on the [Primary Key]button and then [Delete Rows]answer [Yes]8. For both the Surnameand FirstName fields, change the Field Size: property to209. Set a CaptionofFirst Namefor the FirstNamefield10.Drag through Surnameand FirstNamefields then click on [Primary Key]a key can be set on 2 fields11.Move down to the Sexfield and set the Data TypetoYes/No12.To make this read Maleor Femaleset the Formatproperty to;"Male";"Female"13.Set a Default Valueof-1 (for Male) and, on the Lookuptab, change the Display Controlto a Text Box14.For the Birthdayfield and change the Formattod mmmm (to display just the day and month)15.Finally, for the Weightfield, change the Field Sizeproperty to Byte16.Click on the [View]button to revert to Datasheet View- press for [Yes]to save the changes to

    the design

    If you reduce a field size or set up a validation, a warning message appears:

    17.Click on [Yes]to cancel any warning messages which appear and enforce the changes (don't worry toomuch about these messages as you have yet to type in your data)

    http://www.reading.ac.uk/web/files/its/example2007.accdbhttp://www.reading.ac.uk/web/files/its/example2007.accdbhttp://www.reading.ac.uk/web/files/its/example2007.accdbhttp://www.reading.ac.uk/web/files/its/example2007.accdb
  • 8/12/2019 Access Inter 2007

    3/18

    3

    18.The data is all okay except Paul has changed sex move across to this field and type-1 to reset it19.Close the table by clicking on its [Close Window]button then it as you wont be needing it

    Typing -1 might seem a funny way to display this data but remember that a Yes/No field is stored as 0 or -1.To display Femalein future records, simply the default value (or you can type a 0).

    Using a Table TemplateMicrosoft Access has a wide range of sample tables (both for business and personal applications) which canbe invoked from a Template. When you first load up Access, you are asked whether you want to use one ofthese templates to let Access create your database for you. Some of these are very good but you do losecontrol of your database.

    The templates can also be used to create a table in your own database:

    1. Move to the Createtab and click on [Table Templates]in the Tablesgroup on the left2. Choose any of the templates on offer a new table appears3. Move back to the Hometab and click on the [View]button to move to Design View4. Press or click on [OK]to save the table asTable15. Move down the fields and note how each has been set up

    You could now reset the properties and delete the fields not needed, but here:

    6. Close the table by clicking on its [Close Window]button then it - you dont need it

    Input MasksAn Input Maskcan be set on a field whereby only certain characters are permitted in a particular part of thefield. For example a userid here at the University must start with 3 letters (indicating faculty, department andstatus) followed by 2 numbers (year of entry) followed by either 3 more letters or 2 and a number (for theuser's initials). Input masks can also be used to add extra characters automatically, for example a currencysymbol, or to translate a lower case letter to upper case (or vice-versa). To learn more about input masks

    1. Right click on the studentstable and chooseDesign View2. Move down to the Surnamefield and click in the Input Maskproperty3. Press for Helpthen scroll down to see the effect of the special characters

    A >sign translates any following letter into UPPER CASE; LA09\ 0LL, a 4-digit phone extension by \x0000.

    Set an input mask on the Surnamefield to translate any data entered into a Capitalisedname.

    4. For the Surnamefield, set up an Input Maskof>A

  • 8/12/2019 Access Inter 2007

    4/18

    4

    1. Move to the External Datatab and, in the Importgroup, click on [Text File]2. Leave the default setting of Import the source data into a new table in the current database3. Click on [Browse] and change the current directory toTraining(on User (D:))4. Select the filehalls2007.txtthen press to [Open] it then click on [OK]

    (Again, the file can be downloaded from the web by clicking on the hyperlink in step 4).

    The Import Wizard now starts up - to create a new table there are some extra steps:

    5. The data is tab-delimited, so just press or click on [Next >]for Delimited6. The First Row Contains Field Names- click on the box then press for [Next >]

    You now have the opportunity to modify the Field Nameand Data Typeof each field, and can set up an Indexifyou want. You can also skip a field if you don't want to include it in your table.

    7. Set an IndexofYes (No Duplicates)on the (first) NamefieldTo move to another field, simply click on the column.

    8. For the Roomsfield, change the Data Typeto Integerusing the list arrowprovided9. For the Mealsfield, change the Data TypetoYes/No - press for [Next >]10.Choose your own primary keyusing the Namefield - press for [Next >]11.Import to TableHalls2007- press for [Finish]12.Press or click on [Close]to end the Import Wizard13.Double clickon the new table in the Objects list to open it - note that the Mealsfield has values of -1

    (Yes) and 0(No), which needs to be changed

    14.Move to the Home tab then click on the [View]button to move to Design View15.Move down to the Mealsfield and reset the FormatField PropertytoYes/No16.Click on [View]to move back to Datasheet View, saving the changes to the design of the table17.Next, move to the Createtab and click on [Form]a new form appears18.Close the form by clicking on its [Close Window]button save the form asHalls200719.Finally, close the Halls2007 table by similarly clicking on its [Close Window]button

    Tables (queries, forms, reports etc) can also be transferred directly from one database to another - or you canlink two or more databases together. The instructions below import copies of three new tables, including allthe data required for the exercises which follow.

    1. Move to the External Datatab and, in the Importgroup, click on [Access]2. [Browse] for the fileextra.accdb(in the Trainingfolder) then press to [Open] it3. Click on[OK]to Import tables, queries, forms, reports, macros, and modules into the current database4. Finally, click on [Select All]then on [OK]to import them press for [Close]

    Relationships

    Relationships:

    Having set up various tables in your database, you need a way to tell Access how the information in the tablesrelates to each other. You saw how to join two tables together in a query in the Beginners' Guide. You can do asimilar thing for the whole database by defining relationships between your tables. This will thenautomatically draw the join lines when you have more than one table in a query. Further, you can create

    forms and reports to display information from several tables at the same time.1. Move to the Database Toolstab then click on the [Relationships]button2. Next, click on the [Show Table]button

    http://www.reading.ac.uk/web/files/its/halls2007.txthttp://www.reading.ac.uk/web/files/its/halls2007.txthttp://www.reading.ac.uk/web/files/its/halls2007.txthttp://www.reading.ac.uk/web/files/its/extra.accdbhttp://www.reading.ac.uk/web/files/its/extra.accdbhttp://www.reading.ac.uk/web/files/its/extra.accdbhttp://www.reading.ac.uk/web/files/its/extra.accdbhttp://www.reading.ac.uk/web/files/its/halls2007.txt
  • 8/12/2019 Access Inter 2007

    5/18

    5

    3. Double clickon the Halls, Options,studentsand Tutorstables to [Add]them to Relationships4. Press or click on the window's [Close]button to [Close] the Show Tablewindow5. Drag down the bottom border to enlarge each table so that you can see all its fields6. Using drag and drop, create a join line linking the Namefield in the Hallstable to the Hallfield in the

    studentstable

    7. The Edit Relationshipswindow appears - click on [Create]to confirm the type of join

    Note:In this window you can modify theJoin Type(either to select only records where the join field is equal,or to include all records from one of the tables) and can Enforce Referential Integrity(eg a student's tutor mustbe in the Tutors table). Once Referential Integrityis turned on you can Cascade Update/Delete Related Records.This automatically changes data in a linked table (eg if a tutor leaves and a new member of staff takes his/herplace) or deletes records from a database (eg if a customer dies or no longer wishes to use your services).

    8. Set up a join line from the studentstable to the Optionstable, linking via the Optionfield9. Turn on Enforce Referential Integritythen click on [Create]10.Repeat step 8linking the LastNamein the Tutorstable to the Tutorfield in students11.Turn on Enforce Referential Integrityand Cascade Update Related Fieldsfor this link click on [Create]12.Drag the tables around to show the join links clearly - move the Optionstable below Halls13.Close Relationshipsby clicking on the window's [Close]button - press or click on [Yes]to save

    the new design

    Once relationships have been set up, various additional features automatically emerge. For example, if youset up a form for one of the new tables then a subform giving a list of appropriate students is added. Try this

    next for the Halls:1. Clickon the Hallstable in the Objectslist to select it (theres no need to open it)2. Move to the Create tabthen click on the [Form]button in the Forms group

    Note how, for each tutor, a list of students with that tutor appears. You can use or therecord indicator at the bottom to move between tutors.

    3. [Close]the form by clicking on the window's [Close]button - save it as Halls4. Repeat steps 1to 3 to create new forms for the Optionsand Tutorstables

    You can also see related information when viewing a table:

    5. Double clickon the Hallstable in the Objectslist to open it6. Click on the +sign on the far left of a record to see a list of students in that hall7. Finally, click on the -sign to close the list then [Close]the Hallstable

  • 8/12/2019 Access Inter 2007

    6/18

    6

    A second additional benefit of relationships is that the links are automatically shown whenever you use morethan one table in a query. As an example, regenerate the query for the students' addresses (used for mailinglabels in the Beginners' Guide):

    1. Move to the Createtab then click on the [Query Design] button2. [Add]the Hallsand studentstables - press to [Close]the Show Tablewindow

    Note that the join line is automatically drawn between the two tables.3. Double clickon the FirstName, Surnameand Hall fields in the studentstable to fill the Fieldin the first

    three columns

    4. Double clickon the Roadand Townfields in the Hallstable to fill Fieldin columns 4 and 55. Click on the [Run]button to run the query

    You will find that only 207 records (out of the 390) are being displayed - almost half are missing! The reasonfor this is that there is an inconsistency between the two tables on the linked field. You will see next how toidentify such problems.

    6. Click on the [Save]button, saving the query asAddresses - press for [OK]

    Finding Unmatched RecordsThere are two ways to identify inconsistencies between data in two or more tables - you can either modifythe join line or use a special Query Wizard.

    First, try modifying the join line:

    1. Click on the [View]button to move to Design View2. Right clickon thejoin lineand selectJoin Properties(or use single left click to select it, then double click

    to show the properties)

    You will see that the current join only selects records from both tables where the fields are equal

    3. Change the property to showALLthe records in the studentstable (here, option 3) - press for [OK]

    4. Add the Namefield from the Hallstable to the Query Designby double clickingon it5. Click on [Run] to run the amended query

    You now have all 390 records. Note which have empty fields - those for students living in Bridges, Childs orPrivate accommodation. If you remember, you had to type in the Bridges and Childs records yourself on the

    Beginners' course and would have to do so again to complete the table here.6. Close the query by clicking on the window's [Close]button - don't save the changes to the Query

    Design(you only modified it to identify the problem) - click on [No]

  • 8/12/2019 Access Inter 2007

    7/18

    7

    The Find Unmatched Query WizardThe alternative way to find unmatched records is to use the special wizard:

    1. Move to the Createtab then click on the [Query Wizard]button2. Choose Find Unmatched Query Wizardthen press for [OK]

    Note:There is also a Find Duplicates Query Wizardto identify duplicate records.3. Choose studentsas the table containing the records you want - press for[Next>]4. Choose Hallsas the table containing the related records - press for[Next>]5. Match the fields in the tables by selecting Hallin the studentstable and Namein the Hallstable

    (Access has already done this for you) - click on []then on[Next>]

    6. Move across the Surnameand Hallfields to the query results by clicking on them - press for[Next>]

    7. Press for [Finish]to end the wizard and run the queryThe unmatched records are now displayed - again this shows that students living in Bridges, Childs or Privateaccommodation are not properly matched. To view the design of the query:

    8. Right click on the querys tab and choose Design View to move to Query DesignNote the notation Is Nullon the Criterialine - this is what you use to identify an empty field.

    9. Close the query by clicking on the window's [Close]button

    Action QueriesIn the Beginners' Guideyou were introduced to queries. There are however two main sorts of queries - selectqueries and action queries. What you have seen so far have all been select queries (they select records fromtables based on some criteria); action queries, on the other hand, are used to perform one of the followingtasks:

    Copy records into a new table - a Make Tablequery

    Delete records from a table - a Deletequery

    Append records to an existing table - anAppendquery

    Update the values in one or more fields in a table - an Updatequery

    A third type of query, a Crosstabquery, can be used to summarise the data in one or more tables. Let's look ateach of these in turn.

    Make-Table QueriesA good example of a Make Tablequery on the studentsdatabase would be to archive the records for thosestudents graduating at the end of the year. First you create a select query which identifies them; then youturn it into an action query:

    1. Move to the Createtab then click on the [Query Design]button2. [Add]the studentstable - press to [Close]the Show Tablewindow3. Double clickon *to set the first Fieldto students.*4. Double clickon Useridto set the second Field5. For this field, turn off Showthen for the Criteria:type*06*

    Tip:To set this up as a parameter (ie for any year) set the criteria toLike *&[Which Year]&*6. Click on the [View]button - you should have 128 records selected (always check your results first)7. Move back to Query Designby clicking on the [View]button

  • 8/12/2019 Access Inter 2007

    8/18

    8

    8. On the Query Tools Designtab click on the [Make Table]button9. When asked, set a new Table NameofOldStudents - press for [OK]10.Click on the [Run]button - press for [Yes]to verify you want the records in the new table11.[Save]the query asMakeTable

    Note that the new table has been added to the Tables in the Objectslist.

    Delete QueriesThe same query can be used as the basis of a Delete Query- now that the old students have been copied into anew table, their records can be removed from the table of current students:

    1. On the Query Tools Designtab, click on the [Delete]button in the Query Type group2. Click on the [Run] button - choose [Yes]to permanently delete the records3. Click on the [Office Button]and use Save As...to rename the queryDeleteRecords4. Close the query by clicking on the window's [Close]button5.

    Double click onthe studentstable in the Objectslist - you should find only 262 records remain

    6. Close the table by clicking on the window's [Close]buttonWarning:A Delete Query can be a very dangerous thing - it's very easy to accidentally delete the wrongrecords as it runs as soon as it is opened. It is advisable to save it as a Select Query and then, once you are surethe correct records are being selected, turn it into a temporary Delete Query just before you run it.

    Append QueriesAs an example of an Append Query, let's put the old students back into the studentstable - normally youwould append the OldStudentstable to an accumulative table of PastStudents:

    1. Move to the Createtab then click on the [Query Design]button2. [Add]the OldStudentstable - press to [Close]the Show Tablewindow3. Double clickon *to set the first Fieldto OldStudents.*4. In the Query Typegroup click on the [Append] button5. Set the Append To Table Name:to students- press or click on [OK]6. Click on [Run]then press for [Yes]to complete the query

    7. The above error message appears, click on [Yes] to run the query anyway8. Close the query, using the window's [Close]button, saving it asAppendRecords

    If you like, check the studentstable again has 387 records (following the validation rule violations).Incidentally, these violations are a mystery - the offending records originated from a table with exactly thesame design and can be copied/pasted into the new table without a problem!

  • 8/12/2019 Access Inter 2007

    9/18

    9

    Update QueriesUpdate queries are often used in businesses, for example to raise wages or prices by a given percentage or toreflect a change in tax rates. Here, let's make use of the OldStudents table to update a text field - you're goingto add the word Hallto each student's hall of residence (where appropriate).

    1. Move to the Createtab then click on the [Query Design]button2. [Add]the OldStudentstable - press to [Close]the Show Tablewindow3. Double clickon Hallto set the first Field:to Hall4. In the Query Typegroup, click on [Update]5. Set the new Update To:row to[Hall] & " Hall"- don't forget the space in " Hall"

    Note:The square brackets here denote the current field.

    6. In the Criteria:row typeNot Private7. In the Or:row typeNot Student Village

    You can also use the notation to signify not equal - students not living in a hall do not need the extraword added. Before you run the update, its always good idea to check you have set the criteria correctly:

    8. Click on the [View]button and note the resultsAs you can see, the query is not correct it includes all the students!. If you think about the logic of thecriteria, a student living in the Student Village would satisfy the criterion Not Private, while another in Privateaccommodation would similarly be covered by Not Student Village. In this case you need an AND logical test,not an OR:

    9. Click on the [View]button again to return to Design View10.Change the Criteria: to readNOT Private AND NOT Student Village11.Click on [View]to check the criteria is now correct (and again to return to Design View)12.Now click on [Run]- press for [Yes]to update 77 of the 128 records13.Close the query, saving it asUpdateRecords14.Double click on the OldStudentstable in the Objectslist to open it

    You will find that the OldStudentstable has been updated, but not always successfully. This is because theHallfield has a Field Sizeof only 15 characters. You should have increased this to 20 to accommodate theextra letters. Fortunately, it doesn't matter here as you are only learning, but you need to be aware ofpotential problems such as this as updates may not be easily reversible.

    15.Close the OldStudentstable by clicking on the window's [Close]buttonAnother way of updating data is provided through the join options set up in Relationships. In the earliersection on this you set up Cascade Update Related Fieldson the link between a student's Tutorand the

    LastNamefield in the Tutors table. Try out this next exercise to see the effect:1. Double click on the Tutorstable in the Objectslist to open it2. Change the LastNameof one of the tutors toyour_own_name(or typexxxxx if your name is already a

    tutor)

    3. Close the Tutors table by clicking on the window's [Close]button4. Now open the studentstable

    You should find that several students haveyour nameshowing as their tutor instead of the original name - therelated records have been automatically updated.

    5. End by closing the studentstable - click on the window's [Close]button

  • 8/12/2019 Access Inter 2007

    10/18

    10

    Crosstab QueriesCrosstab queries are used to summarise data in tables. As an example, let's count up each tutor's number ofoverseas and non-overseas students:

    1. Move to the Createtab then click on the [Query Wizard]button2. Choose the Crosstab Query Wizard- press for [OK]3. Base your query on the OldStudentstable - click on [Next >]4. Move Tutorfrom theAvailableto the Selected Fieldsby clicking on [>]to set up the row heading- click

    on [Next >]

    5. Click on Overseasto set up the column heading- click on [Next >]6. Summarize each row using Countfrom the list of Functions- click on [Next >]

    Note that you could also find the maximum/minimum values, the sum or average of any of the availablefields.

    7. Press to [Finish]the query, accepting the default query nameNote that Yes/Nofield column headings appear as -1(Yes) or 0(No) in the summarized table

    8. Close the query by clicking on the window's [Close]button

    Form DesignIn this next section you are going to learn more about the design of forms and, in particular, how to use someof the controls provided.

    1. Double clickon the Optionsform in the Objectslist to [Open]itSeveral improvements could be made to the form produced byAutoForm. Some of the boxes (eg option) aretoo large; some fields (eg tutorand day) could be filled in from a list; some fields (eg termand assessed) couldappear as check boxes. In order to have these you must change the form design manually.

    2. Right click on the form background (or the forms tab) and choose Design View3. Try altering the width of the optionText Box(the box on the right) click to select it then move the

    mouse cursor to the far right border, hold down the mouse button, and drag the border to the left

    Youll find that you are changing the size of all the boxes by default, Access 2007 links all the controls in apre-defined tabular format. To unlink them:

    4. First, press to [Undo]the resizing then click on the tableindicator just above the option label5. Next, move to the Form Design Tools Arrange tab and click on the [Remove]button6. Now, click on the form background (to release the selection) then on the optionText Box again7. Move the mouse over the right middle handleand drag this left to shrink the box to one unit wide8. You could repeat step 7to resize some of the other boxes, if you wanted to

    List and Combo Boxes

    Combo Box: List Box:

    Next you are going to replace the existing Tutorbox with a special type of list box, called a combo box.Whereas a list box displays all the possible values on several rows, a combo box has a list arrowattached to asingle row.

    1. Click on the tutorText Box(on the right) to select it then press to get rid of it2. Move to the Form Design Tools Design tabthen click on the [Combo Box]tool in the Controlsgroup3. Move the mouse onto the form to the location of the original box and click once

  • 8/12/2019 Access Inter 2007

    11/18

    11

    A Combo Box Wizardwill start automatically.

    4. Press or click on [Next >]for I want the combo box to look up the values in a table or query.5. Select the Tutorstable - press or click on [Next >]6. Set LastNameas the Selected Fieldby clicking on [>]- click on [Next >]to move on7. Set the Sort order to LastName - click on [Next >]8. Adjust the width of the column if necessary (double clickon the column heading right border) - pressor click on [Next >]9. Store the valuein the tutorfield using the list arrow- press or click on [Next >]10.Typetutor: as the label to appear on the form - press or click on [Finish]

    Don't worry about lining up the boxes properly - you will do that later.

    Combo (and List) Box lists can either be looked up in a table (as above) or typed in manually. For the days ofthe week:

    11.Click on the dayText Box( on the right) to select it then press to get rid of it12.Again, click on the [Combo Box]tool in the Controls group on the Ribbon13.Move the mouse onto the form to the location of the original box and click once14.Select I will type in the values that I want.then press or click on [Next >]

    You now have to type the values you want into the column provided:

    15.Click in the first cell and typeMonday then press or to move to the secondrow

    16.Continue typingTuesday,Wednesday,Thursday,Friday into the rows (double clickon thecolumn border to fit) then press or click on [Next >]

    17.Store the valuein the dayfield using the list arrow- press or click on [Next >]18.Typeday: as the label you want to appear on the form - press or click on [Finish]

    Check Boxes, Toggle Buttons and Option Buttons

    Check Box: Toggle Button: Option Button: Property Sheet:

    Access provides you with three very similar objects which can be placed on a form where you want to be ableto select from a very limited number of alternative values, namely check boxes, toggle buttonsand optionbuttons. All three perform in much the same way but their appearance differs. For a Yes/Nodata type, forexample, you can have a single button or box which can be turned on(forYes) or off(for No). Here, replacethe existingAssessedbox by a check box:

    1. Click on the assessedText Box(on the right) to select it then press to get rid of it2. Click on the [Check Box]tool in the Controls group on the Ribbon3. Move the mouse to the position of the original box and click once

    Sadly, there is no wizard to help you create the check box - you have to do the work yourself:

    4. Click on the [Property Sheet]button in the Toolsgroup on the far right of the RibbonYou have yet to meet Properties, which at first sight seem very complicated but which in fact aren't that bad.Access allows you to set up all sorts of things here and there isn't time to cover everything in detail, so trychanging just a couple of settings:

    5. Click on theData tab to view some of the properties6.

    Change the Control Sourceto assessedby picking from the list provided by the list arrow- this sets upthe field in which the data is stored

    7. Set a Default ValueofYes

  • 8/12/2019 Access Inter 2007

    12/18

    12

    8. Click on theAll tab to view all the properties9. Scroll down to get an idea of what else can be set then click on the panes[Close]button

    To reposition and rename the default label:

    10.Click on the label (Check 19) to select it11.Move the mouse to its top left handle12.Hold down the mouse button and drag the label to line up with the other labels on the left13.Click inside the box then the existing name and type inassessed: instead14.Press when you have finished typing

    Option Groups

    Option Group:

    When you have more than two alternatives, you cannot use a single button or box but instead have to createan Option Group. Try this for the Termfield:

    1. Click on the termText Box(on the right) to select it then press to get rid of it2. Click on the [Option Group]tool in the Controlsgroup on the Ribbon3. Move the mouse onto the form into the space at the top to the right of option/tutorand click once

    A Wizard is provided to help you. First you need to supply labels for the various options:

    4. TypeAutumn as the first Label Name, press or and typeSpring ()thenSummer - press or click on [Next >]

    5. Set up the default value, if you want (eg Autumn) - press or click on [Next >]6. There's no need to change the values here - press or click on [Next >]7. Store the values inthe termfield using the list arrow- press or click on [Next >]8. Choose which sort of box/button you require (eg Toggle Buttons) - click on [Next >]9. Typeterm: as the caption for the option group - press or click on [Finish]

    You can now move the component parts of the option button around, if you want. For example, you couldline the label up with the other labels on the left hand side of the form, or you could have the toggle buttonsacross the form in a row rather than a column. You may also want to realign all the objects and make themevenly spaced:

    10.Click on the Autumntoggle button and drag it to the space to the right of the assessedcheck box11.Repeat step 10to place Springand Summerto the right ofAutumn12.Hold down and click on the Springand Autumnbuttons all three should now be selected13.Move to the Format Design Tools Arrangetab and, click on [Top]in the Control Alignmentgroup14.Next, click on the [Make Horizontal Spacing Equal]button in the Position group your buttons

    should now be properly aligned and spaced

    15.Click on the border which surrounded the group and resize it to the new position using the handles16.Finally, click on the termLabeland, using the handle, drag it to its original position between the time

    and assessedlabels

    Another way to select the controls on a form is to drag through them:

    17.Position the mouse cursor to the left of the Table.studentssub-form then hold down the mouse buttonand drag the rectangle which appears so that it also touches the assessedcontrol

    18.Let go of the mouse (both controls are selected) then use to move the controls down19.Repeat steps 17and 18to select the termtoggle buttons and move them to the left20.Now, drag the mouse down the left-hand ruler to select all the controls except the sub-form

  • 8/12/2019 Access Inter 2007

    13/18

    13

    21.With all the controls selected, click on [To Grid]in Control Alignmentthen on [Make Vertical SpacingEqual]in the Positiongroup other buttons here [Increase/Decrease Vertical Spacing]

    22.Click in the background to release the selection then drag through the Labelcontrols (the ones on theleft) to select just them and then click on [Right]in the Control Alignmentgroup

    23.Finally, use the [Format Painter] on the Design tab to copy the font settings from the option box to theboxes with black text (ie tutor, term, dayand assessed) double clickon the button to turn this on/off

    24.Click on the [View]button to see the redesigned form25.Press to move through the form and watch what happens

    Tab OrderYou will find that the order of data entry has changed as you through the fields. Whenever youreplace an existing box with a new one, the new one is added to the end of the list. You can set up the TabOrderfor a particular box (it's one of its Properties) or you can set it up for the whole form:

    1. Right clickon the form background and chooseDesign View2.

    Right clickanywhere in the Detailbackground and select Tab Order...

    You can now set up the order to whatever you want by dragging the fields up and down the list. More simply,if you want the order to move from the top of the form to the bottom, simply:

    3. Click on Auto Orderto automatically reset it then click on [OK]to close the Tab Orderwindow4. Click on the [View]button and through your data again - it should now work properly5. Close the form using its [Close]button - save the changes to the design

    Setting Controls in TablesIn the above example, changes to the controls were made on a form, but you can also do so in a table:

    1. Right clickon Optionsin the Tableslist and choose Design View2. Move down to the tutor field then click on the Lookuptab in the Field Properties3. Change the Display Control to a Combo Box4. Change the Row Source to the Tutors table5. Repeat steps 2and 3 on the day field, setting the Row Source Type to a Value list6. For the Row Source type inMon,Tues,Wed,Thur,Fri7. Press tosave the changes to the design of the table then click on the [View]button to move

    to Datasheet View- note that you now have combo boxes for the tutorand dayfields

    8. Next, move to the Create tab and click on [Form]9. Note how combo boxes appear automatically on the form then [Close] it - there's no need to save it10.End by closing the Optionstable save the changes to the design

    Though the combo boxes do work in the table itself, it's always better to use a form for data entry.

    Switchboards

    Image: Unbound Object: Bound Object:

    There's a special type of form, called a Switchboard, which you can set up such that anybody can use yourdatabase even if they know nothing about Access. A switchboard is essentially a blank form covered bybuttons which open forms, run queries or produce reports. All the user has to do is simply click on thebuttons provided.

  • 8/12/2019 Access Inter 2007

    14/18

    14

    1. Move to the Createtab and click on the [Form Design] buttonYou are now presented with a blank form, which you will have to design from scratch yourself.

    2. Drag the Detailborders out to fill the whole screen3. Right clickanywhere on the Detailand select a Fill/Back Color4. Click on [Label]in the Controlgroup then click on the form where you would like a title to appear5. Type in a title for your form (egDepartment Student Records) then press 6. Change the [Font], [Font Size]and [Font/Fill Colour]to whatever you require7. Finally, right click on the title and choose Sizethen To Fit

    If you would like a picture as part of the background:

    8. Click on the [Image] buttonNote:An Imagecontrol is normally used for adding a picture to a form. An Object Frameis mainly used foradding other objects, such an Excel spreadsheet or Word document, but it can be used for a picture. UnboundObjectsare not linked to any data held in the tables of the database. If you want to include a picture on a form

    which is held in a table (eg a student's photograph) then you use a Bound Object Frame.9. Move the mouse into the Detailwhere you would like the picture to appear then hold down the

    mouse button and draw out a rectangle to the size required

    10.You are now asked to Insert a Picture- choose your own, if you have a suitable file, or use one from theSample Picturesfolder (in My Documents) choose it then press for [OK]

    11.Resize the picture, if necessary, using the handles provided12.Move the picture to exactly where you would like it on the form either using the arrow keysor by

    holding down the mouse button inside the frame and dragging it to its new position

    Command ButtonsCommand Button:

    To add command buttons to the switchboard:

    1. Click on the [Button] button in the Controlsgroup on the Form Design Tools Design tab2. Move the mouse cursor to where you would like the button to appear on the Switchboardand click

    once - a Wizard starts up to help you set up the necessary instructions:

  • 8/12/2019 Access Inter 2007

    15/18

    15

    Try creating some buttons to open your forms:

    3. In Categories:choose Form Operationsand inActions:select Open Form - press for [Next >]4. Select studentsfrom the available forms - press for [Next >]5. Press for [Next >]to Open the form and show all the records.6. Choose Text:for your button but modify it to readStudents- press for [Next >]7. Name the buttonOpenStudentForm - press for [Finish]8. Repeat steps 1to 7but this time for opening the Optionsform

    Next, add a command button to run a query:

    9. Repeat steps 1and 210.In Categories:choose Miscellaneousand inActions:select Run Query - press for [Next >]11.Select Addressesfrom the available queries - press for [Next >]12.Choose Text:for your button but modify it to readAddresses - press for [Next >]13.Name the buttonRunAddressesQuery - press for [Finish]

    You also need a button to close the Switchboardand QuitAccess:

    14.Repeat steps 1and 215.In Categories:choose Applicationand inActions:select Quit Application - press for [Next >]16.Keep your button as a Picture(the Stopsign is fine) - press for [Next >]17.Name the buttonExitDatabase - press for [Finish]

    Finally, try using the Switchboard:

    18.Right clickon the Switchboardand choose Form View (or use the [View]button on the Hometab)19.Try out the various buttons (except Stop) to see what they do

    You will soon realise that you need to add corresponding command buttons to Closeyour forms (instead of

    having to click on the window's [Close]button).20.Click on your Studentsbutton then on [View]to switch to Design View21.Click on the [Button]button then click on the form where you would like the control to appear22.In Categories:choose Form Operationsand inActions:select Close Form- press for [Next >]23.Amend the Text:to read Close- press for [Next >]24.Name the new buttonCloseStudentsForm- press for [Finish]25.Repeat step 21but this time choose Record Operationsand note what's available26.UnderActions:select Print Record- press for [Next >]27.Keep the Text:asPrint Record- press for [Next >]28.Name the buttonPrintForm- press for [Finish]29.Click on the [View]button to see the amended layout then try out your new [Close]button - save the

    changes to the design of the form when asked

    30.Press to save your changes call the formSwitchboard

    Text and Combo Box ButtonsAs an alternative to command buttons on the Switchboard, you can provide boxes (or combo boxes) for usersto fill in (or select from). Once the user has provided this information, it can be passed to a query which runsautomatically. Try this next:

    1. Right clickin the background of the Switchboardand choose Design View

  • 8/12/2019 Access Inter 2007

    16/18

    16

    2. Next, click on the [Text Box]button in the Controlsgroup then on the Switchboardwhere you wantthe box to appear

    A Text Boxand Labelappears note that the former is Unbound.

    3. Click on the Labelon the left and replace the current text withName of Student4. Now move to the Createtab and click on [Query Design]5. [Add]the studentstable then press to [Close]the Show Tabledialog box6. Double click on the *to set the first field in the query to students.*7. Double click on Surnameto set the second field in the query to Surnameand turn off Show8. Move down to the Criteria:in the second column then click on [Builder]in the Query Setupgroup9. Double clickon Formsthen Loaded Formsand select Switchboard10.Finally, double click on Text3 to set up the criteria Forms![Switchboard]![Text3]then clickon [OK]

    Note: there should only be the one Textbox but it may not be called Text3on your switchboard.

    11.[Close]the query, saving it asLookupStudent12.Select the new query in the Objects list then move to the Createtab and click on [Form]13.[Close]the form, saving it asLookupStudent

    You will need a macro to open this form as youll see later. Next, try creating a combo box:

    14.Move to the Designtab and click on the [Combo Box]button in the Controlsgroup15.Now click on the Switchboardwhere you want the box to appear the Wizardstarts up16.Run through the wizard, as earlier in the course, picking up the values from the Hallstable and using

    the Namefield (inAscendingorder) as a final label for the button typeWhich Hall?17.Now create a new query, as in steps 4to 10 above, based on the Hallstable with Nameas the second

    field and set the Criteria:to Forms![Switchboard]![Combo9](again, it might not be Combo9)

    18.Repeat steps 11to 13, saving the query and form asLookupHall

    MacrosA macro holds a series of instructions (commands) such that all the instructions can be carried out simply byrunning the one macro. Once a macro has been defined it can be assigned to a button on a form so that theuser just clicks on that button to carry out the instructions held in the macro. Macros are not covered indetail in this course but, to demonstrate what they are and how they work, you are going to create a coupleof simple examples.

    The command buttons you just created on the Switchboardcould have been made by writing your own

    macros. Indeed, if you want slightly more sophisticated buttons, you will need to use a macro anyway. Let'screate a new macro to open the Tutorsform:

    1. Move to the Createtab and click on the [Macro]button on the right the following screen appears:

  • 8/12/2019 Access Inter 2007

    17/18

    17

    2. Click on the [Arguments]button on the Macro Tools Design tab youll set these in the area below3. In the action column, click on the list arrowon the right and chose MsgBoxfrom the list4. In the bottom half of the screen, type a MessagesayingYou are about to look at the Tutor's details!5. Set the Typeto Warning!6. Click on the second line in the action column and open the list arrow7. From the list choose Open Form8. In the bottom half of the screen, set the Form Nameto Tutors9. Set the Data Modeto Read Only- this prevents anyone accidentally changing the data10.Close the macro by clicking on the window's [Close]button - save it as OpenTutorsForm

    Now you need to add the macro to your Switchboard:

    11.Click on the [Button]button on the Form Design Tools Design tab then click on the Switchboardwhere you would like the button to appear

    12.Under Categories:choose Miscellaneousand underActions:select Run Macro - press for[Next >]

    13.Select OpenTutorsFormas the macro to be run - press for [Next >]14.Choose Text:for your button but modify it to readTutors - press for [Next >]15.Name the buttonOpenTutorsForm - press for [Finish]16.Click on the [View]button and try out the new button - note that you can't change the data17.Add a Closebutton, as before, then [Close]the Tutorsform - save the changes to the design

    Next, set up macros to open the LookupStudentand LookupHallforms

    1. Move to the Createtab and click on the [Macro]button2. Set the firstAction to Open Form and, in the lower half, set the Form Nameto LookupStudent3. Set the Data Modeto Read Only then [Close]the macro - save it asOpenLookupStudent4. Repeat steps 1to 3 for the LookupHallform, saving the macro asOpenLookupHall

    To activate the macros:

    5. Right clickon the switchboard background and choose Design View6. Right click on the UnboundName of Studenttext box and choose Properties7. Click on the Eventtab and set After Updateto OpenLookupStudentusing the arrow provided8. Next click on the UnboundWhich Hall?combo box and set On Changeto OpenLookupHall9. Click on [Property Sheet] to [Close]the Property Sheettask pane then press to [Save]the

    changes to the Switchboard

    10.Click on the [View]button to move to Form Viewand try out the new controls11.TypeSmithfor the student name and press then try out the hall combo box

    Note: youll need to add [Close]buttons to the forms to make them easier to use.

    AutoexecTo finish, you are going to create a special macro calledAutoexecwhich is run automatically whenever thedatabase is opened:

    1. Move to the Createtab and click on [Macro] for a new macro2. Click on the list arrowin the action column and choose OpenForm3. In the bottom half of the screen, set the Form Nameto Switchboard4. Move to the second row and set the action to Maximize

  • 8/12/2019 Access Inter 2007

    18/18

    18

    5. Close the Macro Windowby clicking on the window's [Close]button6. Press for [Yes] to save the macro; call itAutoexec

    Now test out the macro:

    7. Click on the [Office Button]and choose CloseDatabase8. Now Open it again - it should be in the list of Recent Files

    You should find that the Switchboard is automatically displayed.

    9. Click on the [Stop]button to close the database and ExitAccess

    Macro Security OptionsSometimes when you open a database with macros, a warning message appears:

    1. Press or click on [OK]This is followed by a secondAction Faileddialog box with a button to [Stop All Macros]you have no otherchoice. The database will then open but with the macros disabled. A Security Warningappears on theMessage Barimmediately below the Ribbon:

    To activate the macros:

    2. Click on the *Options+button on the Message Baranother dialog box appears3. Choose Enable this contentthen press for [OK]

    The macros should now be enabled.

    Though it is not recommended (by Microsoft) you can disable this security setting so that the macros runautomatically for any database you open. This should not be a problem here at the University (or at home foryour own databases). To change the security settings:

    4. Click on the [Office Button]and then on [Access Options] at the foot of the dialog box 5. Next, choose Trust Centeron the left then click on [Trust Center Settings]on the right6. Choose Macro Settingson the left then Enable all Macros (not recommended )7. Click on [OK]twiceto close the dialog boxes

    Trademark owned by Microsoft Corporation.

    Screen shot(s) reprinted by permission from Microsoft Corporation.Copyright 2008: The University of ReadingLast Revised: August 2010