1 (edited by papafrankc 2021-09-01 03:13:36)

Topic: Fields being erased

Hi All,
During my testing I've run across something strange:
Here's what I have:
- Form A with fields and a button to take me to Form B
- Form B has fields and a SAVE button.
.
I enter stuff on Form A, then click the button to go to Form B.
I enter data into Form B, then hit SAVE
When I go back to Form A, my new field entries are erased.
.
What's strange is that I have another application where this does not happen.  I've been comparing the 2 applications and can't find anything different.
.
I'm assuming it's something I've done or not done?? My feeling is that it's something basic but I think I'm having a senior moment because I can't find it.
.
UPDATE I've found the problem but am not sure how to fix it.
Recently Brian was kind enough to help me with the following code to refresh a field on an open form:
procedure frmService_btnSave_OnAfterClick (Sender: TObject);

var  TableID :integer;
begin
    TableID := frmEquip.btnSave.dbGeneralTableId; // GET THE FORM ID

    frmEquip.ShowRecord('tbl_Equip', TableID);  // UPDATE THE FORM
end;

.
This code does what I needed but apparently the frmEquip.ShowRecord('tbl_Equip', TableID);  // UPDATE THE FORM is what is causing me to lose the entries on Form A that have not been saved.
.
Thanks, Frank

Re: Fields being erased

You can click the save button via script of FormA after or before opening FormB for it to save your unsaved data.

But if you are using NewRecord Method when opening FormB, this will automatically click the save button of FormA

If you have an after-save message on FormA save button, then you can trap it using the FormA.Tag condition method.

brian

Re: Fields being erased

Brian,
I was able to Save the data on Form A (Equip) when I open Form B (Service)  with:
frmEquip.btnSave.Click ;
but it closes the Equip form, so when I close the Service form it takes me back to the form before Equip.
.
Is there any way to save the data on the Equip form and not close it?
.
Thanks, Frank

Re: Fields being erased

Yes there is. Just Uncheck the "Close the current form after saving". Then to make sure that it won't add a record(which happens to me sometimes), set dbDontResetID of the frmEquip.btnSave to True from the onClick of the save button

procedure frmEquip_btnSave_OnClick (Sender: TObject; var Cancel: boolean);
begin
    frmEquip.btnSave.dbDontResetID := True;
end;
Post's attachments

Attachment icon xxx.png 19.84 kb, 82 downloads since 2021-09-01 

brian

Re: Fields being erased

Brian,
Your fix does prevent the form from closing however now my SAVE button on the Equip form doesn't work.  It does appear to Save the info on the form but now it doesn't close the form.  The only way I can close the form is with the X on the top right of the form.
.
Sorry to be such a pain in the _ _ _.
Frank

6 (edited by brian.zaballa 2021-09-02 00:18:22)

Re: Fields being erased

papafrankc wrote:

Brian,
Your fix does prevent the form from closing however now my SAVE button on the Equip form doesn't work.  It does appear to Save the info on the form but now it doesn't close the form.  The only way I can close the form is with the X on the top right of the form.
.
Sorry to be such a pain in the _ _ _.
Frank

Do the Tag Method and it will solve your problem

in the button (showing the frmService,

procedure frmEquip_[btnshowingfrmservice]_OnClick (Sender: TObject; var Cancel: boolean);
begin
    // if you are not using this button
    frmEquip.btnSave.Tag := 1; //
    // but if you already using btnSave.Tag somewhere else, you can use [btnshowingfrmservice]
    // frmEquip.[btnshowingfrmservice].Tag := 1; 
end;

When this tag is not 1, then close the form OnAfterClick of frmEquip.btnSave

procedure frmEquip_btnSave_OnAfterClick (Sender: TObject; var Cancel: boolean);
begin
    if frmEquip.btnSave.Tag <> 1 then
    // or if you use option 2,
    // if frmEquip.[btnshowingfrmservice].Tag <> 1 then
        frmEquip.Close;
end;

Take Note: Make sure you set the button tag to, 0, after opening frmService, of after saving

procedure frmService_btnSave_OnAfterClick (Sender: TObject);

var  TableID :integer;
begin
    TableID := frmEquip.btnSave.dbGeneralTableId; // GET THE FORM ID

    frmEquip.ShowRecord('tbl_Equip', TableID);  // UPDATE THE FORM

    // Make sure to set it not 1, 
    frmEquip.btnSave.Tag := 0; 
    // but if you already using btnSave.Tag somewhere else, you can use [btnshowingfrmservice]
    // frmEquip.[btnshowingfrmservice].Tag := 0; 
end;
brian

Re: Fields being erased

Brian,
You're my hero! I've implemented your latest code and it looks like everything is working as it should.  I'll do some more testing but so far I don't see any problems smile
.
I've learned about a number of new things (for me)...tags etc.  Now I'll go through the new code and make sure I understand what's going on.
.
Using dates as I'm doing is something new for me, and as you've seen from my posts, pretty challenging.  But I'm making progress & that's what really matters.
.
Thanks, Frank