After Delete macro event
Applies to: Access 2013, Office 2013
The After Delete event occurs after a record is deleted.
Note
The After Delete event is available only in Data Macros.
Remarks
Use the After Delete event to perform any actions that you want to occur when a record is deleted. Common uses for the After Delete include enforcing business rules, workflows, updating an aggregate total, and sending notifications.
When the After Delete event occurs, the values contained in the deleted record are still available. You may want to use a deleted value to increment or decrement a total, create an audit trail, or compare to an existing value in a WhereCondition argument.
You can use the Updated("Field Name") function to determine whether a field has changed. The following code example shows how to use an If statement to determine determine whether the PaidInFull field has been changed.
If Updated("PaidInFull") Then
/* Perform actions based on changes to the field. */
End If
You can use access a value in the deleted record by using the following syntax.
[Old].[Field Name]
For example, to access the value of the QuantityInStock field in the deleted record, use the following syntax.
[Old].[QuantityInStock]
The values contained in the deleted record are deleted permanently when the After Delete event ends.
The following macro commands can be used in the After Delete event.
Command Type |
Command |
---|---|
Program Flow |
|
Program Flow |
|
Program Flow |
|
Data Block |
|
Data Block |
|
Data Block |
|
Data Block |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
|
Data Action |
To create a Data Macro that captures the After Delete event, use the following steps.
Open the table for which you want to capture the After Delete event.
On the Table tab, in the After Events group, click After Delete.
An empty Data Macro is displayed in the macro designer.
Example
The following code example uses the After Delete event to perform some processing when a record is deleted from the Donations table. When a record is deleted, the amount of the donation is subracted form the DonationsReceived field in the DonationsReceived table and the TotalDonatedField in the Donors table.
Click here to view a copy of the macro that you can paste into Macro Designer.
To view this example in the macro designer, use the following steps.
Open the table for which you want to capture the After Delete event.
On the Table tab, in the After Events group, click After Delete.
Select the code listed below and then press CTRL+C to copy it to the Clipboard.
Activate the macro designer window and then press CTRL+V.
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/04/application">
<DataMacro Event="AfterDelete">
<Statements>
<Comment>Initialize a variable and assign the old</Comment>
<Action Name="SetLocalVar">
<Argument Name="Name">varAmount</Argument>
<Argument Name="Value">[Old].[Amount]</Argument>
</Action>
<ConditionalBlock>
<If>
<Condition>Not (IsNull([Old].[CampaignID]))</Condition>
<Statements>
<ForEachRecord>
<Data>
<Reference>Campaigns</Reference>
<WhereCondition>[ID]=[Old].[CampaignID]</WhereCondition>
</Data>
<Statements>
<EditRecord>
<Data />
<Statements>
<Action Collapsed="true" Name="SetField">
<Argument Name="Field">[DonationsReceived]</Argument>
<Argument Name="Value">[DonationsReceived]-[varAmount]</Argument>
</Action>
</Statements>
</EditRecord>
</Statements>
</ForEachRecord>
</Statements>
</If>
</ConditionalBlock>
<ConditionalBlock>
<If>
<Condition>Not (IsNull([Old].[DonorID]))</Condition>
<Statements>
<ForEachRecord>
<Data>
<Reference>Donors</Reference>
<WhereCondition>[ID]=[Old].[DonorID]</WhereCondition>
</Data>
<Statements>
<EditRecord>
<Data />
<Statements>
<Action Name="SetField">
<Argument Name="Field">[TotalDonated]</Argument>
<Argument Name="Value">[TotalDonated]-[varAmount]</Argument>
</Action>
</Statements>
</EditRecord>
</Statements>
</ForEachRecord>
</Statements>
</If>
</ConditionalBlock>
</Statements>
</DataMacro>
</DataMacros>
SetLocalVar
Name varAmount
Expression =[Old].[Amount]
If Not(IsNull([Old].[CampaignID]])) Then
For Each Record In Campaigns
Where Condition =[ID]=[Old].[CampaignID]
Alias
EditRecord
Alias
SetField ([DonationsReceived], [DonationsReceived] - [varAmount])
End EditRecord
End If
If Not(IsNull([Old].[DonorID]])) Then
For Each Record In Donors
Where Condition =[ID]=[Old].[DonorID]
Alias
EditRecord
Alias
SetField
Name [TotalDonated]
Value =[TotalDonated]-[varAmount]
End EditRecord
End If