帮帮我吧!下面的代码,想实现三个控件选择任意其中的内容 
组合都能查询。编译执行提示。
选择第一个控件中的内容提示。
list index out of bounds(1)
选择第二个控件中的内容提示
list index out of bounds(2)
选择第三个控件中的内容提示
list index out of bounds(3)
我不知道如何是好,我想找一个高人帮我试一试,指出一条光明大道。
我虚心好学。痛恨那些只说些无关痛痒的话的人、procedure TForm6.BitBtn1Click(Sender: TObject);
var
i:integer;
var
check:array [1..4] of string;
var
sun:array [1..4] of string;
var
s2,s1,a1:string;
var
p:integer;
begin
query1.DatabaseName:='sun';
for i:=1 to 3 do
begin
if i=1 then
begin
Check[i]:='fanwei';
end;
if i=2 then
begin
Check[i]:='gongre';
end;
if i=3 then
begin
Check[i]:='dizhi';
end
end;
for i:=1 to 3 do
begin
if i=1 then
begin
sun[i]:=sun1.Text;
end;
if i=2 then
begin
sun[i]:=sun2.Text;
end;
if i=3 then
begin
sun[i]:=sun3.Text;
end
end;
query1.SQL.Clear;
query1.SQL.Add('select * from "housesource" as s where ');
for i:=1 to 3 do
if sun[i]<>'' then
begin
s2:=inttostr(i);
s1:='s.'+'"'+check[i]+'"'+'=:p'+s2+' and ';
a1:=a1+s1;
end;
a1:=a1+'s."id">:p20';
query1.SQL.Add(a1);
edit2.Text:=query1.SQL.Text;
query1.Prepare;
p:=0;
for i:=0 to 2 do
if sun[i+1]<>'' then
bfegin
query1.Params[p].AsString:=sun[i+1];
p:=p+1;
end;
query1.Params[p+1].AsInteger:=1;
query1.Open;
datasource1.DataSet:=query1;
dbgrid1.DataSource:=datasource1;
end;

解决方案 »

  1.   

    哪句代码也没错,编译后运行
    出现选择第一个控件中的内容提示。
    list index out of bounds(1)
    选择第二个控件中的内容提示
    list index out of bounds(2)
    选择第三个控件中的内容提示
    list index out of bounds(3)
      

  2.   

    写了那么我垃圾代码, 累不?
    Check[1]:='fanwei';
    Check[2]:='gongre';
    Check[3]:='dizhi';
    sun[1]:=sun1.Text;
    sun[2]:=sun2.Text;
    sun[3]:=sun3.Text;
    简单问题复杂化了, 思维有问题.a1初始化了吗?
    Params 的项你创建了吗? Params 没有创建参数项, query1.Params[p+1].AsInteger:=1; 的操作产生异常. 也可以 Params.ParseSQL(SQL.Text, True); 得到参数列表.