Recent

Author Topic: how to sum the value of a column and display it in a memobox  (Read 2811 times)

iamtheatorres2408

  • New Member
  • *
  • Posts: 23
how to sum the value of a column and display it in a memobox
« on: December 06, 2018, 01:32:19 am »
Good day, so im trying to make a trial project using dbgrid and i wan to sum up the value of  "total mistake" for every encoder when  I put the keyword on the searchbox which is the encoder's name, and i want the the total value to be displayed in the memobox above.What codes can i use to do this?? hoping you can help me with this matter...thank you in advance.. :D :D :D O:-) O:-)

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: how to sum the value of a column and display it in a memobox
« Reply #1 on: December 06, 2018, 01:57:01 am »
Depends on the underlying data source for the grid.

For TDbf you can use the filter property to select the records shown in the grid. The record count is problematic though.

For a sql-based dataset, you can use a paramterized query with a "WHERE" clause to select rows that match. The record count would be correct for it too.
« Last Edit: December 06, 2018, 02:05:01 am by dsiders »
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: how to sum the value of a column and display it in a memobox
« Reply #2 on: December 06, 2018, 02:28:10 am »
TDBGrid is merely a visual representation; instead of focusing on the grid itself you should rather be concentrating in its underlying datasource, which is where the real data exists. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

balazsszekely

  • Guest
Re: how to sum the value of a column and display it in a memobox
« Reply #3 on: December 06, 2018, 06:47:29 am »
Good day, so im trying to make a trial project using dbgrid and i wan to sum up the value of  "total mistake" for every encoder when  I put the keyword on the searchbox which is the encoder's name, and i want the the total value to be displayed in the memobox above.What codes can i use to do this?? hoping you can help me with this matter...thank you in advance.. :D :D :D O:-) O:-)

Here you go:
Code: Pascal  [Select][+][-]
  1. uses DBGrids, sqldb, db;
  2.  
  3.  
  4. function SumUpMistakes(const ADataSet: TDataSet; const ACommitedBy: String): Integer;
  5. begin
  6.   Result := 0;
  7.   if not ADataSet.Active then
  8.     Exit;
  9.   ADataSet.DisableControls;
  10.   try
  11.    ADataSet.First;
  12.    while not ADataSet.Eof do
  13.    begin
  14.      if UpperCase(ADataSet.FieldByName('COMMITEDBY').AsString) = UpperCase(ACommitedBy) then
  15.        Result := Result + ADataSet.FieldByName('TOTALMISTAKE').AsInteger;
  16.      ADataSet.Next;
  17.    end;
  18.   finally
  19.     ADataSet.EnableControls;
  20.   end;
  21. end;
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. var
  25.   TotMistakes: Integer;
  26. begin
  27.   TotMistakes := SumUpMistakes(DBGrid1.DataSource.DataSet, Edit1.Text);
  28.   ShowMessage(IntToStr(TotMistakes));
  29. end;


PS: Don't forget to change fields "COMMITEDBY" and "TOTALMISTAKE" to match the ones in your database. Also change DBGrid1 and Edit1 according to your needs.

iamtheatorres2408

  • New Member
  • *
  • Posts: 23
Re: how to sum the value of a column and display it in a memobox
« Reply #4 on: December 07, 2018, 06:11:55 am »
Thank you very much for the codes , but is it possible to eliminate the button and show the result directly at the memobox after i input the encoder's name in the searchbox? thanks again :) :) :)


Code: Pascal  [Select][+][-]
  1. uses DBGrids, sqldb, db;
  2.  
  3.  
  4. function SumUpMistakes(const ADataSet: TDataSet; const ACommitedBy: String): Integer;
  5. begin
  6.   Result := 0;
  7.   if not ADataSet.Active then
  8.     Exit;
  9.   ADataSet.DisableControls;
  10.   try
  11.    ADataSet.First;
  12.    while not ADataSet.Eof do
  13.    begin
  14.      if UpperCase(ADataSet.FieldByName('COMMITEDBY').AsString) = UpperCase(ACommitedBy) then
  15.        Result := Result + ADataSet.FieldByName('TOTALMISTAKE').AsInteger;
  16.      ADataSet.Next;
  17.    end;
  18.   finally
  19.     ADataSet.EnableControls;
  20.   end;
  21. end;
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. var
  25.   TotMistakes: Integer;
  26. begin
  27.   TotMistakes := SumUpMistakes(DBGrid1.DataSource.DataSet, Edit1.Text);
  28.   ShowMessage(IntToStr(TotMistakes));
  29. end;

balazsszekely

  • Guest
Re: how to sum the value of a column and display it in a memobox
« Reply #5 on: December 07, 2018, 06:26:55 am »
Thank you very much for the codes , but is it possible to eliminate the button and show the result directly at the memobox after i input the encoder's name in the searchbox? thanks again :) :) :)
TEdit has an OnKeyPress event:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  2. var
  3.   TotMistakes: Integer;
  4. begin
  5.   if Key = #13 then
  6.   begin
  7.     TotMistakes := SumUpMistakes(DBGrid1.DataSource.DataSet, Edit1.Text);
  8.     Edit1.Text := IntToStr(TotMistakes);
  9.   end;
  10. end;

After you input the encoder's name, just press enter to see the result.


 

TinyPortal © 2005-2018