请救各位帮帮忙,在线等待啊。(100分)

  • 主题发起人 主题发起人 topdelphi
  • 开始时间 开始时间
T

topdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
我的窗体有三个CHEAKLIST,一个对应部门表的字段CHEAKLIST,
一个是部门里面的小组表CHEAKLIST,
一个是小组里面的人员表CHEAKLIST,从而实现层次关系
,功能是无论当用户在每一个CHEAKlistBOX选择了多小个项目,
都能查询出来,例如同时选择多个部门,之后CHEAKLIST2和CHEAKLIST3就列出所对应小组和
部门出来,我的代码如下,但总是有点出错,望各位指点一下。谢谢。

procedure TForm1.BitBtn1Click(Sender: TObject);
VAR
I,J,K:INTEGER;
begin

FOR I:=0 TO LIST1.Count -1 DO
BEGIN
IF LIST1.Selected THEN
BEGIN
FOR J:=0 TO LIST2.Count -1 DO
BEGIN
FOR K:=0 TO LIST3.COUNT -1 DO
BEGIN
IF LIST3.CHEAK THEN
BEGIN

QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('SELECT * FROM JOBS WHERE '+LIST1.Items.Strings+LIST2.Items.Strings[J]++''''+LIST2.Items.Strings[J]+'''');
QUERY1.Open;

END;
END;
END;
END;

 
有可能与数据库的设计有关,例如主键的问题
 
把 +''''
换成 +#39
试一下

不对,好想你贴的代码有问题啊
 
where 后边的等号哪里去了?
 
程序流程和你的要求不一致,你的程序最后仅能得到最后一组的查询结果。因为每次循环后
query.close。。。query.open都将前次结果喜笑覆盖了。所以不应当使用循环,应当先确定
选中了哪些部门,然后在where的条件中用or将它们连接起来,对小组的情况一样处理,这样
的条件最后可能很复杂,如果可能应尽量优化一下。
 
那照windbell先生所说的,我这条语句应该怎样来实现这个功能呢, 因为我做的也是跟
先生所讲的结果差不多,请问怎样解决呢,谢谢你了,因为我已经无分了,
下次有分定必相送,
 
你的代码有问题;
SQL不应该放在循环里面。
你这样执行的结果永远是最后循环到的那一个。
应该:
s:='select.........where 字段名 in [';
循环里面:s := s +LIST1.Items.Strings+LIST2.Items.Strings[J]
+LIST2.Items.Strings[J]+',' ;
循环结束:s := S + ']';
 
有没有分都惝问题,大家在一起讨论学习。
你仔细考虑一下,其实问题不是很难,只是特别繁琐,一不小心就会出错。不知二你的库表结构
好象你提的问题也不太清楚,所以只能按你上面程序的可亲样式给你一个参考对不对请你斟酌:
QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('SELECT * FROM JOBS'
s:='';
for 部门 do
if 部门选中 then begin
s:=s+'or'+部门条件;
for 小组 do
if 小组是 then begin
s:=s+'or'+小组条件;
for 人员 do
if 人员选中 then
s:=s+'or'+人员条件;
end;
end;
query.sql.add(s);
query.open;
上面还有很多情况没有考虑,如条件是 or 还是 and ,在sL=s+'or'+条件句中,当s=''时or或and是不能加的等等
具体详细编码都要考虑
祝你好运!

 
对不起,:弄影先生,我写的代码如下,但总是出了错,可以详细比点信息我嘛,
小虾比较笨,一时之间头脑转不过来,万份感谢你了。

procedure TForm1.Button1Click(Sender: TObject);
VAR
I,J:INTEGER;
s:string;
sql:string;
begin
s:='select * from fwork where 代号,产品名称,产品型号 IN[' FOR I:=0 TO LIST1.Count -1 DO
S:=S+LIST1.Items.Strings+LIST2.Items.Strings [J]+LIST2.Items.Strings[J]+',';
S:=S+']';
BEGIN
IF LIST1.Selected THEN
BEGIN
FOR J:=0 TO LIST2.Count -1 DO
BEGIN
IF LIST2.Checked[J] THEN
BEGIN

QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('S');
QUERY1.Open;
END;
END;
END;
END;
end;

 
谢谢:windbel先生的回答,现我的代码如下,我在窗体里加多个COMBOBOX,用作选择关系
表达式,但是我的查询语句没有出错,但是查询不出来,到底是怎样问题呢,谢谢。

procedure TForm1.Button1Click(Sender: TObject);
VAR
I,J:INTEGER;
s:string;
sql:string;
begin
QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('SELECT * FROM FWORK');

S:='';
FOR I:=0 TO LIST1.Items.Count -1 DO //部门
IF LIST1.Checked THEN
BEGIN
S:=S+'OR'+COMBOBOX1.Text ;

FOR J:=0 TO LIST2.ItemS.Count -1 DO //小组
IF LIST2.Checked[J] THEN
BEGIN
S:=S+'OR'+COMBOBOX1.Text ;
END;

FOR 人员 DO
.......
END;
QUERY1.SQL.Add('S');
QUERY1.Open;

END;
 
你跟踪一下,看最后生成的s字符串是否是你所希望的,然后再想办法解决
 
现在我把代码改成这样,但是产生了list index out of bounds (3);的错误,应该如何
改正呢,谢谢

procedure TForm1.Button1Click(Sender: TObject);
VAR
I,J:INTEGER;
s:string;
sql:string;
begin
QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('SELECT * FROM FWORK');

S:=''; //代号
IF LIST1.Checked THEN
BEGIN
FOR I:=0 TO LIST1.ItemS.Count -1 DO

BEGIN
S:='S'+list1.Items.Strings+combobox1.Text+''''+list2.Items.Strings[j];
END;
END;
IF LIST2.Checked[J] THEN
BEGIN
FOR J:=0 TO LIST2.ItemS.Count -1 DO //产品名称

BEGIN
S:='S'+list1.Items.Strings+combobox1.Text+''''+list2.Items.Strings[j];
END;
END;

IF 人员 THEN
.............
..........
QUERY1.SQL.Add(S);
QUERY1.Open;

END;
 
S:=''; //代号
IF LIST1.Checked THEN
BEGIN
FOR I:=0 TO LIST1.ItemS.Count -1 DO

BEGIN
S:='S'+list1.Items.Strings+combobox1.Text+''''+list2.Items.Strings[j];
好象没有看到变量j的值是多少
 
现我把语句改成这样,但系统说S附近有语句错误,但是改来改去还是这样子,
请先生再指教一下。
procedure TForm1.Button1Click(Sender: TObject);
VAR
I,J,i1,j1:INTEGER;
s:string;
sql:string;
begin
QUERY1.Close;
QUERY1.SQL.Clear;
QUERY1.SQL.Add('SELECT * FROM FWORK');

S:=''; //代号
FOR I:=0 TO LIST1.Items.Count -1 DO
begin
for j:=0 to list1.Items.Count -1 do
begin
IF LIST1.Checked THEN
BEGIN
S:=s+list1.Items.Strings+combobox1.Text+''''+list1.Items.Strings[j]+'''';
end;

IF LIST2.Checked[J] THEN
BEGIN
S:=s+list1.Items.Strings+combobox1.Text+''''+list2.Items.Strings[j]+'''';
end;
end;

QUERY1.SQL.Add('S');
QUERY1.Open;
 
你可以直接用生成SQL语句的方法呀。
我曾经做过一个这样的程序,不过跟你的不太一样,应该更复杂些。
 
那先生你所说的生成SQL语句在那里生成啊,小虾比较笨些,请问先生可以比点这方面的
例子我吗,真的谢谢你了。
 
这里是我当时实现的源码,因为我没用中文做字段名,所以处理得比较复杂。
===================================
unit Pur_Selection;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, QuickRpt, ExtCtrls, Buttons, ComCtrls,
QRCtrls, QRExport;

type
TForm12 = class(TForm)
QRTextFilter1: TQRTextFilter;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Panel3: TPanel;
Label11: TLabel;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
Bevel4: TBevel;
Label12: TLabel;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
Label13: TLabel;
Bevel5: TBevel;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton15: TSpeedButton;
Label7: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
CheckListBox1: TCheckListBox;
ComboBox4: TComboBox;
ComboBox5: TComboBox;
Edit1: TEdit;
TabSheet3: TTabSheet;
QuickRep1: TQuickRep;
DetailBand1: TQRBand;
PageFooterBand1: TQRBand;
QRShape2: TQRShape;
QRExpr1: TQRExpr;
QRBand1: TQRBand;
QRShape3: TQRShape;
QRShape1: TQRShape;
QRImage1: TQRImage;
QRLabel1: TQRLabel;
QRLabel2: TQRLabel;
QRExpr2: TQRExpr;
QRLabel4: TQRLabel;
QRLabel3: TQRExpr;
SummaryBand1: TQRBand;
QRExpr3: TQRExpr;
QRShape4: TQRShape;
procedure ListBox1DblClick(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SpeedButton7Click(Sender: TObject);
procedure SpeedButton8Click(Sender: TObject);
procedure TabSheet1Show(Sender: TObject);
procedure FlashData;
procedure FormCreate(Sender: TObject);
procedure ComboBox4DropDown(Sender: TObject);
procedure ComboBox5DropDown(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton15Click(Sender: TObject);
procedure SpeedButton9Click(Sender: TObject);
procedure SpeedButton10Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure CreateReport;
procedure FormShow(Sender: TObject);
procedure QRBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
procedure SummaryBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
private
procedure ReFresh;
{ Private declarations }
public
{ Public declarations }
end;

var
Form12: TForm12;
Store:array of String;
List:array of String;
DField:Array of String;
DyList:Array of String;
PRList:array of String;
MOList:Array of String;
TermList:Array of String;
Term:Array of String;
s:String;
Flag:Array[0..7] of Integer;
FList:Array of String;
a:boolean;
OrderList:Array of String;

implementation

uses Pur_DataModule, Pur_Report, Pur_Login;

{$R *.dfm}

procedure TForm12.ListBox1DblClick(Sender: TObject);
begin
if Copy(ListBox1.Items[ListBox1.ItemIndex],5,4)='µ¥' then
ShowMessage('Êý¾ÝÀà±ð²»¿ÉÒÔ±»Ñ¡Ôñ£¡')
else
begin
s:= ListBox1.Items[ListBox1.ItemIndex];
ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
ReFresh;
end;

procedure TForm12.SpeedButton6Click(Sender: TObject);
begin
Close;
end;

procedure TForm12.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TForm12.SpeedButton7Click(Sender: TObject);
begin
if Copy(ListBox1.Items[ListBox1.ItemIndex],5,4)='µ¥' then
ShowMessage('Êý¾ÝÀà±ð²»¿ÉÒÔ±»Ñ¡Ôñ£¡')
else
begin
s:=ListBox1.Items[ListBox1.ItemIndex];
ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
ReFresh;
end;

procedure TForm12.SpeedButton8Click(Sender: TObject);
begin
if ListBox2.ItemIndex<0 then
ShowMessage('ÄãûÓÐÑ¡ÔñÈκÎ×ÊÁÏ£¡')
else
begin
ListBox1.Items.Add(ListBox2.Items[ListBox2.ItemIndex]);
ListBox2.Items.Delete(ListBox2.ItemIndex);
end;
FlashData;
ReFresh;
end;

procedure TForm12.TabSheet1Show(Sender: TObject);
var
i:Integer;
begin
SetLength(Store,ListBox1.Count);
for i:=0 to ListBox1.Count-1 do
Store:=ListBox1.Items.Strings;
end;

procedure TForm12.FlashData;
var
i,j:Integer;
begin
SetLength(List,0);
for i:=0 to Length(Store)-1 do
begin
for j:=0 to ListBox1.Count-1 do
begin
if ListBox1.Items[j]=Store then
begin
SetLength(List,Length(List)+1);
List[Length(List)-1]:=ListBox1.Items[j];
end;
end;
end;
ListBox1.Clear;
for i:=0 to Length(List)-1 do
ListBox1.Items.Add(List);
end;

procedure TForm12.FormCreate(Sender: TObject);
begin
SetLength(DField,42);
DField[0]:='PUR_PRFORM A';
DField[1]:='A.PR_NO';
DField[2]:='A.DEPT_NO';
DField[3]:='A.MATE_ITEM';
DField[4]:='A.MATE_CODE';
DField[5]:='A.QTY';
DField[6]:='A.DATE_ORDER';
DField[7]:='A.DATE_SUPP';
DField[8]:='A.SUPP_CODE';
DField[9]:='A.BUDGETED';
DField[10]:='PUR_POFORM B';
DField[11]:='B.PO_NO';
DField[12]:='B.SUPP_CODE';
DField[13]:='B.PO_DATE';
DField[14]:='B.PR_NO';
DField[15]:='B.MATE_CODE';
DField[16]:='B.UNIT_PRICE';
DField[17]:='B.QTY';
DField[18]:='B.DELI_DATE';
DField[19]:='PUR_QAFORM C';
DField[20]:='C.PR_NO';
DField[21]:='C.PO_NO';
DField[22]:='C.QA_DATE';
DField[23]:='C.MATE_CODE';
DField[24]:='C.REC_QTY';
DField[25]:='C.REF_QTY';
DField[26]:='C.MEMO';
DField[27]:='PUR_RECFORM D';
DField[28]:='D.PR_NO';
DField[29]:='D.PO_NO';
DField[30]:='D.REC_DATE';
DField[31]:='D.MATE_CODE';
DField[32]:='D.REC_QTY';
DField[33]:='D.PRESENT';
DField[34]:='PUR_INVOICE E';
DField[35]:='E.IN_NO';
DField[36]:='E.IN_DATE';
DField[37]:='E.PO_NO';
DField[38]:='E.MATE_CODE';
DField[39]:='E.QTY';
DField[40]:='E.UNIT_PRICE';
DField[41]:='E.SUPP_CODE';
SetLength(FList,42);
FList[0]:='PUR_PRFORM A';
FList[1]:='Ç빺µ¥ºÅ';
FList[2]:='³É±¾ÖÐÐÄ';
FList[3]:='²ÄÁÏÀà±ð';
FList[4]:='²ÄÁÏ´úºÅ';
FList[5]:='Ç빺ÊýÁ¿';
FList[6]:='Ç빺ÈÕÆÚ';
FList[7]:='ÐèÇóÆÚÏÞ';
FList[8]:='¹©Ó¦ÉÌ';
FList[9]:='Ô¤Ëã';
FList[10]:='PUR_POFORM B';
FList[11]:='¶©¹ºµ¥ºÅ';
FList[12]:='¹©Ó¦ÉÌ';
FList[13]:='¶©¹ºÈÕÆÚ';
FList[14]:='Ç빺µ¥ºÅ';
FList[15]:='²ÄÁÏ´úºÅ';
FList[16]:='µ¥¼Û';
FList[17]:='¶©¹ºÊýÁ¿';
FList[18]:='½»»õÆÚÏÞ';
FList[19]:='PUR_QAFORM C';
FList[20]:='Ç빺µ¥ºÅ';
FList[21]:='¶©¹ºµ¥ºÅ';
FList[22]:='¼ìÑéÈÕÆÚ';
FList[23]:='²ÄÁÏ´úºÅ';
FList[24]:='ÑéÊÕÊý';
FList[25]:='¾ÜÊÕÊý';
FList[26]:='¾ÜÊÕÔ­Òò';
FList[27]:='PUR_RECFORM D';
FList[28]:='Ç빺µ¥ºÅ';
FList[29]:='¶©¹ºµ¥ºÅ';
FList[30]:='ÑéÊÕÈÕÆÚ';
FList[31]:='²ÄÁÏ´úºÅ';
FList[32]:='ÑéÊÕÊýÁ¿';
Flist[33]:='ÔùËÍÊýÁ¿';
FList[34]:='PUR_INVOICE E';
FList[35]:='·¢Æ±ºÅÂë';
FList[36]:='·¢Æ±ÈÕÆÚ';
FList[37]:='¶©¹ºµ¥ºÅ';
FList[38]:='²ÄÁÏ´úºÅ';
FList[39]:='ÊýÁ¿';
FList[40]:='µ¥¼Û';
FList[41]:='¹©Ó¦ÉÌ';
end;


procedure TForm12.ComboBox4DropDown(Sender: TObject);
var
i:Integer;
XSQL:String;
Str:String;
begin
SetLength(Term,CheckListBox1.Count+1);
XSQL:='SELECT';
if ListBox2.ItemIndex<0 then
ShowMessage('ÇëÑ¡ÔñÒª²éѯµÄÀ¸Î»!')
else
begin
for i:=0 to Length(Store) -1 do
begin
if ListBox2.Items[ListBox2.ItemIndex]=Store then
begin
Str:=Copy(DField,3,Length(DField)-2);
Term[Length(Term)-1]:=DField;
XSQL:=XSQL+' '+DField;
if i<10 then
XSQL:=XSQL+' '+'FROM '+' '+Dfield[0]
else if (i<19) and (i>10) then
XSQL:=XSQL+' '+'FROM ' + ' ' +Dfield[10]
else if (i<27) and (i>19) then
XSQL:=XSQL+' '+'FROM '+' '+Dfield[19]
else if (i<34) and (i>27) then
XSQL:=XSQL+' '+'FROM '+' '+Dfield[27]
else if (i>34) then
XSQL:=XSQL+' '+'FROM '+' '+DField[3];
end;
end;
XSQL:=XSQL+' GROUP BY '+Str;
Pur_DM1.Pur_ADS1.Close;
Pur_DM1.Pur_ADS1.CommandText:=XSQL;
Pur_DM1.Pur_ADS1.Open;
Combobox4.Clear;
Pur_DM1.Pur_ADS1.First;
while not Pur_DM1.Pur_ADS1.Eof do
begin
ComboBox4.Items.Add(Pur_DM1.Pur_ADS1.FieldByName(Str).AsString);
Pur_DM1.Pur_ADS1.Next;
end;
end;
end;

procedure TForm12.ComboBox5DropDown(Sender: TObject);
begin
ComboBox5.Items:=ComboBox4.Items;
end;

procedure TForm12.SpeedButton1Click(Sender: TObject);
var
i:Integer;
j:Integer;
begin
if (ComboBox4.Text<>'') and (ComboBox5.Text<>'') then
begin
SetLength(TermList,CheckListBox1.Count+1);
SetLength(OrderList,CheckListBox1.Count+1);
CheckListBox1.Items.Add(Trim(ListBox2.Items[ListBox2.ItemIndex])+' From'+' '+Trim(ComboBox4.Text)+' '+'To'+' '+Trim(ComboBox5.Text));
for i:=0 to Length(TermList)-1 do
begin
TermList[Length(TermList)-1]:=Term+'>='+''''+Trim(ComboBox4.Text)+''''+' AND '+Term+'<='+''''+Trim(ComboBox5.Text)+'''';
OrderList[Length(OrderList)-1]:=Term;
end;
if CheckListBox1.Count>1 then
begin
TermList[Length(TermList)-1]:='AND '+TermList[Length(TermList)-1];
OrderList[Length(OrderList)-1]:=OrderList[Length(OrderList)-1];
end;
end
else
begin
ShowMessage('ÇëÑ¡ÔñÊý¾Ý·¶Î§');
ComboBox4.SetFocus;
end;
end;

procedure TForm12.SpeedButton15Click(Sender: TObject);
begin
if CheckListBox1.ItemIndex>-1 then
begin
TermList[CheckListBox1.ItemIndex]:='';
Term[CheckListBox1.ItemIndex]:='';
CheckListBox1.Items.Delete(CheckListBox1.ItemIndex);
end
else
ShowMessage('ÄãûÓÐÑ¡ÔñÈκÎÊý¾Ý!');
end;

procedure TForm12.SpeedButton9Click(Sender: TObject);
var
i:Integer;
begin
SetLength(MOList,Length(PRList));
for i:=0 to Length(PRList)-1 do
MOList:=PRList;
if ListBox2.ItemIndex>0 then
begin
for i:=0 to Length(PRList)-1 do
begin
if Trim(ListBox2.Items[ListBox2.ItemIndex])=Trim(PRList) then
begin
MOList:=PRlist[i-1];
MOList[i-1]:=PRList;
s:=MOList[i-1];
end;
end;
ListBox2.Clear;
for i:=0 to Length(MOList)-1 do
ListBox2.Items.Add(MOList);
end
else if ListBox2.ItemIndex=0 then
ShowMessage(ListBox2.Items[ListBox2.ItemIndex]+'ÒÑλÓÚµÚһλ!')
else if ListBox2.ItemIndex<0 then
ShowMessage('ÄãûÓÐÑ¡ÔñÈκÎ×ÊÁÏ!');
ReFresh;
end;

procedure TForm12.ReFresh;
var
i:Integer;
begin
SetLength(PRList,ListBox2.Count);
for i:=0 to ListBox2.Count-1 do
begin
PrList:=ListBox2.Items.Strings;
end;
for i:=0 to ListBox2.Count-1 do
begin
if s=ListBox2.Items.Strings then
begin
ListBox2.Selected:=True;
end;
end;
end;

procedure TForm12.SpeedButton10Click(Sender: TObject);
var
i:Integer;
begin
SetLength(MOList,Length(PRList));
for i:=0 to Length(PRList)-1 do
MOList:=PRList;
if ListBox2.ItemIndex<ListBox2.Count-1 then
begin
for i:=0 to Length(PRList)-1 do
begin
if Trim(ListBox2.Items[ListBox2.ItemIndex])=Trim(PRList) then
begin
MOList:=PRlist[i+1];
MOList[i+1]:=PRList;
s:=MOList[i+1];
end;
end;
ListBox2.Clear;
for i:=0 to Length(MOList)-1 do
ListBox2.Items.Add(MOList);
end
else if ListBox2.ItemIndex=ListBox2.Count-1 then
ShowMessage(ListBox2.Items[ListBox2.ItemIndex]+'ÒÑλÓÚ×îºóһλ!')
else if ListBox2.ItemIndex<0 then
ShowMessage('ÄãûÓÐÑ¡ÔñÈκÎ×ÊÁÏ!');
ReFresh;
end;

procedure TForm12.SpeedButton5Click(Sender: TObject);
var
XSQL:String;
i:Integer;
j:Integer;
str:String;
begin
QRExpr1.Expression:='';
for i:=0 to 7 do
Flag:=0;
XSQL:='';
if ListBox2.Count<>0 then
begin
for j:=0 to Length(PRList)-1 do
begin
for i:=0 to Length(Store)-1 do
begin
if PRList[j]=Store then
begin
if XSQL='' then
XSQL:='SELECT '+DField+' '+FList+' '
else
XSQL:=XSQL+','+DField+' '+FList+' ';
if i=2 then
XSQL:=XSQL+',F.DEPT_NAME ÐèÇó²¿ÃÅ ';
if i in [4,15,23,31,38] then
XSQL:=XSQL+',G.MATE_NAME ²ÄÁÏÃû³Æ ,G.MATE_UNIT µ¥Î» ';
if i in [12,41] then
XSQL:=XSQL+',H.SUPP_NAME ¹©Ó¦ÉÌÃû³Æ ';
end;
end;
end;
XSQL:=XSQL+'FROM ';
for i:=0 to Length(Store)-1 do
begin
for j:=0 to ListBox2.Count-1 do
begin
if PRList[j]=Store then
begin
if (i<10) and (Flag[0]=0) and (Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+DField[0]+' ';
Flag[0]:=1;
end
else if (i<10) and (Flag[0]=0) and (Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+','+DField[0]+' ';
Flag[0]:=1;
end;
if (i<19) and (i>10) and (Flag[1]=0) and (Flag[0]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+DField[10]+' ';
Flag[1]:=1;
end
else if (i<19) and (i>10) and (Flag[1]=0) and (Flag[0]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+','+DField[10]+' ';
Flag[1]:=1;
end;
if (i<27) and (i>19) and (Flag[2]=0) and (Flag[0]+Flag[1]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+Dfield[19]+' ';
Flag[2]:=1;
end
else if (i<27) and (i>19) and (Flag[2]=0) and (Flag[0]+Flag[1]+Flag[3]+Flag[4]+Flag[5]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+','+Dfield[19]+' ';
Flag[2]:=1;
end;
if (i<34) and (i>27) and (Flag[3]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[4]+Flag[5]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+DField[27]+' ';
Flag[3]:=1;
end
else if (i<34) and (i>27) and (Flag[3]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[4]+Flag[5]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+','+DField[27]+' ';
Flag[3]:=1;
end;
if (i>34) and (Flag[4]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[5]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+DField[34]+' ';
Flag[4]:=1;
end
else if (i>34) and (Flag[4]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[5]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+','+DField[34]+' ';
Flag[4]:=1;
end;

if (i=2) and (Flag[5]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[6]+Flag[7]=0) then
begin
XSQL:=XSQL+' PUR_DEPT F ';
Flag[5]:=1;
end
else if (i=2) and (Flag[5]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[6]+Flag[7]>0) then
begin
XSQL:=XSQL+' , PUR_DEPT F ';
Flag[5]:=1;
end;
if (i in [4,15,23,31,38]) and (Flag[6]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[7]=0) then
begin
XSQL:=XSQL+' PUR_MATERIAL G ';
Flag[6]:=1;
end
else if (i in [4,15,23,31,38]) and (Flag[6]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[7]>0) then
begin
XSQL:=XSQL+' , PUR_MATERIAL G ';
Flag[6]:=1;
end;
if (i in [12,40]) and (Flag[7]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]=0) then
begin
XSQL:=XSQL+' PUR_SUPPLIER H ';
Flag[7]:=1;
end
else if (i in [12,41]) and (Flag[7]=0) and (Flag[0]+Flag[1]+Flag[2]+Flag[3]+Flag[4]+Flag[5]+Flag[6]>0) then
begin
XSQL:=XSQL+' , PUR_SUPPLIER H ';
Flag[7]:=1;
end;
end;
end;
end;
if (Flag[1]>0) and (Flag[0]>0) then
STR:=STR+' AND '+DField[14]+'='+DField[1]+' AND '+ DField[15]+'='+DField[4];
if (Flag[2]>0) and (Flag[0]>0) then
STR:=STR+' AND '+DField[20]+'='+DField[1]+' AND '+ DField[23]+'='+DField[4];
if (Flag[3]>0) and (Flag[0]>0) then
STR:=STR+' AND '+DField[28]+'='+DField[1]+' AND '+ DField[31]+'='+DField[4];
if (Flag[2]>0) and (Flag[1]>0) then
STR:=STR+' AND '+DField[20]+'='+DField[14]+' AND '+ DField[21]+'='+DField[11]+' AND '+ DField[23]+'='+DField[15];
if (Flag[3]>0) and (Flag[1]>0) then
STR:=STR+' AND '+DField[28]+'='+DField[14]+' AND '+ DField[29]+'='+DField[11]+' AND '+ DField[31]+'='+DField[15];
if (Flag[3]>0) and (Flag[2]>0) then
STR:=STR+' AND '+DField[28]+'='+DField[20]+' AND '+ DField[29]+'='+DField[21]+' AND '+ DField[31]+'='+DField[23];
if (Flag[4]>0) and (Flag[1]>0) then
STR:=STR+' AND '+DField[36]+'='+DField[11]+' AND '+ DField[38]+'='+DField[14];
if (Flag[4]>0) and (Flag[2]>0) then
STR:=STR+' AND '+DField[36]+'='+DField[21]+' AND '+ DField[38]+'='+DField[23];
if (Flag[4]>0) and (Flag[3]>0) then
STR:=STR+' AND '+DField[36]+'='+DField[29]+' AND '+ DField[38]+'='+DField[31];
if Flag[5]>0 then
STR:=STR+' AND F.DEPT_NO=A.DEPT_NO ';
if (Flag[6]>0) and (Flag[0]>0) then
STR:=STR+' AND G.MATE_CODE=A.MATE_CODE ';
if (Flag[6]>0) and (Flag[1]>0) then
STR:=STR+' AND G.MATE_CODE=B.MATE_CODE ';
if (Flag[6]>0) and (Flag[2]>0) then
STR:=STR+' AND G.MATE_CODE=C.MATE_CODE ';
if (Flag[6]>0) and (Flag[3]>0) then
STR:=STR+' AND G.MATE_CODE=D.MATE_CODE ';
if (Flag[6]>0) and (Flag[4]>0) then
STR:=STR+' AND G.MATE_CODE=E.MATE_CODE ';
if (Flag[7]>0) and (Flag[1]>0) then
STR:=STR+' AND H.SUPP_CODE=B.SUPP_CODE ';
if (Flag[7]>0) and (Flag[4]>0) then
STR:=STR+' AND H.SUPP_CODE=E.SUPP_CODE ';
if (CheckListBox1.Count>0) or (STR>'') then
XSQL:=XSQL+' WHERE ';
for j:=0 to CheckListBox1.Count-1 do
XSQL:=XSQL+' '+TermList[j];
if STR>'' then
begin
if Trim(Copy(XSQL,Length(XSQL)-6,8))<>'WHERE' then
XSQL:=XSQL+' '+STR
else
XSQL:=XSQL+' '+Copy(STR,5,Length(STR));
end;
XSQL:=XSQL+' GROUP BY ';
for j:=0 to Length(PRList)-1 do
begin
for i:=0 to Length(Store)-1 do
begin
if PRList[j]=Store then
begin
if Trim(Copy(XSQL,Length(XSQL)-3,4))='BY' then
XSQL:=XSQL+DField
else
XSQL:=XSQL+','+DField;
if i=2 then
XSQL:=XSQL+',F.DEPT_NAME ';
if i in [4,15,23,31,38] then
XSQL:=XSQL+',G.MATE_NAME ,G.MATE_UNIT ';
if i in [12,41] then
XSQL:=XSQL+',H.SUPP_NAME ';
end;
end;
end;
for i:=0 to CheckListBox1.Count-1 do
begin
if CheckListBox1.Checked then
begin
if Trim(Copy(XSQL,Length(XSQL)-2,2))<>'BY' then
XSQL:=XSQL+' ORDER BY ';
end;
end;
for i:=0 to CheckListBox1.Count-1 do
begin
if CheckListBox1.Checked then
begin
if Trim(Copy(XSQL,Length(XSQL)-2,2))='BY' then
XSQL:=XSQL+' '+OrderList
else
XSQL:=XSQL+','+OrderList;
end;
end;
if Edit1.Text='' then
begin
if Edit1.Text<>'' then
QuickRep1.ReportTitle:=Edit1.Text
else
QuickRep1.ReportTitle:=' ²é ѯ ½á ¹û ';
if MessageDlg('ÄãÖ»ÊÇÒª½øÐвéѯ¶ø²»´òÓ¡Âð?Èç¹ûÄãÒª´òÓ¡,ÇëÊäÈ뱨±íÃû³Æ!', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
// ComboBox4.Text:=XSQL;
Pur_DM1.Pur_ADS2.Close;
Pur_DM1.Pur_ADS2.CommandText:=XSQL;
Pur_DM1.Pur_ADS2.Open;
if Pur_DM1.Pur_ADS2.RecordCount>0 then
begin
with TForm12.Create(Application) do
try
CreateReport;
finally
Release;
end;
end
else
ShowMessage('ûÓÐÈκÎÄãÒª²éѯͳ¼ÆµÄ×ÊÁÏ!');
end;
end
else
begin
// ComboBox4.Text:=XSQL;
Pur_DM1.Pur_ADS2.Close;
Pur_DM1.Pur_ADS2.CommandText:=XSQL;
Pur_DM1.Pur_ADS2.Open;
if Pur_DM1.Pur_ADS2.RecordCount>0 then
begin
with TForm12.Create(Application) do
try
CreateReport;
finally
Release;
end;
end
else
ShowMessage('ûÓÐÈκÎÄãÒª²éѯͳ¼ÆµÄ×ÊÁÏ!');
end;
end
else
ShowMessage('ÄãûÓÐÑ¡ÔñÒª²éѯµÄÈκÎ×ÊÁÏ!');
end;

procedure TForm12.CreateReport;
var
QRLalist:Array of TQRLabel;
QRTeList:Array of TQRDBText;
Count:Integer;
i,k:Integer;
begin
Count:=Pur_DM1.Pur_ADS2.FieldCount;
SetLength(QRLaList,Count);
SetLength(QRTeList,Count);
for i:=0 to high(QRTeList) do
begin
k:=0;
QRLaList:=TQRLabel.Create(self);
if i=0 then
QRLaList.Left:=38
else
QRLaList.Left:=QRLaList[i-1].Left+QRLaList[i-1].Width+12;
QRLaList.Parent:=QuickRep1;
QRLaList.AutoSize:=False;
Pur_DM1.Pur_ADS2.First;
while not Pur_DM1.Pur_ADS2.Eof do
begin
if Pur_DM1.Pur_ADS2.Fields.DataSize>k then
k:=Pur_DM1.Pur_ADS2.Fields.DataSize;
Pur_DM1.Pur_ADS2.Next;
end;
QRLaList.Caption:=Pur_DM1.Pur_ADS2.Recordset.Fields.Name;
QRLaList.Top:=130;
if Pur_DM1.Pur_ADS2.FieldCount>12 then
begin
QRLaList.Font.Size:=7;
QRLaList.Width:=K*2+20;
end
else
begin
QRLaList.Font.Size:=8;
QRLaList.Width:=K*3+20;
end;
QRTeList:=TQRDBText.Create(self);
QRTeList.Parent:=DetailBand1;
QRTeList.DataSet:=Pur_DM1.Pur_ADS2;
QRTeList.DataField:=Pur_DM1.Pur_ADS2.Recordset.Fields.Name;
QRTeList.AutoSize:=True;
QRTeList.Font.Size:=QRLaList.Font.Size;
QRTeList.Top:=10;
QRTeList.Left:=QRLaList.Left-38;
end;
if Pur_DM1.Pur_ADS2.RecordCount/24<>Pur_DM1.Pur_ADS2.RecordCount div 24 then
QRExpr1.Expression:='''Page ''+'+'PAGENUMBER'+'+'' of ''+'+''''+FloatToStr(Pur_DM1.Pur_ADS2.RecordCount/24-(Pur_DM1.Pur_ADS2.RecordCount / 24-Pur_DM1.Pur_ADS2.RecordCount div 24)+1)+''''
else
QRExpr1.Expression:='''Page ''+'+'PAGENUMBER'+'+'' of ''+'+''''+IntToStr(Pur_DM1.Pur_ADS2.RecordCount div 24)+'''';
QuickRep1.Preview;
end;

procedure TForm12.FormShow(Sender: TObject);
begin
TabSheet3.TabVisible:=False;
end;

procedure TForm12.QRBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
var
XSQL:String;
begin
XSQL:='SELECT USER_CNAME FROM PUR_PURVIEW WHERE USER_NAME='+''''+Form2.Edit1.Text+'''';
Pur_DM1.Pur_ADS1.Close;
Pur_DM1.Pur_ADS1.CommandText:=XSQL;
Pur_DM1.Pur_ADS1.Open;
QRLabel4.Caption:='ÖƱí : '+Pur_DM1.Pur_ADS1.FieldByName('USER_CNAME').AsString;
QRLabel3.Left:=(Screen.Width) div 2;
end;

procedure TForm12.SummaryBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
begin
if QRExpr3.Value.strResult='' then
QRShape4.Pen.Color:=clWhite;
end;

end.
 
先生,你也在利害了,写了这么多的代码啊,但是小虾真的看到头昏了,请问先生,如果
换着我上面的语句,应该怎样修改啊,不知怎样回报,只能真心的
谢谢你了,
 
我在前面说过:如条件是 or 或 and ,在s=s+'or'+条件句中,当s=''时or或and是不能加
的等等,同样你的语句
S:='S'+list1.Items.Strings+combobox1.Text+''''+list2.Items.Strings[j];
在循环中第一次时正确,因s='',但除第一次名其它的就不正确了如第二次相当于:
S:=list1.Items.Strings[0]+combobox1.Text+''''+list2.Items.Strings[0]+
list1.Items.Strings[1]+combobox1.Text+''''+list2.Items.Strings[1];
可以看到中间没有条条件连接,
另外累加时你的s是不用's'的
 
那如果我将这几个CHEAKLISTBOX,改成例如将数据库的字段读入到CHEAKLISTBOX1里
,当选择CHEAKLISTBOX1里面的某个或多个字段之后,将数据库的记录读入到CHEAKLISTBOX2
里,如果用户在CHEAKLISTBOX1选择多个字段,或在CHEAKLISTBOX2选择多个记录的时候,
都能查询出来将数据显示在DBGRID里,请问如何实现啊这个功能啊,请给一个详细的例子,
真的谢谢各位了
 

Similar threads

I
回复
0
查看
452
import
I
I
回复
0
查看
611
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部