Microsoft KB Archive/88635

{|
 * width="100%"|

INF: How to Force a Cascading Delete
'Article ID: Q88635

Creation Date: 30-AUG-1992

Revision Date: 19-SEP-1996' The information in this article applies to:


 * Microsoft Access versions 1.0, 1.1

SUMMARY

The referential integrity capabilities in Microsoft Access versions 1.0 and 1.1 do not extend to performing cascading deletes. A cascading delete removes all the records of a child table when the corresponding record in the master table is deleted. This article describes a sample macro you can use to perform a cascading delete when you delete a record from a master table.

MORE INFORMATION

As an example of a cascading delete, suppose you are viewing the Categories table in the sample database NWIND.MDB and you decide to delete the BEVR Category ID. The Products table is related to the Categories table with referential integrity enabled, and contains many child records with the BEVR value. Therefore, Microsoft Access prevents you from deleting the record in the Categories table until you delete all the child BEVR records in the Products table. With the ability to perform a cascading delete, all of the Products table's BEVR records would be deleted automatically and then the BEVR Category ID would be deleted from the Categories table.

The following example demonstrates how to set up a macro that will perform cascading deletes:

WARNING: Following these steps will permanently remove records from the sample database NWIND.MDB. It is a good idea to make a backup copy of the NWIND database before following these steps, and then restore the database when you are done following these steps.

 Open the sample database NWIND.MDB.  Create the following new macro, and then save it as Cascade: NOTE: In the following sample macro, an underscore (_) is used as a line-continuation character. Remove the underscore when re-creating this macro. Condition                                           Action MsgBox(&quot;Delete record & all child records?&quot;,33)<>1  CancelEvent ...                                                 StopMacro SetWarnings SendKeys RunSQL SetWarnings Action --    Warnings On: No      SendKeys Action ---    Keystrokes: {enter} RunSQL Action ---    SQL Statement: DELETE * FROM [Products] WHERE [Category ID] = _ Forms![Categories]![Category ID]; Note the semicolon at the end of the SQL statement; it is required.  Open the Categories form in Design view. Set the form's OnDelete property to the Cascade macro. View the form in Form view.

When you delete a Category ID in the Categories form, all the associated records in the Products table will be deleted. To delete a record in the Categories form, choose the Select command from the Edit menu, and then choose Delete from the Edit menu. NOTE: Make sure that the subform RecordsLocks property is not set to All Records.
 * }

-

"THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY." '' ©1997 Microsoft Corporation. All rights reserved. Legal Notices.

''

Additional reference words: 1.00 1.10

KBCategory: kbusage kbhowto

KBSubcategory: QryMktbl