Topic: loop in Table

Dear ones, I have been working on the program for a short while, and today I needed help


I am working on a project right now and want to create a button that moves through a table from the first row to the last row, where I want to perform certain operations. My question is, how can this rotation be done? I have set up a small project and added a column called 'to add'. For example, I would like to include it in the operation of rotating through the rows... Anyone who would like to do this practically, I would be very grateful.

Post's attachments

Attachment icon TestApp5.zip 335.3 kb, 25 downloads since 2024-04-10 

Re: loop in Table

You need to formulate the problem more precisely, for example:
I need a button to cycle through data in a table, so that the desired value in a specified field moves through the records (rows on the screen) from the beginning to the end, and then back to the beginning.
If this is what you need, then let me clarify: should the result of the “movement” be saved in the database or will it be relevant only during a program session? That is, you need to “move” the data in a table view on the screen or in a database table?

Визуальное программирование: блог и телеграм-канал.

3 (edited by bonalona59 2024-04-10 10:03:39)

Re: loop in Table

k245 wrote:

You need to formulate the problem more precisely, for example:
I need a button to cycle through data in a table, so that the desired value in a specified field moves through the records (rows on the screen) from the beginning to the end, and then back to the beginning.
If this is what you need, then let me clarify: should the result of the “movement” be saved in the database or will it be relevant only during a program session? That is, you need to “move” the data in a table view on the screen or in a database table?


I want to perform operations on cells in the table while navigating through each row of the table, not just moving on the tablegrid (Yes it will be saved in the table)...

4 (edited by k245 2024-04-11 13:59:08)

Re: loop in Table

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
  ID : String;
  NextID : String;
begin
  ID := SQLExecute('SELECT id FROM myData WHERE toAdd = 0') ;
  NextID := SQLExecute('SELECT id FROM myData WHERE id > '+ID+' ORDER BY id LIMIT 1' ) ;
  if NextID = '' then
    NextID := SQLExecute('SELECT id FROM myData ORDER BY id LIMIT 1' ) ;
  SQLExecute('UPDATE myData SET toAdd = NULL where id = '+ID ) ;
  SQLExecute('UPDATE myData SET toAdd = 0 where id = '+NextID ) ;
  Form1.TableGrid1.dbUpdate;
end;

The procedure moves the data to the toAdd field, using the ID field as the serial number

Post's attachments

Attachment icon TestApp5_2.rar 1.02 mb, 35 downloads since 2024-04-11 

Визуальное программирование: блог и телеграм-канал.

5 (edited by lara.0080 2024-04-12 18:29:38)

Re: loop in Table

question ... If have a set of columns on which I need to perform operations, then place the result for each row into a specific column in the table. This means that I want to perform the operation for all rows with a single click. In the method you provided, each click moves me from one row to another, but this isn't exactly that... Do you have a solution???

Re: loop in Table

Hi Lara, Konstantin,
If I understand the question correctly (an identical calculation for all rows in a table with the result added to a "result" column), perhaps this will do what you want (attached is a simple example).
Derek.

Post's attachments

Attachment icon bulk calculation.zip 443.53 kb, 38 downloads since 2024-04-12 

Re: loop in Table

Thank you very much, Derek. smile I have another question: If I want to write some code to be executed, is there a kind of loop that can walk through all rows? I want to take values from other tables, make some calculations, and perform checks.


derek wrote:

Hi Lara, Konstantin,
If I understand the question correctly (an identical calculation for all rows in a table with the result added to a "result" column), perhaps this will do what you want (attached is a simple example).
Derek.

Re: loop in Table

Hi,
The conventional way is to load all the data into a tablegrid and then loop through each row and perform whatever calculations / checks you need.
Have a look at the attachment which gets a 'discount rate' from a look-up table and calculates a 'sale price' for each item in the 'items' table.
You can then either 'clear' the 'sale price' or change the 'discount rates' and then recalculate the 'sale prices' etc etc.
Obviously there are other checks and controls you would want to put in place but this is just to show you the basics.
Derek.

Post's attachments

Attachment icon cycle through.zip 443.67 kb, 35 downloads since 2024-04-13 

Re: loop in Table

Dear Derek,

That's exactly what I needed. Many thanks!

derek wrote:

Hi,
The conventional way is to load all the data into a tablegrid and then loop through each row and perform whatever calculations / checks you need.
Have a look at the attachment which gets a 'discount rate' from a look-up table and calculates a 'sale price' for each item in the 'items' table.
You can then either 'clear' the 'sale price' or change the 'discount rates' and then recalculate the 'sale prices' etc etc.
Obviously there are other checks and controls you would want to put in place but this is just to show you the basics.
Derek.