Access 2016 Table Macros After Insert modify fields in newly created record

十年热恋 提交于 2020-01-06 04:53:10

问题


Using a custom form, fields are updated on a newly created record by an operator. When a new record is created there are fields within that newly created record that are to be updated based on values in another field.

For example a field, CustID is updated based on the Primary Key, "PK" as follows: CustID="NW"&Format([PK],"000000").

I can perform this update using an update query however, I would like this update to happen automatically immediately following the creation of the new record.

I know I could do this writing VBA code but I prefer not to. I should be able to do this using an event driven Table Macro, After Insert. For some reason I cannot get this to work, now before anyone asks Macros are enabled in the Trust Centre. If I can get this to work, I have other fields that will be updated based on values entered by the operator.

I have set the table Macro as follows:

After Insert
Edit Record
Set Field CustID="NW"&Format([PK],"000000")

I feel this should work but nothing happens. I am making the bold assumption the Edit Record edits the newly created record, is this correct. I cannot find any information and/or examples as to how to successfully use After Insert to modify fields in the newly created record.

Question 1: can this be done?

And 2: If so, how?


回答1:


The just inserted record in an Access Data Macro is read-only (just like in SQL Server or other solutions with triggers). You should see an error indicating that in the table USysApplicationLog. You can, however, lookup the row you've just inserted, and modify it.

You can use the following macro code for that:

SetLocalVar
    Name            NewID
    Expression      =[PK]

For Each Record In  MyTable
Where Condition     =[PK]=[NewID]
    EditRecord
        SetField
            Name    CustID
            Value   ="NW"&Format([PK],"000000")
    End EditRecord

The macro XML will look like this (you can paste the XML in the macro window to create the macro):

<?xml version="1.0" encoding="UTF-8"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application">
   <DataMacro Event="AfterInsert">
      <Statements>
         <Action Name="SetLocalVar">
            <Argument Name="Name">NewID</Argument>
            <Argument Name="Value">[PK]</Argument>
         </Action>
         <ForEachRecord>
            <Data>
               <Reference>MyTable</Reference>
               <WhereCondition>[PK]=[NewID]</WhereCondition>
            </Data>
            <Statements>
               <EditRecord>
                  <Data />
                  <Statements>
                     <Action Name="SetField">
                        <Argument Name="Field">CustID</Argument>
                        <Argument Name="Value">"NW" &amp; Format([PK],"000000")</Argument>
                     </Action>
                  </Statements>
               </EditRecord>
            </Statements>
         </ForEachRecord>
      </Statements>
   </DataMacro>
</DataMacros>


来源:https://stackoverflow.com/questions/49482858/access-2016-table-macros-after-insert-modify-fields-in-newly-created-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!