在表单上放置两个TDateTimePicker控件(对应开始日期和终止日期)、一个TComboBox控件,以及其它相关控件,数据库控件是采用ADO控件。
Button1控件的事件代码如果如下时,提示错误,代码如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=:jx and a.f2_id in (1,2) ');
sql.Add('and a.f2 between :y_ks and :y_js '); parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date); //在这里会提示错误
parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1); //在这里会提示错误
parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;
如果代码改为如下,则不会出错:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
label3.caption:='';
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=''AA'' and a.f2_id in (1,2) '); //改为常量
sql.Add('and a.f2 between :y_ks and :y_js '); parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date);
parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1);
//parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;如果代码改为如下,也不会出错:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
label3.caption:='';
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=:jx and a.f2_id in (1,2) ');
sql.Add('and a.f2 between to_date('2004/10/11','yyyy/mm/dd') and to_date('2004/10/15','yyyy/mm/dd') '); //parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date);
//parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1);
parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;请问什么原因?
Button1控件的事件代码如果如下时,提示错误,代码如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=:jx and a.f2_id in (1,2) ');
sql.Add('and a.f2 between :y_ks and :y_js '); parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date); //在这里会提示错误
parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1); //在这里会提示错误
parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;
如果代码改为如下,则不会出错:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
label3.caption:='';
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=''AA'' and a.f2_id in (1,2) '); //改为常量
sql.Add('and a.f2 between :y_ks and :y_js '); parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date);
parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1);
//parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;如果代码改为如下,也不会出错:
procedure TForm1.Button1Click(Sender: TObject);
begin
screen.Cursor:=crSQLWait;
label3.caption:='';
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=:jx and a.f2_id in (1,2) ');
sql.Add('and a.f2 between to_date('2004/10/11','yyyy/mm/dd') and to_date('2004/10/15','yyyy/mm/dd') '); //parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date);
//parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1);
parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;请问什么原因?
中jx的类型 和ComboBox1.text的类型不一致
begin
screen.Cursor:=crSQLWait;
with adoquery1 do
begin
close;
sql.Clear; sql.Add('select a.f1,b.f1,c.f2,d.f3 from table1 a,table2 b,table3 c,table4 d where ');
sql.Add('substr(a.so_nbr,1,2)=:jx and a.f2_id in (1,2) ');
sql.Add('and a.f2 between :y_ks and :y_js '); parameters.ParamValues['y_ks']:=formatdatetime('yyyy/mm/dd',DateTimePicker1.Date); //在这里会提示数据类型不对,如果sql.Add('substr(a.so_nbr,1,2)=:jx 改为常量sql.Add('substr(a.so_nbr,1,2)=''AA''则运行正常,为什么?
parameters.ParamValues['y_js']:=formatdatetime('yyyy/mm/dd',DateTimePicker2.Date+1); //在这里会提示数据类型不对,如果sql.Add('substr(a.so_nbr,1,2)=:jx 改为常量sql.Add('substr(a.so_nbr,1,2)=''AA''则运行正常,为什么? parameters.ParamValues['jx']:=ComboBox1.text; open; screen.Cursor:=crarrow;
button2.Enabled:=true;
button4.Enabled:=true;
ShowMessage('搜索完毕!!!');
end;
end;
为什么没人帮忙呀?
出错是因为你用了FormatDateTime(),这个函数返回的是字符型,不是日期型了
你直接用
parameters.ParamValues['y_ks']:=DateTimePicker1.Date;
应该不会出错了