Cancelling Record Changes
Pergunta
I am editing a record using a dbedit component, I have a cancel button but I'm not sure on how I can make it so all of the changes made using the dbedit components are reverted.
I was thinking about copying the record either to a temp table or duplicate the record within the same table which would let me remove the old record if the changes are saved or delete the copied record (leaving the original) if the input is cancelled.
I'm just wanting to know the best way to handle this without creating useless tables, creating too many procedures.
Solução
If I'm not mistaken changes to a paradox table only get written to the database after a post
command.
If you want to cancel the change, just do
TForm1.CancelButtonPresss(Sender: TObject);
begin
ParadoxTable.Cancel;
end;
TForm1.OKButtonPress(Sender: TObject);
begin
ParadoxTable.Post;
end;
BTW, its been a long long time since I've worked with paradox tables, so my recollection my be incorrect, please feel free to vote down this answer if I'm mistaken.
I'm typing this on the mac, so I cannot check it now.
Will see if I can supply you with a more informed answer later.
Outras dicas
To Compliment Johan's answer (use TDataSet.Cancel
), if you use a TCustomClientDataSet
, you also can use the RevertRecord
method to remove the modifications to the current record, provided they are still in the change log.
You can also set a snapshot with SavePoint
and revert to that state, cancelling all modifications done in the meantime.
Johan's answer is good for single record. If you are working with a SQL database (Oracle, MSSql, MySql, Firebird, etc) there is an additional approach that can be used for multiple records: transactions. Using ADO as an example
TForm1 = class(TForm)
ADOConnection: TADOConnection;
…
// start the transaction
ADOConnection.BeginTrans;
…
// create records and post them
…
// rollback removes the records posted
// since the transaction was started
ADOConnection.RollbackTrans;
… or …
// commit completes saving the records posted
// since the transaction was started
ADOConnection.CommitTrans;
If you do not explicitly start a transaction, one is automatically started and committed as records are posted to the database.
François's answer is similar to transactions, but only works with ClientDatasets.