Recent

Author Topic: rolling back all changes on a modal form  (Read 6340 times)

codifies

  • Jr. Member
  • **
  • Posts: 98
    • bedroomcoders
rolling back all changes on a modal form
« on: August 14, 2021, 09:22:13 am »
at the moment whenever I leave a component in a model "edit record" form, as soon as I move from one component to another it seems to be saving the changes immediately (I can even see the grid used for choosing the record changing in the main form)

rolling back the transaction seems to have no effect (when clicking a cancel button I added to close the form)

I want to be able to make a bunch of changes and then when the cancel button is clicked roll everything back to how it was when the form opened

Lazarus 2.3.0 r3945f73 FPC 3.2.0 x86_64-linux-gtk2

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #1 on: August 14, 2021, 10:07:32 am »
Take a look at this code:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijd_Informatie.Wedstrijd_Annuleren;
  2. begin
  3.   Close;
  4. end;     // Wedstrijd_Annuleren
  5.  
  6. procedure TForm_Wedstrijd_Informatie.Wedstrijd_Bijwerken;
  7. begin
  8.   if CheckInput = False then Exit;
  9.   Wedstrijd_BijwerkenDB;
  10. end;     // Wedstrijd_Bijwerken
  11.  
  12. procedure TForm_Wedstrijd_Informatie.Wedstrijd_BijwerkenDB;
  13. begin
  14.   //Sla het op in de tabel
  15.   if TQ_Wedstrijd.State in [dsEdit, dsInsert] then
  16.   begin
  17.     TQ_Wedstrijd.Post;
  18.     TQ_Wedstrijd.ApplyUpdates;
  19.     if Form_Lint.Trans_RefereeDB.Active then
  20.       Form_Lint.Trans_RefereeDB.CommitRetaining;
  21.   end;
  22. end;     // Wedstrijd_BijwerkenDB      
TQ_Wedstrijd is the query i use for the form to open.
And then I check the state of the query if it's in edit or insert (a new record). If so I am writing the changes.
This procedure I use when I am pressing the [OK] (Wedstrijd_Bijwerken).
If I press [Cancel] I just close the form (Wedstrijd_Annuleren).


So I have 2 different methods for closing the form.


Hope it brings you something.
« Last Edit: August 14, 2021, 10:09:32 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

codifies

  • Jr. Member
  • **
  • Posts: 98
    • bedroomcoders
Re: rolling back all changes on a modal form
« Reply #2 on: August 14, 2021, 11:16:33 am »
so are you using a separate query???

isn't it still going to update the record as soon as a component looses focus ?
Lazarus 2.3.0 r3945f73 FPC 3.2.0 x86_64-linux-gtk2

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #3 on: August 14, 2021, 09:23:26 pm »
My forms are all dependent on at least one query (in my case TSQLQuery) and a TDataSource from which the TDataSource.DataSet refers to my Query.


All fields that are in that form are TDB.... fields AKA if it's an edit field it's reference is TDBEdit.
The TDBEdit.DataField refers to a field in your query
The TDBEdit.Datasource refers to your TDataSource.


And then you can use what I wrote before
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

codifies

  • Jr. Member
  • **
  • Posts: 98
    • bedroomcoders
Re: rolling back all changes on a modal form
« Reply #4 on: August 15, 2021, 04:12:32 pm »
try as I might I can't get the modal form to rollback, I've tried allsorts, even using zeos db components, but as soon as a component looses focus, it looks as its changing the dataset immediately and the rollback is a noop

I might even try a separate transaction / query for the modal form, or I might just end up filling in values from the current record when the modal form first shows, and use a separate query to post the data.... 

I don't remember any issues doing this with Delphi, but mind that was nearly two decades ago so I might be misremembering...!
Lazarus 2.3.0 r3945f73 FPC 3.2.0 x86_64-linux-gtk2

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #5 on: August 16, 2021, 09:55:41 am »
can you post an example of your program?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

codifies

  • Jr. Member
  • **
  • Posts: 98
    • bedroomcoders
Re: rolling back all changes on a modal form
« Reply #6 on: August 16, 2021, 04:14:27 pm »
I'm using a datamodule for the data components (connection transaction etc)

There is a none editable TDBGrid where a user can select items on one particular form's tab, and do various operations on tables containing foreign keys of the primary key in grids records.

When the user chooses to edit an item in the grid...

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.EditStaffClick(Sender: TObject);
  2. begin
  3.   DataModule1.SQLTransaction1.CommitRetaining;
  4.   StaffEditForm:=TStaffEditForm.Create(nil);
  5.   StaffEditForm.ShowModal;
  6.   FreeAndNil(StaffEditForm);
  7. end;    
There (will be a number of checks (to do with business rules)) when the user wants to post the changes
Code: Pascal  [Select][+][-]
  1. procedure TStaffEditForm.SaveButtonClick(Sender: TObject);
  2. begin
  3.   CheckForm();
  4.   DataModule1.SQLTransaction1.CommitRetaining;
  5.   Close;
  6. end;
  7.  
  8. procedure TStaffEditForm.CancelButtonClick(Sender: TObject);
  9. begin
  10.   DataModule1.SQLTransaction1.RollbackRetaining;
  11.   Close;
  12. end;  
At the moment no matter what I do the edits never rollback, and as soon as one field on the modal edit form looses focus you can even see it change immediately in the main forms gird...
I'm sure years ago I did exactly what I'm trying to achieve with Delphi...
Lazarus 2.3.0 r3945f73 FPC 3.2.0 x86_64-linux-gtk2

codifies

  • Jr. Member
  • **
  • Posts: 98
    • bedroomcoders
Re: rolling back all changes on a modal form
« Reply #7 on: August 16, 2021, 05:18:04 pm »
well... by abandoning transactions I now seem to have the behaviour I want, but it seems to me that there is no way to do what I wanted using the transaction component...?

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.EditStaffClick(Sender: TObject);
  2. begin
  3.   StaffEditForm:=TStaffEditForm.Create(nil);
  4.   StaffEditForm.ShowModal;
  5.   FreeAndNil(StaffEditForm);
  6. end;  
in the modal form..
Code: Pascal  [Select][+][-]
  1. procedure TStaffEditForm.SaveButtonClick(Sender: TObject);
  2. begin
  3.   DataModule1.StaffQuery.ApplyUpdates();
  4.   Close;
  5. end;
  6.  
  7. procedure TStaffEditForm.CancelButtonClick(Sender: TObject);
  8. begin
  9.   DataModule1.StaffQuery.CancelUpdates;
  10.   Close;
  11. end;
  12.  

basically I can edit the fields in the modal form as much as I like and despite the main form showing the changes, if I click cancel the original values are restored. If I click save the changes are retained.

Is it possible to use transaction save points with the transaction component ?
Lazarus 2.3.0 r3945f73 FPC 3.2.0 x86_64-linux-gtk2

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #8 on: August 19, 2021, 05:19:48 pm »
Added a small example of one of my programs..
Take a look

You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #9 on: August 20, 2021, 11:27:56 pm »
Are you getting it to work codifies
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

madref

  • Hero Member
  • *****
  • Posts: 864
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: rolling back all changes on a modal form
« Reply #10 on: August 24, 2021, 12:11:48 pm »
Added a small example of one of my programs..
Take a look


over a 100 downloads and no feedback???
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus Trunc.git [2.3.0 Rev. 65550] / FPC Trunc.git [3.3.1]
Mac OS X Big Sur

 

TinyPortal © 2005-2018