Topic: FooterValue

please help me fix this procedure, to generate column 6 footer value from last ROW column 6 value

procedure FrmBukuBesar_TableGrid1_OnChange(Sender: TObject);
var
  i: Integer;
  Debet, Kredit, Hasil: double;
begin
  // Format dan hitung total untuk kolom Debet dan Kredit
  for i := 4 to 5 do
  begin
    if FrmBukuBesar.TableGrid1.Columns[i] is TNxNumberColumn then
    begin
      with TNxNumberColumn(FrmBukuBesar.TableGrid1.Columns[i]) do
      begin
        FormatMask := '###,##0';
        Footer.FormatMask := '###,##0';
        Footer.FormulaKind := fkSum;
        FrmBukuBesar.TableGrid1.CalculateFooter;
        end;
    end;
  end;

  // Hitung saldo akhir = Debet - Kredit
  //Debet := FrmBukuBesar.TableGrid1.Columns[4].Footer.FormulaValue;
  //Kredit := FrmBukuBesar.TableGrid1.Columns[5].Footer.FormulaValue;
  //Hasil  := Debet - kredit;
  //FrmBukuBesar.TableGrid1.Columns[6].Footer.Caption := FloatToStr(Hasil);

  with TNxNumberColumn(FrmBukuBesar.TableGrid1.Columns[6]) do
  begin
    FormatMask := '###,##0';
    Footer.FormatMask := '###,##0';
    Footer.FormulaKind := fkNone;
    Footer.FormulaValue := Debet - Kredit;
  end;

  // Kalkulasi ulang footer dan sembunyikan kolom ke-7
  FrmBukuBesar.Edit1.Value := Debet;

  FrmBukuBesar.TableGrid1.CalculateFooter;
  FrmBukuBesar.TableGrid1.Columns[7].Visible := False;
end;
Post's attachments

Attachment icon 1.JPG 70.71 kb, 2 downloads since 2025-06-16 

2 (edited by agusecc 2025-06-16 04:33:11)

Re: FooterValue

this is solution, and Done

procedure FrmBukuBesar_TableGrid1_OnChange(Sender: TObject);
var
  i: Integer;
  Debet, Kredit, Hasil: double;

  LastRow: Integer;
  ColumnValue: string;
begin
  for i := 4 to 5 do
  begin
    if FrmBukuBesar.TableGrid1.Columns[i] is TNxNumberColumn then
    begin
      with TNxNumberColumn(FrmBukuBesar.TableGrid1.Columns[i]) do
      begin
        FormatMask := '###,##0';
        Footer.FormatMask := '###,##0';
        Footer.FormulaKind := fkSum;
        end;
    end;
  end;

  with TNxNumberColumn(FrmBukuBesar.TableGrid1.Columns[6]) do
  begin
    FormatMask := '###,##0';
  end;

  if FrmBukuBesar.TableGrid1.RowCount > 1 then
  begin
    LastRow := FrmBukuBesar.TableGrid1.RowCount - 1;
    ColumnValue := FrmBukuBesar.TableGrid1.Cells[6, LastRow];

    if FrmBukuBesar.TableGrid1.Columns.Count > 6 then
    begin
      FrmBukuBesar.TableGrid1.Columns[6].Footer.Caption := ColumnValue;
      FrmBukuBesar.TableGrid1.Refresh;
    end
    else
      ShowMessage('Kolom ke-6 tidak ditemukan');
  end
  else
    ShowMessage('Tidak ada data di TableGrid1');

  FrmBukuBesar.TableGrid1.CalculateFooter;
  FrmBukuBesar.TableGrid1.Columns[7].Visible := False;
end;

3 (edited by sparrow 2025-06-16 09:15:53)

Re: FooterValue

1. You have strange math in your table.


2. There is a mistake here:   

   Footer.FormulaValue := Debet - Kredit;   < ----- does not cause an error but does not display the value
   Footer.Caption:= FloatToStr(Debet - Kredit);  < ----- that's right

Re: FooterValue

I am creating a ledger with a running balance, therefore I cannot use the debit - credit formula.
for running balance i use this query:

-- Baris Saldo Awal
SELECT 
  a.kode_akun AS Kode_Akun,
  a.nama_akun AS Nama_Akun,
  strftime('%d', date({DateTimePicker1}, '-1 day')) || ' ' ||
        CASE strftime('%m', date({DateTimePicker1}, '-1 day'))
            WHEN '01' THEN 'Jan'                                        
            WHEN '02' THEN 'Feb'
            WHEN '03' THEN 'Mar'         
            WHEN '04' THEN 'Apr'
            WHEN '05' THEN 'Mei'                
            WHEN '06' THEN 'Jun'
            WHEN '07' THEN 'Jul'
            WHEN '08' THEN 'Agt'
            WHEN '09' THEN 'Sep'
            WHEN '10' THEN 'Okt'
            WHEN '11' THEN 'Nov'
            WHEN '12' THEN 'Des'
        END || ' ' || strftime('%Y', date({DateTimePicker1}, '-1 day')) AS Tanggal,
  'Saldo Awal' AS Deskripsi,
  CAST(0 AS NUMERIC) AS Total_Debet,
  CAST(0 AS NUMERIC) AS Total_Kredit,

  IFNULL((
    SELECT SUM(CASE 
      WHEN td2.posisi = 'Debet' THEN td2.jumlah 
      ELSE -td2.jumlah 
    END)
    FROM transaksi_detail td2 
    JOIN transaksi t2 ON td2.id_transaksi = t2.id
    WHERE td2.id_akun = a.id
      AND t2.tanggal < {DateTimePicker1}
  ), 0) AS Saldo_Berjalan,
  0 AS urutan_sorting

FROM akun a
WHERE a.id = {ComboBox1}

UNION ALL

-- Transaksi Normal
SELECT 
  a.kode_akun AS Kode_Akun,
  a.nama_akun AS Nama_Akun,
  strftime('%d', t.tanggal) || ' ' ||
        CASE strftime('%m', t.tanggal)
            WHEN '01' THEN 'Jan'
            WHEN '02' THEN 'Feb'
            WHEN '03' THEN 'Mar'         
            WHEN '04' THEN 'Apr'
            WHEN '05' THEN 'Mei'
            WHEN '06' THEN 'Jun'
            WHEN '07' THEN 'Jul'
            WHEN '08' THEN 'Agt'
            WHEN '09' THEN 'Sep'
            WHEN '10' THEN 'Okt'
            WHEN '11' THEN 'Nov'
            WHEN '12' THEN 'Des'
        END || ' ' || strftime('%Y', t.tanggal) AS Tanggal,
  t.deskripsi AS Deskripsi,
  SUM(CASE WHEN td.posisi = 'Debet' THEN td.jumlah ELSE 0 END) AS Total_Debet,
  SUM(CASE WHEN td.posisi = 'Kredit' THEN td.jumlah ELSE 0 END) AS Total_Kredit,
  (
    SELECT SUM(CASE 
      WHEN td2.posisi = 'Debet' THEN td2.jumlah                
      ELSE -td2.jumlah 
    END)
    FROM transaksi_detail td2                               
    JOIN transaksi t2 ON td2.id_transaksi = t2.id
    WHERE td2.id_akun = a.id
      AND (t2.tanggal < t.tanggal OR (t2.tanggal = t.tanggal AND t2.id <= t.id))
  ) AS Saldo_Berjalan,                   
  t.tanggal AS urutan_sorting

FROM transaksi_detail td
JOIN transaksi t ON td.id_transaksi = t.id
JOIN akun a ON td.id_akun = a.id
WHERE a.id = {ComboBox1}
  AND t.tanggal BETWEEN {DateTimePicker1} AND {DateTimePicker2} 
  AND a.id_status_akun = 1  -- hanya akun aktif 
GROUP BY a.kode_akun, a.nama_akun, a.id, t.tanggal, t.deskripsi, t.id
                                           
ORDER BY urutan_sorting ASC;

This is Running Balance:

  (
    SELECT SUM(CASE 
      WHEN td2.posisi = 'Debet' THEN td2.jumlah                
      ELSE -td2.jumlah 
    END)
    FROM transaksi_detail td2                               
    JOIN transaksi t2 ON td2.id_transaksi = t2.id
    WHERE td2.id_akun = a.id
      AND (t2.tanggal < t.tanggal OR (t2.tanggal = t.tanggal AND t2.id <= t.id))
  ) AS Saldo_Berjalan

Re: FooterValue

Look at the last three lines of your picture from the previous post. Aren't you confused by the final balance of the last line?
Let's count?
18500 - 700 = 17800 (doesn't match here anymore!!!)
17800 - 600 = 17200 (your total is 17900!!!)
Why?
Because the records are sorted incorrectly when output in the request.
The last two lines are swapped. TOTAL INCORRECT AMOUNT OF CURRENT BALANCE at the end of the 12th.. But everything is fine)))). The cashier will have to give his money to the cash register, but everything is correct in your program)))).

But the cashier is not to blame.

Sort correctly. By date, time, transaction number, other parameters. And double-check the program. It's money.

Re: FooterValue

Hi Agusecc,
Jean and I did some work a couple of years ago on a CashBook (Running Balance) application.
Attached is a cut down version of it that might be useful.
There is probably a link to Jean's fully featured application if you search on the Forum for it.
As Sparrow has mentioned, make sure you have defined your transaction date as date/time and not just date or your 'running balance' calculation can get messed up if you have multiple transactions on the same day.
Regards,
Derek.

Post's attachments

Attachment icon cash book agusecc (2).zip 442.41 kb, 32 downloads since 2025-06-16 

Re: FooterValue

thanks you sparrow and Derek, excelent