user975502
user975502

Reputation: 1

Problem with calculated fields in attached kbmMemTable

I have a delphi project which has two kbmMemtables, the first kbmMemtable1 has 4 data fields (ID, PRODUCT_NAME, QUANTITY, PRICE) and a calculated field TOTAL, the TOTAL field is calculated in the onCalcFields event and its calculating fine.

The problem is with the attached kbmMemTable2, which has the same structure of kbmMemtable1, when posting the data of kbmMemTable1 all the data fields of kbmMemTable2 are updated correctly but the TOTAL field is null and that is causing the SumField function to fail.

I am using Delphi Alexandria and kbmMemTable Pro.

KbmMemTables Properties:

kbmMemTable1:
AttachedAutoRefresh = True
AttachedTo = empty
AutoCalcFields = True

kbmMemTable2:
AttachedAutoRefresh = True
AttachedTo = kbmMemTable1
AutoCalcFields = True

procedure TForm1.FormCreate(Sender: TObject);
begin
  kbmMemTable1.Close;
  kbmMemTable1.Open;
  kbmMemTable2.Close;
  kbmMemTable2.Open;

  kbmMemTable1.Append;
end;

function TForm1.SumField(aDataSet: TKbmMemTable; aFieldName: string): variant;
begin
    Result := 0;
  aDataSet.DisableControls;
  aDataSet.First;
  with aDataSet do
    while not eof do
      begin
        Result := Result + FieldByName(aFieldName).Value;
        Next;
      end;
  aDataSet.EnableControls;
end;

procedure TForm1.kbmMemTable1AfterPost(DataSet: TDataSet);
begin
  lblTotal.Caption := FormatFloat('0.000', SumField(kbmMemTable2, 'TOTAL'));
end;

procedure TForm1.kbmMemTable1CalcFields(DataSet: TDataSet);
begin
  kbmMemTable1TOTAL.AsFloat := kbmMemTable1QUANTITY.AsInteger * kbmMemTable1PRICE.AsFloat;
end;

procedure TForm1.kbmMemTable1NewRecord(DataSet: TDataSet);
begin
  kbmMemTable1ID.AsInteger := kbmMemTable1.RecordCount + 1;
  kbmMemTable1PRODUCT_NAME.AsString := 'TEST PRODUCT';
  kbmMemTable1QUANTITY.AsInteger := 2;
  kbmMemTable1PRICE.AsFloat := 1.230;
end;

Please could someone help me to resolve this issue.

For anyone having this issue the solution is to disable the property RecalcOnFetch in the attached Memtable thanks for everyone's support.

Upvotes: 0

Views: 199

Answers (0)

Related Questions