Board index » delphi » calculated field problems in a DBGrid

calculated field problems in a DBGrid

Quote
Adrian Linggi wrote in message

<71tg3n$20...@newsreader1.core.theplanet.net>...

Quote
>I am using a calculated field to work out a subtotal by multipying the
price
>field and weight field in a paradox table.  The code is :-

>procedure TCreateSB.Table1CalcFields(DataSet: TDataSet);
>begin
>    table1subtotal.value := table1price.value * table1weight.value
>end;

>This part works fine, but now Im trying to work out the total of all the
>records in the table.  The procedure is :-

>procedure TCreateSB.totals;
>var temptotal : extended;
>      tempweight : extended;
>begin
>  Table1.DisableControls;
>  temptotal := 0;
>  tempweight:=0;
>  table1.first;
>  while not table1.eof do
>  begin
>    temptotal := temptotal + table1subtotal.value;
>    tempweight := tempweight + table1weight.value;
>    table1.next;
>  end;
>table1.edit;                            STARTING HERE
>table1totalweight.value :=tempweight;
>table1total.value := temptotal;
>Table1.EnableControls;
>end;

Get the totals out of the table. Place them in the private section of the
form. Display them using a label, since the user should not be able to
update totals anyway.

After you loop thru the table, simply place your totals in the
label.caption. (label.caption := tempweight; formatted any way you want).

Hope this helps,

Robert

 

Re:calculated field problems in a DBGrid


I am using a calculated field to work out a subtotal by multipying the price
field and weight field in a paradox table.  The code is :-

procedure TCreateSB.Table1CalcFields(DataSet: TDataSet);
begin
    table1subtotal.value := table1price.value * table1weight.value
end;

This part works fine, but now Im trying to work out the total of all the
records in the table.  The procedure is :-

procedure TCreateSB.totals;
var temptotal : extended;
      tempweight : extended;
begin
  Table1.DisableControls;
  temptotal := 0;
  tempweight:=0;
  table1.first;
  while not table1.eof do
  begin
    temptotal := temptotal + table1subtotal.value;
    tempweight := tempweight + table1weight.value;
    table1.next;
  end;
table1.edit;
table1totalweight.value :=tempweight;
table1total.value := temptotal;
Table1.EnableControls;
end;

the result of this is shown in a dbedit control on the form.  This works
fine, but when I click on the DBGrid, the total disappears until I add a new
record.  Then it returns, and the same thing happens again.....(it
dissapears)

Can anyone help, as Im really stuck and dont know what the problem could be.

Thanks

Adrian

Other Threads