你把SQL语句赋给一个字符串变量,当增加查询条件时在改变该字符串! 如: if Edit1.text<>'' then str:='select * from table where name="'+Edit1.text+'"'; if CheckBox1.Checked then str:=str+' and age='+edit2.text; query.sql.add(str);
下面的是一个支票查询窗口 控件如下: DateTimePicker1---DateTimePicker2//决定查询时间段 checkbox1 ---->combobox1//按付款银行查: checkbox2 ---->combobox2//按出票帐号查: checkbox3 ---->edit1//按金额查: checkbox4 ---->edit2//按用途查: checkbox5 ---->combobox5//按开票人查: procedure TForm1.Button1Click(Sender: TObject); var str1,str2,str3,d1,d2,d3,d4,moneys:string; i:integer; fuck,a,b,c,d,e:boolean; begin screen.cursor:=crhourglass; selection:=1; d1:=datetostr(datetimepicker1.Date); d2:=datetostr(datetimepicker2.Date); setlength(d1,2); setlength(d2,2); if d1>'50' then d1:='ch19'+d1 else d1:='ch20'+d1; if d2>'50' then d2:='ch19'+d2 else d2:='ch20'+d2; listbox1.Items.Clear; with datamodule1.Table8 do begin open; first; repeat d3:=fieldbyname('cfile').asstring; setlength(str3,6); if (d3>=d1) and (d3<=d2) then begin d3:=d3+'.db'; listbox1.Items.Add(d3); end; next; until eof; close; end; with datamodule1.Table3 do begin open; if recordcount<> 0 then for i:=0 to recordcount-1 do begin delete; edit; post; end; end; // if listbox1.items.Count <> 0 then for i:=0 to listbox1.Items.Count-1 do with datamodule1 do begin if table9.Active=true then table9.Close; table9.TableName:=listbox1.items[i]; table9.Open; table9.First; repeat fuck:=((table9.FieldByName('date').asstring>=datetostr(datetimepicker1.date)) and (table9.FieldByName('date').asstring<=datetostr(datetimepicker2.date))); a:=table9.FieldByName('bank').asstring=combobox1.Text; b:=table9.FieldByName('count').asstring=combobox2.Text; c:=table9.FieldByName('useway').asstring=edit2.Text; d:=table9.fieldbyname('operator').asstring=combobox5.text; e:=table9.fieldbyname('money').asstring=edit1.text; if checkbox1.Checked then fuck:=fuck and a; if checkbox2.Checked then fuck:=fuck and b; if checkbox3.Checked then fuck:=fuck and e; if checkbox4.Checked then fuck:=fuck and c; if checkbox5.Checked then fuck:=fuck and d; if fuck then begin table3.Last; table3.Append; table3.Fields[0].value:=table9.fields[0].Value; table3.Fields[1].value:=table9.fields[1].Value; table3.Fields[2].value:=table9.fields[2].Value; table3.Fields[3].value:=table9.fields[3].Value; table3.Fields[4].value:=table9.fields[4].Value; table3.Fields[5].value:=table9.fields[5].Value; table3.Fields[6].value:=table9.fields[6].Value; table3.Fields[7].value:=table9.fields[7].Value; table3.Fields[8].value:=table9.fields[8].Value; table3.Fields[9].value:=table9.fields[9].Value; table3.Fields[10].value:=table9.fields[10].Value; table3.Fields[11].value:=table9.fields[11].Value; table3.Fields[12].value:=table9.fields[12].Value; table3.Fields[13].value:=table9.fields[13].Value; table3.Fields[14].value:=table9.fields[14].Value; table3.post; end; table9.Next; until table9.eof; table9.Close; end; with datamodule1.Query1 do begin close; with sql do begin clear; add('select * from cash'); add('order by serial desc'); end; open; end; form5:=tform5.create(self); form5.showmodal;end;
如:
if Edit1.text<>'' then
str:='select * from table where name="'+Edit1.text+'"';
if CheckBox1.Checked then
str:=str+' and age='+edit2.text;
query.sql.add(str);
控件如下:
DateTimePicker1---DateTimePicker2//决定查询时间段
checkbox1 ---->combobox1//按付款银行查:
checkbox2 ---->combobox2//按出票帐号查:
checkbox3 ---->edit1//按金额查:
checkbox4 ---->edit2//按用途查:
checkbox5 ---->combobox5//按开票人查:
procedure TForm1.Button1Click(Sender: TObject);
var
str1,str2,str3,d1,d2,d3,d4,moneys:string;
i:integer;
fuck,a,b,c,d,e:boolean;
begin
screen.cursor:=crhourglass;
selection:=1; d1:=datetostr(datetimepicker1.Date);
d2:=datetostr(datetimepicker2.Date);
setlength(d1,2);
setlength(d2,2);
if d1>'50' then d1:='ch19'+d1 else d1:='ch20'+d1;
if d2>'50' then d2:='ch19'+d2 else d2:='ch20'+d2; listbox1.Items.Clear;
with datamodule1.Table8 do
begin
open;
first;
repeat
d3:=fieldbyname('cfile').asstring;
setlength(str3,6);
if (d3>=d1) and (d3<=d2) then
begin
d3:=d3+'.db';
listbox1.Items.Add(d3);
end;
next;
until eof;
close;
end;
with datamodule1.Table3 do
begin
open;
if recordcount<> 0 then
for i:=0 to recordcount-1 do
begin
delete;
edit;
post;
end;
end;
//
if listbox1.items.Count <> 0 then
for i:=0 to listbox1.Items.Count-1 do
with datamodule1 do
begin
if table9.Active=true then table9.Close;
table9.TableName:=listbox1.items[i];
table9.Open;
table9.First;
repeat
fuck:=((table9.FieldByName('date').asstring>=datetostr(datetimepicker1.date)) and (table9.FieldByName('date').asstring<=datetostr(datetimepicker2.date)));
a:=table9.FieldByName('bank').asstring=combobox1.Text;
b:=table9.FieldByName('count').asstring=combobox2.Text;
c:=table9.FieldByName('useway').asstring=edit2.Text;
d:=table9.fieldbyname('operator').asstring=combobox5.text;
e:=table9.fieldbyname('money').asstring=edit1.text; if checkbox1.Checked then
fuck:=fuck and a;
if checkbox2.Checked then
fuck:=fuck and b;
if checkbox3.Checked then
fuck:=fuck and e;
if checkbox4.Checked then
fuck:=fuck and c;
if checkbox5.Checked then
fuck:=fuck and d; if fuck then
begin
table3.Last;
table3.Append;
table3.Fields[0].value:=table9.fields[0].Value;
table3.Fields[1].value:=table9.fields[1].Value;
table3.Fields[2].value:=table9.fields[2].Value;
table3.Fields[3].value:=table9.fields[3].Value;
table3.Fields[4].value:=table9.fields[4].Value;
table3.Fields[5].value:=table9.fields[5].Value;
table3.Fields[6].value:=table9.fields[6].Value;
table3.Fields[7].value:=table9.fields[7].Value;
table3.Fields[8].value:=table9.fields[8].Value;
table3.Fields[9].value:=table9.fields[9].Value;
table3.Fields[10].value:=table9.fields[10].Value;
table3.Fields[11].value:=table9.fields[11].Value;
table3.Fields[12].value:=table9.fields[12].Value;
table3.Fields[13].value:=table9.fields[13].Value;
table3.Fields[14].value:=table9.fields[14].Value;
table3.post;
end;
table9.Next;
until table9.eof;
table9.Close;
end; with datamodule1.Query1 do
begin
close;
with sql do
begin
clear;
add('select * from cash');
add('order by serial desc');
end;
open;
end; form5:=tform5.create(self);
form5.showmodal;end;
過濾是在本地進行的,所以能好點