简体   繁体   English

通过Access 2010中的表宏删除给定ID的最后一条记录?

[英]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. 我有一个表格,其中包含一个通过公用相对ID的人与人之间的一对多关系,该表用于例如在地址处将家庭分组在一起。

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. 我想设置一个After Delete Table宏,如果删除倒数第二个记录,它将删除具有给定相对ID的最后一个剩余记录。

In other words: "After Deleteing a row from the REL table, if only one record remains with a given REL_ID, delete it also." 换句话说:“从REL表中删除一行后,如果只有一个记录保留给定的REL_ID,则也将其删除。”

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. 我可以将VBA放在按钮代码中,以便在此表上运行一个删除查询来查找孤行并删除它们,但是我认为最好设置一个触发器,因为将来我可能会在其他地方操作这些记录而忘记了运行清理查询。

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: 以下After Delete数据宏似乎对我有用:

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粘贴( Ctrl + V )到数据宏窗口中,以节省一些单击和键入的操作...

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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