0

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.

2
  • This question needs more info. What is the relation between kbmMemtable1 and 'attached' kbmMemtable2? Show your OnCalcField handlers. Commented Jun 23, 2023 at 8:26
  • "Show" as in: edit your question to include more details and most likely code. And while you're at it, fix typos and think about adding tags. Also take the tour. Commented Jun 23, 2023 at 16:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.