After Insert macro event

Applies to: Access 2013, Office 2013

The After Insert event occurs after a record is added.

Note

The After Insert event is available only in Data Macros.

Remarks

Use the After Insert event to perform any actions that you want to occur when a record is added to a table. Common uses for the After Insert include enforcing business rules, workflows, updating an aggregate total, and sending notifications.

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 
 

The following table lists macro commands that can be used in theAfter Insert event.

Command Type

Command

Program Flow

Comment macro statement

Program Flow

Group macro statement

Program Flow

If...Then...Else macro block

Data Block

CreateRecord macro action

Data Block

EditRecord macro action

Data Block

ForEachRecord macro action

Data Block

LookupRecord data block

Data Action

CancelRecordChange macro action

Data Action

ClearMacroError macro action

Data Action

DeleteRecord macro action

Data Action

ExitForEachRecord macro action

Data Action

LogEvent macro action

Data Action

OnError macro action

Data Action

RaiseError macro action

Data Action

RunDataMacro macro action

Data Action

SendEmail macro action

Data Action

SetField macro action

Data Action

SetLocalVar macro action

Data Action

StopAllMacros macro action

Data Action

StopMacro macro action

To create a Data macro that captures the After Insert event, use the following steps.

  1. Open the table for which you want to capture the After Insert event.

  2. On the Table tab, in the After Events group, click After Insert.

An empty data macro is displayed in the macro designer.

Example

The following code example uses the After Insert event to perform some processing when a record is added to the Donations table. When a record is added, the amount of the donation is added to the DonationsReceived field in the Campaigns 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:

  1. Open the table for which you want to capture the After Insert event.

  2. On the Table tab, in the After Events group, click After Insert.

  3. Select the code in the following code example and then press CTRL+C to copy it to the Clipboard.

  4. Activate the macro designer window and then press CTRL+V.

    <DataMacros> 
      <DataMacro Event="AfterInsert"> 
        <Statements> 
          <Comment>This data macro increments the DonationsReceived field in Campaigns and theAmountCollected field in Pledges </Comment> 
          <Action Name="SetLocalVar"> 
            <Argument Name="Name">varAmount</Argument> 
            <Argument Name="Value">[Amount]</Argument> 
          </Action> 
          <ConditionalBlock> 
            <If> 
              <Condition>Not (IsNull([CampaignID]))</Condition> 
              <Statements> 
                <ForEachRecord> 
                  <Data> 
                    <Reference>Campaigns</Reference> 
                    <WhereCondition>[ID]=[Donations].[CampaignID]</WhereCondition> 
                  </Data> 
                  <Statements> 
                    <EditRecord> 
                      <Data /> 
                      <Statements> 
                        <Action 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([DonorID]))</Condition> 
              <Statements> 
                <ForEachRecord> 
                  <Data> 
                    <Reference>Donors</Reference> 
                    <WhereCondition>[ID]=[Donations].[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   =[Amount] 
     
    If   Not (IsNull([CampaignID]))   Then 
     
         For Each Record In   Campaigns 
            Where Condition   =[ID]=[Donations].[CampaignID] 
                      Alias 
     
                 EditRecord 
                              Alias 
                     SetField 
                                 Name   [DonationsReceived] 
                                Value   =[DonationsReceived]+[varAmount] 
                End EditRecord 
    End If 
     
    If   Not (IsNull([DonorID]))   Then 
     
         For Each Record In  Donors 
            WhereCondition   =[ID]=[Donations].[DonorID] 
                     Alias 
     
                 EditRecord 
                              Alias 
                     SetField 
                                 Name   [TotalDonated] 
                                Value   =[TotalDonated]+[varAmount] 
                 End EditRecord 
    End If