Topic: Transfer field from one grid to another form to save in another table

I do have another complication and I am scratching my head to see which solution would save me to write tons of scripts to do this.

The attached project is a very very simple version of what I am working on, I need to get some fields from a form opened via a "show record" to be transferred to a new form and then saved into a different table. I know I can do it via scripts but I want to know if there would be a simple solution that I don't see yet. There is a screenshot in the zip file for the details.

I don't mind using tabs but that would prove pretty difficult to implement in my current big project.

Post's attachments

Attachment icon TransferData_to_other_form.zip 670.34 kb, 137 downloads since 2022-08-11 

Re: Transfer field from one grid to another form to save in another table

Hi,
Whenever I look at a database schema and see a table that 'stands alone' (in your example, the 'maintenance' table), I wonder about the structure.
As there is a 'maintdate' in your 'maintenance' table, I would assume that an individual computer is going to have maintenance records for multiple dates, in which case is this not a typical 'one-to-may' relationship and should have a link between the 'computers' and 'maintenance' tables? 
And if there were a relationship, wouldn't it solve your problem without having to write any script?
Derek.

Re: Transfer field from one grid to another form to save in another table

That is a correct statement, I will go back to design. Thanks Derek.

Re: Transfer field from one grid to another form to save in another table

But I don't get how to carry infos from one form to another when clicking on a button...

Re: Transfer field from one grid to another form to save in another table

Hi Derek, tcoton.

Derek is right about the separate table.


It's easy to add.
On pressing the add button, transfer the values of the required fields to the
fields on the new form. Nothing is difficult.
Just change the button property from new records to show the form.

6 (edited by tcoton 2022-08-11 22:04:14)

Re: Transfer field from one grid to another form to save in another table

Yeah... except that the button show records does not allow for a lot of liberty, you must select whether a grid or a combobox which none of them are present in form where the ADD is called from, hence my request.

Anyway, I found a way of doing it with a single line of script instead of multiple lines which is not too bad. I wish the GUI "Show Record" button had more flexibility.

The Add button must not have any action but must have an On Click event and the script is as follow:

procedure Admin_addMaint_OnClick (Sender: TObject; var Cancel: boolean);
begin
    ShowRecord(Maintenance,'Computers',Form1.TableGrid1.dbItemID);
end;

Syntax is ShowRecord(Target form name,'Source table name',Source form and object where dbItemID is acquired from);

AND the fields where you record the data in the target table must match the name of the original FieldNames otherwise you get an error about column names!!


If someone has a better way, please share!!

Post's attachments

Attachment icon TransferData_to_other_form-FIXED.zip 679.93 kb, 122 downloads since 2022-08-11 

Re: Transfer field from one grid to another form to save in another table

Hi,
If you create a relationship between 'computers' and 'maintenance' there is no need to copy the pcname, serialnb etc etc because those details are already held in the related table.
Personally, all I'd have in the 'maintenance' table would be the maintdate and the maintdetails - everything else is derived from the relationship. 
FYI, the relationship should be held on the 'maintenance' table (id_computers) not on the 'computers' table.
Have a look at the attachment as one way to do it (there are others) but this way requires no script at all (apart from an 'update' statement for the calculated field but it's optional).
Derek.

Post's attachments

Attachment icon TransferData_to_other_form 3.zip 340.87 kb, 139 downloads since 2022-08-12 

Re: Transfer field from one grid to another form to save in another table

Thanks Derek, I have been away for too long from MVDB,  I forgot how to work with it :rolleyes

The calculated fields are a very good addition.

I just wonder why and how is the footer displayed at the bottom of Form1.TableGrid1 in your project as I don't spot any script or property for that and if there is a way of managing it like adding some wording changing the font etc...

Re: Transfer field from one grid to another form to save in another table

Hi,
Footer calculations are handled within the tablegrid layout - click on the 'cog' to the right of any column that you've selected and you'll see all the options available.  Text can also be added either before or after the calculation itself (see the screenshot in the attachment and the slightly amended attachment to the previous version).
I wasn't sure if you're still needing to see the PC name, Serial No' etc etc (even though it's being pulled across from the related 'computers' table rather than held in the 'maintenance' table).  One option to do it (but without cluttering up the body of the form) is to use the form caption.  It's a couple of more lines of script but it makes it a bit more user friendly.
Derek.

Post's attachments

Attachment icon TransferData_to_other_form 4.zip 405.4 kb, 207 downloads since 2022-08-12 

Re: Transfer field from one grid to another form to save in another table

Thanks for the explanation and whoooo that's a nice script to modify the caption of... almost anything!!
It is best for the final user to know what he/she is actually working on.

I guess your snippet manager might be quite consequent given all the tips you know!      wink

Re: Transfer field from one grid to another form to save in another table

Hi,

I struggled a little bit to include this in my project which has a lot of script already and I got some results.
I had to put the code in the OnShow section of the Admin form, otherwise it did not work at all or extremely randomly.

Now, I would like the caption to be more dynamic just to change it if the source of the SQL id comes from another table grid on form1 where I display only the computers having the status "maintenance".

Is this even possible? I tried to use another query with a different variable name but only the last caption works after that.

Re: Transfer field from one grid to another form to save in another table

Never mind, I found an easy way of doing it by using the text present in a textbox that is already present when calling the window instead of the SQL query which was a bit cumbersome. It works perfectly whether I call the Admin form from any other grid since it is just a visual information.

The SQL Query works fine only if data are retrieved from only one table grid.

vcaption := Admin.EditPCName.Text +' - S/N: '+ Admin.SerialNo.Text;

Thanks a lot again, Derek, for your knowledge!!