begin SQLTime:=True; s:='select * from jshjk.db where a<>nil'; for i := 0 to jshjcx.ComponentCount-1 do begin try str:=jshjcx.Components[i].Name; if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框 and((jshjcx.Components[i] as Tcombobox).text<>'))then if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name) or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制字段长度为了4 +(jshjcx.Components[i] as Tcombobox).text else //如果时间相关的年份有数据的话 if ((ComboboxYear.text<>') and SQLTime)then begin s:=s+' and hjsj between :date1 and :date2'; SQLTime:=false; end; if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话 and((jshjcx.Components[i] as TEdit).text<>'))then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制了字段长度必须为4 +(jshjcx.Components[i] as TEdit).text; except exit; end;
end; showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题 end; //设置完SQL语句之后开始查询 with query1 do begin Close; SQL.Clear; SQL.Add(s); ParamByName('date1').AsDate:= EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1); ParamByName('date2').AsDate:= EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30); prepare; open; end;