>>Hi everybody,
>>
>>I want to automatically update a field in a table. If I add a record, I want to put 'A' in ModiType, if I change it, I want to put 'C' and if I delete it, I want to put "D". I tried to use triggers for this, but got can not update cursor message. How can I perform this update automatically?
>>
>>It's for VFP6.
>
>Nadya,
>
>A trigger cannot update the record that caused it to fire. It can update/delete/insert other records of the same table or any record in another table. (you can keep the status in another table though)
>
>The only possibility left is to change the record in the record validation rule.
>
>You'll have to do some testing
>
> reopen the table and seek
>if found()
> it's an update
>else
> it's new record
>endif
>
>another method to see wether it's an insert is to test for isnull(oldVal('fieldname')) provided that nulls are not allowed for that field
>
>
>Watch out for the fields that form the key. They can possibly change ( fieldname and oldval('fieldname'))
>
>Here's how I check whether a field has changed or not.
>
>The RecordValidation also fires when you replace a field with its same value. ( eg repl FieldName with FieldName ). You will have to check whether any field has changed
>
>
>
>
>
>function FieldChanged(FieldName)
>
> do case
> case isnull(oldval(FieldName)) or Isnull(eval(FieldName))
> return (isnull(oldval(FieldName)) <> Isnull(eval(FieldName)) )
> otherwise
> return (oldval(FieldName) <> eval(FieldName))
> endcase
>endfunc
>*-------------------------------------------------------------------------
>
>
>The Field validation does not fire when you delete a record. But is that necessary since you can check for deleted() ?
Hi Gregory,
Thanks a lot. This sounds like a good plan. I'll try it and let you know the result.
If it's not broken, fix it until it is.
My Blog