问题
Hi I would like to automatically update the value of an attribute in a field in Access 2013 if another attribute in that field is edited.
In the database backend I have selected the table I want to work on, and clicked on the AfterUpdate tab in the ribbon I enter these steps and save it, but nothing seems to happen when I update the DATE_OF_DEATH field and there is no error message generated, so I can't even debug the thing, I don't know whether the Macro is even been triggered or not, or if the IF statement is failing or if the SetField statement is failing, I'm totally in the dark. I would really appreciate some advice on fixing this. Thanks.
The code is below.
If Updated([DATE_OF_DEATH])
EditRecord
SetField
Name OUTCOME
VALUE = "test"
End EditRecord
End If
回答1:
The just inserted or updated 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 =[ID]
If Updated([DATE_OF_DEATH])
For Each Record In MyTable
Where Condition =[ID]=[NewID]
EditRecord
SetField
Name OUTCOME
Value ="test"
End EditRecord
End If
This will require your records to have a unique identifier. Without a unique identifier, I think you can't achieve this. You can try using a Before Change macro to edit the currently updated record and use IsInsert
to only affect updates, but unfortunately these don't have access to the Updated
function.
来源:https://stackoverflow.com/questions/49853116/cant-get-afterupdate-macro-to-work