简体   繁体   中英

Delete last record of a given ID via table macro in Access 2010?

I have a table containing one-to-many relationships between people via a common Relative ID, used for grouping families together at addresses, for example.

The Relative table is pretty simple:

REL_PK autonumber, primary key
REL_ID number - non-unique, matching numbers mean related
PERSON_ID - ID of a record on the person table

Each relationship needs at least two rows in order to be complete; I want to set up an After Delete Table Macro which will delete the last remaining record with a given Relative ID if the second-to-last record is deleted.

In other words: "After Deleteing a row from the REL table, if only one record remains with a given REL_ID, delete it also."

I can put VBA in the button code to simply run a delete Query on this table which looks for lone rows and deletes them, but I figured it would be better to set a trigger, since I may manipulate these records elsewhere in the future and forget to run the cleanup query.

I've already checked, and it seems it is not possible to trigger an update query from a Table Macro.

The following After Delete data macro seems to be working for me:

AfterDelete.png

If you like, you can apparently paste ( Ctrl + V ) the following XML into the data macro window to save yourself some clicking and typing...

<?xml version="1.0" encoding="utf-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application">
  <DataMacro Event="AfterDelete">
    <Statements>
      <Action Name="SetLocalVar">
        <Argument Name="Name">RemainingCount</Argument>
        <Argument Name="Value">DCount(&quot;*&quot;,&quot;Relative&quot;,&quot;REL_ID=&quot; &amp; [Old].[REL_ID])</Argument>
      </Action>
      <ConditionalBlock>
        <If>
          <Condition>[RemainingCount]=2</Condition>
          <Statements>
            <LookUpRecord>
              <Data Alias="DeleteMe">
                <Reference>Relative</Reference>
                <WhereCondition>[REL_ID]=[Old].[REL_ID] And [REL_PK]&lt;&gt;[Old].[REL_PK]</WhereCondition>
              </Data>
              <Statements>
                <Action Name="DeleteRecord">
                  <Argument Name="Alias">DeleteMe</Argument>
                </Action>
              </Statements>
            </LookUpRecord>
          </Statements>
        </If>
      </ConditionalBlock>
    </Statements>
  </DataMacro>
</DataMacros>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM