Lesson6.13.Delete Queries


Lesson 6.13. Delete Queries

Figure 6-29. A delete query


Figure 6-30. Access asks you to confirm the query.


Definitely the most dangerous of all queries is a delete query, a query that deletes a whole bunch of records at a time. Once you have deleted records using a delete query, you cannot undo the resultsthe records are gone forever! Creating a delete query is no different than creating a select querywith one very important difference: While a select query displays the records that match your criteria, a delete query deletes those records. For this reason, you should always preview the results of a delete query in Datasheet View to see which records will be deleted.

If you want to delete records from multiple tablesfor example, a customer and all of that customer's ordersyou need to do a few things first:

  • Define relationships between the tables.

  • Establish referential integrity for the join(s) between the tables and turn on the Cascade Delete Related Records option.

LeAnne Chang, one of North Shore Travel's managers, decides to open her own competing travel agency across the street, and being the vindictive person she is, takes all her customers with her. In this lesson you will create a delete query to delete all the tours sold by LeAnne Chang.

  1. From the Database window, click the Queries icon in the Objects bar and click the New button.

    The New Query dialog box appears.

  2. Select Design View and click OK.

    The query design window and Show Table dialog box both appear. You have to select the tables and/or queries you want to use in the delete query.

  3. Click the tblEmployees table and click Add.

    A field list for the tblEmployees table appears in the top half of the query design window.

  4. Add the tblCustomerTours table and tblTours table to the query.

    When you have finished adding the tables and/or queries, you can close the Show Table dialog box.

  5. Click Close.

    If the tables are related, Access automatically connects their common fields with a join line. If the tables aren't related, you will have to manually join them by dragging a field from one table's field list to the matching field in the other table's field list.

    You need to connect the tblEmployees table with the tblCustomerTours table.

  6. Click the EmployeeID field in the tblEmployees field list and drag and drop it onto the Employee field in the tblCustomerTours field list.

    Now you need to tell Access that this is a delete query.

  7. Click the Query Type button list arrow on the toolbar and select Delete Query from the list.


    Tip: Another way to change the type of query is to select Query from the menu and select the type of query from the list.

    Access converts the select query to a delete query and displays the Delete row in the query design grid. Now you have to tell Access what you want to delete.

  8. Drag the asterisk (*) from the top of the tblCustomerTours field list into the design grid.

    Notice that From appears in the Delete cell for the asterisk field, indicating that the records will be deleted from the tblCustomerTours table. Unless you want the query to delete each and every record in the tblCustomerTours table, you will need to add some limiting criteria. You decide to use the last name Chang as the limiting criteria.

  9. Drag the LastName field from the tblEmployees field list into the design grid.

    This time Where appears in the Delete cell for the LastName field, indicating that the LastName field will be used as the criteria to select which records will be deleted from the tblCustomerTours table.

  10. Click the LastName column's Criteria row and type Chang.

    Access will add the "quotation marks" around the text string Chang for you, as shown in Figure 6-29. That's all there is to creating a delete query.


    Note: Before you run a delete query, you should always preview the results in Datasheet view first. Click the View button on the toolbar to display the delete query in Datasheet view.

  11. Click the View button on the toolbar to preview the results of the delete query in Datasheet view.

    The delete query displays the results of the delete query. Let's switch back to Design view.

  12. Click the View button on the toolbar to return to Design view.

    Let's run the delete query and delete the selected records.

  13. Click the Run button on the toolbar.

    Ever-cautious Access asks if you really want the delete query to delete the records, as shown in Figure 6-30.

  14. Click Yes.

    Access silently deletes all 75 tour records for LeAnne Chang.

  15. Close the query without saving your changes and open the qryToursByName query.

    Notice that all the tours for LeAnne Chang are gone.

  16. Close the qryToursByName query.

QUICK REFERENCE

TO CREATE A DELETE QUERY:

  1. IN THE DATABASE WINDOW, CLICK THE QUERIES ICON IN THE OBJECTS BAR AND CLICK THE NEW BUTTON.

  2. SELECT DESIGN VIEW AND CLICK OK.

  3. ADD THE APPROPRIATE TABLES AND QUERIES AND CLICK CLOSE.

  4. CONNECT ANY UNRELATED TABLES.

  5. CLICK THE QUERY TYPE BUTTON LIST ARROW ON THE TOOLBAR AND SELECT DELETE QUERY.

    OR...

    SELECT QUERY

    DRAG THE TABLE FROM WHICH YOU WANT TO DELETE RECORDS AND THE FIELD YOU WANT TO USE AS THE CRITERIA ONTO THE DESIGN GRID.

  6. CLICK THE VIEW BUTTON TO VIEW THE RESULTS OF THE DELETE QUERY.

  7. IF YOU'RE SATISFIED THAT THE APPROPRIATE RECORDS WILL BE DELETED, CLICK THE RUN BUTTON ON THE TOOLBAR AND CLICK YES TO CONFIRM THE DELETION.




CustomGuide Inc - Access 2003 Personal Trainer
Access 2003 Personal Trainer (Personal Trainer (OReilly))
ISBN: 0596009372
EAN: 2147483647
Year: 2005
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net