我的思路是用户选择preview时,弹出一个选择字段框,通过将adoquery的sql.text中的原有字段替换为所选字段.然后发出查询,用fastreport显示出来.生成的SQL语句我看是没问题,用查询分析器查也没问题,可用程序就是出错,才是出现Field:xxxx not found.请大家帮忙解决.不胜感谢.
程序代码 private { Private declarations } public function GetGenerate(Sqlstr:String):String; function GetCheckStr:string; function Filterstr(Str:String):String; { Public declarations } end;var SelectFieldForms: TSelectFieldForms;implementationuses dmunit, MainAlllist;{$R *.dfm}function TSelectFieldForms.Filterstr(Str:String): String; begin if str='Serial Number' then Result:='[Serial Number]' else if str='physical-off-location' then Result:='[physical-off-location]' else if str='Purchase date' then Result:='[purchase-date]' else if str='Expiry date' then Result:='[ma-expiry]' else if Str='Cost' then Result:='[ma-fee]' else if Str='Warranty Provider' then Result:='[provide-name]' else if str='Category' then Result:='[cata-name]' else if str='Department' then Result:='[dept-name]' else if str='Maintenance times' then Result:='[ma-times]' else if str='Current Maintenance' then Result:='Maintenance' else if str='Memo' then Result:='[Memo]' else result:=str; end;procedure TSelectFieldForms.FormShow(Sender: TObject); Var i:Integer; begin // for i:=0 to CheckListbox1.Items.Count-1 do CheckListBox1.Checked[i]:=true;end;function TSelectFieldForms.GetCheckStr: string; Var i,j:integer; Str:String; begin j:=0; for i:=0 to CheckListbox1.Items.Count-1 do begin if CheckListBox1.Checked[i]=true then Inc(j); end; if j-1=CheckListbox1.Items.Count-1 then Result:='*' else begin for i:=0 to CheckListBox1.Items.Count-1 do begin if CheckListBox1.Checked[i]=true then begin Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+','; //µÃ³ö×ֶΠend; end; Result:=str; end; //»¹Ã»ÓÐдÍê¡£ end;function TSelectFieldForms.GetGenerate(Sqlstr: String): String; Var Temp1,Temp2,Temp3:string; begin if GetcheckStr='*' then begin Result:=Sqlstr; exit; end; if Sqlstr<>'' then begin if Pos('*',Sqlstr)>0 then begin Temp1:=Copy(Sqlstr,0,Pos('*',Sqlstr)-1); Temp2:=Copy(Sqlstr,(Pos('*',Sqlstr)+2),(Length(Sqlstr)-(Pos('*',Sqlstr)+2))); Temp3:=Copy(GetCheckStr,0,Length(GetCheckStr)-1); Result:=Temp1+' '+Temp3+' '+Temp2; end; end; end;procedure TSelectFieldForms.BitBtn1Click(Sender: TObject); Var SqlStr:string; begin Memo1.Clear; // Memo1.Lines.Add(GetGenerate(dm.MainAllQuery.SQL.Text)); // ShowMessage(GetGenerate(dm.MainAllQuery.SQL.Text)); Sqlstr:=GetGenerate(dm.MainAllQuery.SQL.Text); ShowMessage(Sqlstr); Memo1.Lines.Add(Sqlstr); With Dm.MainAllQuery do begin Close; Sql.Clear; Sql.Add(Sqlstr); // try open; // except // raise Exception.Create('Can''t open this table'); // end; end; end;
private { Private declarations } public function GetGenerate(Sqlstr:String):String;//提取原字段,替换新字段. function GetCheckStr:string; //检查CheckListBox中选择的字段,并转换为xx,xxx,xx,形式 function Filterstr(Str:String):String;//过滤CheckListbox中的文字为字段名 { Public declarations } end;var SelectFieldForms: TSelectFieldForms;implementationuses dmunit, MainAlllist;{$R *.dfm}function TSelectFieldForms.Filterstr(Str:String): String; begin if str='Serial Number' then Result:='[Serial Number]' else if str='physical-off-location' then Result:='[physical-off-location]' else if str='Purchase date' then Result:='[purchase-date]' else if str='Expiry date' then Result:='[ma-expiry]' else if Str='Cost' then Result:='[ma-fee]' else if Str='Warranty Provider' then Result:='[provide-name]' else if str='Category' then Result:='[cata-name]' else if str='Department' then Result:='[dept-name]' else if str='Maintenance times' then Result:='[ma-times]' else if str='Current Maintenance' then Result:='Maintenance' else if str='Memo' then Result:='[Memo]' else result:=str; end;procedure TSelectFieldForms.FormShow(Sender: TObject); Var i:Integer; begin // for i:=0 to CheckListbox1.Items.Count-1 do CheckListBox1.Checked[i]:=true;end;function TSelectFieldForms.GetCheckStr: string; Var i,j:integer; Str:String; begin j:=0; for i:=0 to CheckListbox1.Items.Count-1 do begin if CheckListBox1.Checked[i]=true then Inc(j); end; if j-1=CheckListbox1.Items.Count-1 then Result:='*' else begin for i:=0 to CheckListBox1.Items.Count-1 do begin if CheckListBox1.Checked[i]=true then begin Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+','; //µÃ³ö×ֶΠend; end; Result:=str; end; //»¹Ã»ÓÐдÍê¡£ end;function TSelectFieldForms.GetGenerate(Sqlstr: String): String; Var Temp1,Temp2,Temp3:string; begin if GetcheckStr='*' then begin Result:=Sqlstr; exit; end; if Sqlstr<>'' then begin if Pos('*',Sqlstr)>0 then begin Temp1:=Copy(Sqlstr,0,Pos('*',Sqlstr)-1); Temp2:=Copy(Sqlstr,(Pos('*',Sqlstr)+2),(Length(Sqlstr)-(Pos('*',Sqlstr)+2))); Temp3:=Copy(GetCheckStr,0,Length(GetCheckStr)-1); Result:=Temp1+' '+Temp3+' '+Temp2; end; end; end;procedure TSelectFieldForms.BitBtn1Click(Sender: TObject); Var SqlStr:string; begin Memo1.Clear; // Memo1.Lines.Add(GetGenerate(dm.MainAllQuery.SQL.Text)); // ShowMessage(GetGenerate(dm.MainAllQuery.SQL.Text)); Sqlstr:=GetGenerate(dm.MainAllQuery.SQL.Text); ShowMessage(Sqlstr); Memo1.Lines.Add(Sqlstr); With Dm.MainAllQuery do begin Close; Sql.Clear; Sql.Add(Sqlstr); // try open; // except // raise Exception.Create('Can''t open this table'); // end; end; end;
程序沒有仔細分析,不過我覺得有兩個地方需要注意一下: 1. //在此處是否需要加一個初始化的動作?不過不加好象也沒有事, //因為局部變量會自動初始化的 for i:=0 to CheckListBox1.Items.Count-1 do begin if CheckListBox1.Checked[i]=true then begin Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+','; //此處返回時,是不是多了一個","號? end; end; Result:=str; 2.在Filterstr過程中,你用的是字符串比較 所以要注意大小寫的問題
{ Private declarations }
public
function GetGenerate(Sqlstr:String):String;
function GetCheckStr:string;
function Filterstr(Str:String):String;
{ Public declarations }
end;var
SelectFieldForms: TSelectFieldForms;implementationuses dmunit, MainAlllist;{$R *.dfm}function TSelectFieldForms.Filterstr(Str:String): String;
begin
if str='Serial Number' then
Result:='[Serial Number]'
else if str='physical-off-location' then
Result:='[physical-off-location]'
else if str='Purchase date' then
Result:='[purchase-date]'
else if str='Expiry date' then
Result:='[ma-expiry]'
else if Str='Cost' then
Result:='[ma-fee]'
else if Str='Warranty Provider' then
Result:='[provide-name]'
else if str='Category' then
Result:='[cata-name]'
else if str='Department' then
Result:='[dept-name]'
else if str='Maintenance times' then
Result:='[ma-times]'
else if str='Current Maintenance' then
Result:='Maintenance'
else if str='Memo' then
Result:='[Memo]'
else
result:=str;
end;procedure TSelectFieldForms.FormShow(Sender: TObject);
Var i:Integer;
begin
//
for i:=0 to CheckListbox1.Items.Count-1 do
CheckListBox1.Checked[i]:=true;end;function TSelectFieldForms.GetCheckStr: string;
Var i,j:integer;
Str:String;
begin
j:=0;
for i:=0 to CheckListbox1.Items.Count-1 do
begin
if CheckListBox1.Checked[i]=true then
Inc(j);
end;
if j-1=CheckListbox1.Items.Count-1 then
Result:='*'
else
begin
for i:=0 to CheckListBox1.Items.Count-1 do
begin
if CheckListBox1.Checked[i]=true then
begin
Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+','; //µÃ³ö×Ö¶Î
end;
end; Result:=str;
end;
//»¹Ã»ÓÐдÍê¡£
end;function TSelectFieldForms.GetGenerate(Sqlstr: String): String;
Var Temp1,Temp2,Temp3:string;
begin
if GetcheckStr='*' then
begin
Result:=Sqlstr;
exit;
end;
if Sqlstr<>'' then
begin
if Pos('*',Sqlstr)>0 then
begin
Temp1:=Copy(Sqlstr,0,Pos('*',Sqlstr)-1);
Temp2:=Copy(Sqlstr,(Pos('*',Sqlstr)+2),(Length(Sqlstr)-(Pos('*',Sqlstr)+2)));
Temp3:=Copy(GetCheckStr,0,Length(GetCheckStr)-1);
Result:=Temp1+' '+Temp3+' '+Temp2;
end;
end;
end;procedure TSelectFieldForms.BitBtn1Click(Sender: TObject);
Var SqlStr:string;
begin
Memo1.Clear;
// Memo1.Lines.Add(GetGenerate(dm.MainAllQuery.SQL.Text));
// ShowMessage(GetGenerate(dm.MainAllQuery.SQL.Text)); Sqlstr:=GetGenerate(dm.MainAllQuery.SQL.Text); ShowMessage(Sqlstr);
Memo1.Lines.Add(Sqlstr);
With Dm.MainAllQuery do
begin
Close;
Sql.Clear;
Sql.Add(Sqlstr);
// try
open;
// except
// raise Exception.Create('Can''t open this table');
// end;
end;
end;
{ Private declarations }
public
function GetGenerate(Sqlstr:String):String;//提取原字段,替换新字段.
function GetCheckStr:string; //检查CheckListBox中选择的字段,并转换为xx,xxx,xx,形式
function Filterstr(Str:String):String;//过滤CheckListbox中的文字为字段名
{ Public declarations }
end;var
SelectFieldForms: TSelectFieldForms;implementationuses dmunit, MainAlllist;{$R *.dfm}function TSelectFieldForms.Filterstr(Str:String): String;
begin
if str='Serial Number' then
Result:='[Serial Number]'
else if str='physical-off-location' then
Result:='[physical-off-location]'
else if str='Purchase date' then
Result:='[purchase-date]'
else if str='Expiry date' then
Result:='[ma-expiry]'
else if Str='Cost' then
Result:='[ma-fee]'
else if Str='Warranty Provider' then
Result:='[provide-name]'
else if str='Category' then
Result:='[cata-name]'
else if str='Department' then
Result:='[dept-name]'
else if str='Maintenance times' then
Result:='[ma-times]'
else if str='Current Maintenance' then
Result:='Maintenance'
else if str='Memo' then
Result:='[Memo]'
else
result:=str;
end;procedure TSelectFieldForms.FormShow(Sender: TObject);
Var i:Integer;
begin
//
for i:=0 to CheckListbox1.Items.Count-1 do
CheckListBox1.Checked[i]:=true;end;function TSelectFieldForms.GetCheckStr: string;
Var i,j:integer;
Str:String;
begin
j:=0;
for i:=0 to CheckListbox1.Items.Count-1 do
begin
if CheckListBox1.Checked[i]=true then
Inc(j);
end;
if j-1=CheckListbox1.Items.Count-1 then
Result:='*'
else
begin
for i:=0 to CheckListBox1.Items.Count-1 do
begin
if CheckListBox1.Checked[i]=true then
begin
Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+','; //µÃ³ö×Ö¶Î
end;
end; Result:=str;
end;
//»¹Ã»ÓÐдÍê¡£
end;function TSelectFieldForms.GetGenerate(Sqlstr: String): String;
Var Temp1,Temp2,Temp3:string;
begin
if GetcheckStr='*' then
begin
Result:=Sqlstr;
exit;
end;
if Sqlstr<>'' then
begin
if Pos('*',Sqlstr)>0 then
begin
Temp1:=Copy(Sqlstr,0,Pos('*',Sqlstr)-1);
Temp2:=Copy(Sqlstr,(Pos('*',Sqlstr)+2),(Length(Sqlstr)-(Pos('*',Sqlstr)+2)));
Temp3:=Copy(GetCheckStr,0,Length(GetCheckStr)-1);
Result:=Temp1+' '+Temp3+' '+Temp2;
end;
end;
end;procedure TSelectFieldForms.BitBtn1Click(Sender: TObject);
Var SqlStr:string;
begin
Memo1.Clear;
// Memo1.Lines.Add(GetGenerate(dm.MainAllQuery.SQL.Text));
// ShowMessage(GetGenerate(dm.MainAllQuery.SQL.Text)); Sqlstr:=GetGenerate(dm.MainAllQuery.SQL.Text); ShowMessage(Sqlstr);
Memo1.Lines.Add(Sqlstr);
With Dm.MainAllQuery do
begin
Close;
Sql.Clear;
Sql.Add(Sqlstr);
// try
open;
// except
// raise Exception.Create('Can''t open this table');
// end;
end;
end;
1.
//在此處是否需要加一個初始化的動作?不過不加好象也沒有事,
//因為局部變量會自動初始化的
for i:=0 to CheckListBox1.Items.Count-1 do
begin
if CheckListBox1.Checked[i]=true then
begin
Str:=Str+FilterStr(CheckListbox1.Items.Strings[i])+',';
//此處返回時,是不是多了一個","號?
end;
end; Result:=str;
2.在Filterstr過程中,你用的是字符串比較
所以要注意大小寫的問題