我想在同一个页面中做一个混查,就是说,在eidt中,有输入便查,没输入便不查,字符型要求模糊查询,可是当日期型加进去混查时老出错 ------------------------------------------------ 就楼主“在eidt中” ,日期型输入格式 : yyyy-mm-dd hh:nn:ss 另外 procedure Tform1.QueryBtnClick(Sender: TObject); var str:string; begin str := ''; if edit1.Text <> '' then // edit1.Text--- 输入数值 str := str + ' data1 ='+edit1.Text else str := str + '';if edit2.Text <> '' then // edit2.Text--- 输入字符 begin if edit1.Text <> '' then str := str + ' and data2 ='''+edit2.Text+'''' else str := str + ' data2 ='''+edit2.Text+'''' ; end else str := str + '';if edit3.Text <> '' then // edit3.Text--- 输入日期 begin if edit2.Text <> '' then str := str + ' and data3 ='''+edit3.Text+'''' else str := str + ' data3 ='''+edit3.Text+'''' ; end else str := str + '';try ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from yourtable where '+Str); ADOQuery1.open; except //-----异常处理 end; end;end;
我的。 //如果选择日期,加入日期条件 if cbbdate.Checked =true then StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+ '#'+' and '+'#'+datetostr(dtpsfend.Date)+'#'; //如果选择客户 if cbkh1.Checked=true then begin StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+''''; end; //如果选择表名 if cbbm1.Checked=true then begin StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+''''; end; //如果选择收费员 if cbsfy1.Checked=true then begin StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果选择开票员 if cbkpy1.Checked=true then begin StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果是选择水费或者电费 if rbsf.Checked=true then begin StrSql:=StrSql+' and khbh like '''+'w%'+''''; end else begin StrSql:=StrSql+' and khbh like '''+'e%'+''''; end; //开始查找数据 StrSql:=copy(StrSql,5,255); if StrSql='' then begin showmessage('请选择一个查询条件,在其前面打钩即可'); exit; end; with dmmain.ADOQKhsf do begin sql.Clear; sql.Add('select * from khsf where'); sql.Add(StrSql); sql.Add('order by khdm,khbh'); open; end;
我 的。。//如果选择日期,加入日期条件 if cbbdate.Checked =true then StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+ '#'+' and '+'#'+datetostr(dtpsfend.Date)+'#'; //如果选择客户 if cbkh1.Checked=true then begin StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+''''; end; //如果选择表名 if cbbm1.Checked=true then begin StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+''''; end; //如果选择收费员 if cbsfy1.Checked=true then begin StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果选择开票员 if cbkpy1.Checked=true then begin StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果是选择水费或者电费 if rbsf.Checked=true then begin StrSql:=StrSql+' and khbh like '''+'w%'+''''; end else begin StrSql:=StrSql+' and khbh like '''+'e%'+''''; end; //开始查找数据 StrSql:=copy(StrSql,5,255); if StrSql='' then begin showmessage('请选择一个查询条件,在其前面打钩即可'); exit; end; with dmmain.ADOQKhsf do begin sql.Clear; sql.Add('select * from khsf where'); sql.Add(StrSql); sql.Add('order by khdm,khbh'); open; end;
tempwhere:=''; if length(tempdh)>2 then tempwhere:=tempwhere+ ' dh like ''' +tempdH+''' And '; if length(tempXMMC)>2 then tempwhere:=tempwhere+ ' name like ''' +tempXMMC+''' And '; if length(tempJSDW)>2 then tempwhere:=tempwhere+ ' danwei like ''' +tempJSDW+''' And '; if length(tempXKZH)>2 then tempwhere:=tempwhere+ ' xzyjs like ''' +tempXKZH+''' And '; if length(tempJBR)>2 then tempwhere:=tempwhere+ ' JBR like ''' +tempJBR+''' And '; tempwhere:= copy(tempwhere,1,length(tempwhere)-5);//截掉最后一个, label1.Caption :=GhywQueryzh('1','tablename',tempwhere,dm.sdsGhyw,dbgrid1); end;
tempDh:='%'+trim(edit3.text)+'%'; tempXMMC:='%'+trim(edit4.text)+'%'; tempJSDW:='%'+trim(edit5.text)+'%'; tempXKZH:='%'+trim(edit6.text)+'%'; tempJBR:='%'+trim(edit7.text)+'%';tempwhere:=''; if length(tempdh)>2 then tempwhere:=tempwhere+ ' dh like ''' +tempdH+''' And '; if length(tempXMMC)>2 then tempwhere:=tempwhere+ ' name like ''' +tempXMMC+''' And '; if length(tempJSDW)>2 then tempwhere:=tempwhere+ ' danwei like ''' +tempJSDW+''' And '; if length(tempXKZH)>2 then tempwhere:=tempwhere+ ' xzyjs like ''' +tempXKZH+''' And '; if length(tempJBR)>2 then tempwhere:=tempwhere+ ' JBR like ''' +tempJBR+''' And '; tempwhere:= copy(tempwhere,1,length(tempwhere)-5);//截掉最后一个, label1.Caption :=GhywQueryzh('1','tablename',tempwhere,dm.sdsGhyw,dbgrid1); end;
var
str:string;
begin
str := ' where';
if edit1.Text <> '' then
begin
str := str + ' Bjdh ='''+edit1.Text+'''';
str := str + ' and';
end;if edit2.Text <> '' then
begin
str := str + ' yjmc ='''+edit2.Text+'''';
str := str + ' and';
end;if combobox1.Text <> '' then
begin
str := str + ' yjlx ='''+combobox1.Text+'''';
str := str + ' and';
end;if combobox2.Text <> '' then
begin
str := str + ' khmc ='''+combobox2.Text+'''';
str := str + ' and';
end;if combobox3.Text <> '' then
begin
str := str + ' zt ='''+combobox3.Text+'''';
str := str + ' and';
end;if combobox4.Text <> '' then
begin
str := str + ' cly ='''+combobox4.Text+'''';
str := str + ' and';
end;if Checkbox1.Checked=true then
begin
if DateTimePicker1.date>DateTimePicker2.Date then
begin
application.messagebox('起始日期不能大于终止日期','错误',mb_ok+mb_iconstop);
exit;
end
else begin
{两种情况都能运行}
//Strwhere:=Strwhere+' rksj>=:datefrom and rksj<=:dateto and';
Str:=Str+' rq between :DateTimePicker1 and :DateTimePicker2 and';
end; end; Str:=copy(Str,1,length(Str)-3);//去掉最后一个and
scdddm.adoQbjd.Close;
scdddm.adoQbjd.SQL.Clear;
scdddm.adoQbjd.SQL.Add('select * from sc_bjd'+Str);
if checkbox1.Checked=true then
begin
scdddm.adoQbjd.Parameters.ParamByName('DateTimePicker1').value:=DateTimePicker1.Date;
scdddm.adoQbjd.Parameters.ParamByName('DateTimePicker2').value:=DateTimePicker2.Date;
end;
scdddm.adoQbjd.Open;
end;
sql server使用'2000-1-1'这种格式
------------------------------------------------
就楼主“在eidt中” ,日期型输入格式 : yyyy-mm-dd hh:nn:ss
另外
procedure Tform1.QueryBtnClick(Sender: TObject);
var
str:string;
begin
str := '';
if edit1.Text <> '' then // edit1.Text--- 输入数值
str := str + ' data1 ='+edit1.Text
else
str := str + '';if edit2.Text <> '' then // edit2.Text--- 输入字符
begin
if edit1.Text <> '' then
str := str + ' and data2 ='''+edit2.Text+''''
else
str := str + ' data2 ='''+edit2.Text+'''' ;
end
else
str := str + '';if edit3.Text <> '' then // edit3.Text--- 输入日期
begin
if edit2.Text <> '' then
str := str + ' and data3 ='''+edit3.Text+''''
else
str := str + ' data3 ='''+edit3.Text+'''' ;
end
else
str := str + '';try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from yourtable where '+Str);
ADOQuery1.open;
except
//-----异常处理
end;
end;end;
//如果选择日期,加入日期条件
if cbbdate.Checked =true then
StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+
'#'+' and '+'#'+datetostr(dtpsfend.Date)+'#';
//如果选择客户
if cbkh1.Checked=true then
begin
StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+'''';
end;
//如果选择表名
if cbbm1.Checked=true then
begin
StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+'''';
end;
//如果选择收费员
if cbsfy1.Checked=true then
begin
StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果选择开票员
if cbkpy1.Checked=true then
begin
StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果是选择水费或者电费
if rbsf.Checked=true then
begin
StrSql:=StrSql+' and khbh like '''+'w%'+'''';
end
else
begin
StrSql:=StrSql+' and khbh like '''+'e%'+'''';
end;
//开始查找数据
StrSql:=copy(StrSql,5,255);
if StrSql='' then
begin
showmessage('请选择一个查询条件,在其前面打钩即可');
exit;
end;
with dmmain.ADOQKhsf do
begin
sql.Clear;
sql.Add('select * from khsf where');
sql.Add(StrSql);
sql.Add('order by khdm,khbh');
open;
end;
if cbbdate.Checked =true then
StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+
'#'+' and '+'#'+datetostr(dtpsfend.Date)+'#';
//如果选择客户
if cbkh1.Checked=true then
begin
StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+'''';
end;
//如果选择表名
if cbbm1.Checked=true then
begin
StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+'''';
end;
//如果选择收费员
if cbsfy1.Checked=true then
begin
StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果选择开票员
if cbkpy1.Checked=true then
begin
StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果是选择水费或者电费
if rbsf.Checked=true then
begin
StrSql:=StrSql+' and khbh like '''+'w%'+'''';
end
else
begin
StrSql:=StrSql+' and khbh like '''+'e%'+'''';
end;
//开始查找数据
StrSql:=copy(StrSql,5,255);
if StrSql='' then
begin
showmessage('请选择一个查询条件,在其前面打钩即可');
exit;
end;
with dmmain.ADOQKhsf do
begin
sql.Clear;
sql.Add('select * from khsf where');
sql.Add(StrSql);
sql.Add('order by khdm,khbh');
open;
end;
if length(tempXMMC)>2 then tempwhere:=tempwhere+ ' name like ''' +tempXMMC+''' And ';
if length(tempJSDW)>2 then tempwhere:=tempwhere+ ' danwei like ''' +tempJSDW+''' And ';
if length(tempXKZH)>2 then tempwhere:=tempwhere+ ' xzyjs like ''' +tempXKZH+''' And ';
if length(tempJBR)>2 then tempwhere:=tempwhere+ ' JBR like ''' +tempJBR+''' And ';
tempwhere:= copy(tempwhere,1,length(tempwhere)-5);//截掉最后一个, label1.Caption :=GhywQueryzh('1','tablename',tempwhere,dm.sdsGhyw,dbgrid1);
end;
tempXMMC:='%'+trim(edit4.text)+'%';
tempJSDW:='%'+trim(edit5.text)+'%';
tempXKZH:='%'+trim(edit6.text)+'%';
tempJBR:='%'+trim(edit7.text)+'%';tempwhere:=''; if length(tempdh)>2 then tempwhere:=tempwhere+ ' dh like ''' +tempdH+''' And ';
if length(tempXMMC)>2 then tempwhere:=tempwhere+ ' name like ''' +tempXMMC+''' And ';
if length(tempJSDW)>2 then tempwhere:=tempwhere+ ' danwei like ''' +tempJSDW+''' And ';
if length(tempXKZH)>2 then tempwhere:=tempwhere+ ' xzyjs like ''' +tempXKZH+''' And ';
if length(tempJBR)>2 then tempwhere:=tempwhere+ ' JBR like ''' +tempJBR+''' And ';
tempwhere:= copy(tempwhere,1,length(tempwhere)-5);//截掉最后一个, label1.Caption :=GhywQueryzh('1','tablename',tempwhere,dm.sdsGhyw,dbgrid1);
end;