首页 新闻 会员 周边 捐助

delphi做家庭财务管理系统

0
悬赏园豆:5 [待解决问题] 浏览: 496次

在支出统计窗体中,做一个按支出项目的名称统计。代码怎么写?

张雯雯的主页 张雯雯 | 初学一级 | 园豆:195
提问于:2012-03-30 12:56
< > 字节跳动旗下AI助手豆包
分享
所有回答(1)
0

能否说详细点,你哪里不会?

artwl | 园豆:16736 (专家六级) | 2012-03-30 19:39

这个窗体里的【按项目名称统计】实现的话代码怎写。                                                                     

目前只实现了效果如下:

 

它的现在的代码为如下:

unit untsrzctj;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, ExtCtrls, DB, ADODB;

type
  Tfrmsrzctj = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    CheckBox1: TCheckBox;
    Panel2: TPanel;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    ComboBox2: TComboBox;
    CheckBox2: TCheckBox;
    Label3: TLabel;
    ComboBox3: TComboBox;
    Label4: TLabel;
    ComboBox4: TComboBox;
    srtj: TADOQuery;
    zctj: TADOQuery;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBGrid2: TDBGrid;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    q1: TADOQuery;
    Label5: TLabel;
    Edit2: TEdit;
    Label6: TLabel;
    Edit3: TEdit;
    Label7: TLabel;
    Edit4: TEdit;
    procedure ComboBox1Enter(Sender: TObject);
    procedure ComboBox2Enter(Sender: TObject);
    procedure ComboBox3Enter(Sender: TObject);
    procedure ComboBox4Enter(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure ComboBox4Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmsrzctj: Tfrmsrzctj;

implementation

uses untdm;

{$R *.dfm}

procedure Tfrmsrzctj.ComboBox1Enter(Sender: TObject);
var
  i, j: integer;
begin
  ComboBox1.Items.Clear;
  j := strtoint(formatdatetime('yyyy', now));
  for i := j to j + 10 do
  begin
    ComboBox1.Items.Add(inttostr(i));
  end;
end;

procedure Tfrmsrzctj.ComboBox2Enter(Sender: TObject);
var
  i: integer;
begin
  ComboBox2.Items.Clear;
  for i := 1 to 12 do
  begin
    if length(inttostr(i)) = 1 then
    begin
      ComboBox2.Items.Add('0' + inttostr(i));
    end
    else
    begin
      ComboBox2.Items.Add(inttostr(i));
    end;
  end;
end;

procedure Tfrmsrzctj.ComboBox3Enter(Sender: TObject);
var
  i, j: integer;
begin
  ComboBox3.Items.Clear;
  j := strtoint(formatdatetime('yyyy', now));
  for i := j to j + 10 do
  begin
    ComboBox3.Items.Add(inttostr(i));
  end;
end;

procedure Tfrmsrzctj.ComboBox4Enter(Sender: TObject);
var
  i: integer;
begin
  ComboBox4.Items.Clear;
  for i := 1 to 12 do
  begin
    if length(inttostr(i)) = 1 then
    begin
      ComboBox4.Items.Add('0' + inttostr(i));
    end
    else
    begin
      ComboBox4.Items.Add(inttostr(i));
    end;
  end;
end;

procedure Tfrmsrzctj.FormShow(Sender: TObject);
var
  s: string;
begin
  ComboBox1.Text := ''; ComboBox2.Text := '';
  ComboBox3.Text := ''; ComboBox4.Text := ''; Edit1.Text := 'LSH' + formatdatetime('yyyymmddmmhhss', now);
  Edit2.Text := '0'; Edit3.Text := '0'; Edit4.Text := '0';
  s := 'delete from srtjb';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
  s := 'delete from zctjb';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
end;

procedure Tfrmsrzctj.Button1Click(Sender: TObject);
var
  s, date1, date2: string;
begin
  if CheckBox1.Checked = false or CheckBox2.Checked = false then
  begin
    application.MessageBox('请输入统计开始日期和终止日期!', '提示', 64);
    abort;
  end;
  date1 := ComboBox1.Text + '-' + ComboBox2.Text + '-01';
  date2 := ComboBox3.Text + '-' + ComboBox4.Text + '-01';
  s := 'delete from srtjb';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
  s := 'insert srtjb(lsh,xmmc,je)'
    + 'select ' + quotedstr(trim(Edit1.Text)) + ',xmmc,sum(je) from srxmls where rq between ' + quotedstr(date1 + ' 00:00:00') + ' and ' + quotedstr(date2 + ' 23:59:59')
    + ' group by xmmc';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
  s := 'delete from zctjb';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
  s := 'insert zctjb(lsh,xmmc,je)'
    + 'select ' + quotedstr(trim(Edit1.Text)) + ',xmmc,sum(je) from zcxmls where rq between ' + quotedstr(date1 + ' 00:00:00') + ' and ' + quotedstr(date2 + ' 23:59:59')
    + ' group by xmmc';
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; execsql;
  end;
  s := 'select * from srtjb where lsh=' + quotedstr(trim(Edit1.Text));
  with srtj do
  begin
    close; sql.Clear; sql.Add(s); prepared; open;
  end;
  s := 'select * from zctjb where lsh=' + quotedstr(trim(Edit1.Text));
  with zctj do
  begin
    close; sql.Clear; sql.Add(s); prepared; open;
  end;
  s := 'select sum(je) as je from srtjb where lsh=' + quotedstr(trim(Edit1.Text));
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; open;
  end;
  Edit2.Text := floattostr(q1.fieldbyname('je').AsFloat);
  s := 'select sum(je) as je from zctjb where lsh=' + quotedstr(trim(Edit1.Text));
  with q1 do
  begin
    close; sql.Clear; sql.Add(s); prepared; open;
  end;
  Edit3.Text := floattostr(q1.fieldbyname('je').AsFloat);
  Edit4.Text := floattostr(strtofloat(Edit2.Text) - strtofloat(Edit3.Text));
end;

procedure Tfrmsrzctj.ComboBox1Change(Sender: TObject);
begin
  if (ComboBox1.Text <> '') and (ComboBox2.Text <> '') then
  begin
    CheckBox1.Checked := true;
  end
  else
  begin
    CheckBox1.Checked := false;
  end;
end;

procedure Tfrmsrzctj.ComboBox2Change(Sender: TObject);
begin
  if (ComboBox1.Text <> '') and (ComboBox2.Text <> '') then
  begin
    CheckBox1.Checked := true;
  end
  else
  begin
    CheckBox1.Checked := false;
  end;
end;

procedure Tfrmsrzctj.ComboBox3Change(Sender: TObject);
begin
  if (ComboBox3.Text <> '') and (ComboBox4.Text <> '') then
  begin
    CheckBox2.Checked := true;
  end
  else
  begin
    CheckBox2.Checked := false;
  end;
end;

procedure Tfrmsrzctj.ComboBox4Change(Sender: TObject);
begin
  if (ComboBox3.Text <> '') and (ComboBox4.Text <> '') then
  begin
    CheckBox2.Checked := true;
  end
  else
  begin
    CheckBox2.Checked := false;
  end;
end;

procedure Tfrmsrzctj.Button2Click(Sender: TObject);
begin
close;
end;

end.

支持(0) 反对(0) 张雯雯 | 园豆:195 (初学一级) | 2012-03-30 20:07

@张雯雯: Delphi的代码不是很熟悉,你这里既然可以统计出所有的项目的,那么按项目名称统计时只要在SQL语句中添加一个条件 and 项目名称 = ComboBox5选中的值就好了吧

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-03-30 20:19

@artwl: 在SQL语句中吗?用你的方法具体怎么写呢?

支持(0) 反对(0) 张雯雯 | 园豆:195 (初学一级) | 2012-03-30 20:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册
Top