我把源码给你,你帮我看一下,谢谢!
procedure TfrmExtract1.Button1Click(Sender: TObject);
var
sGroupId, sSearch, sReturn :string;
iOptioncount,IExtracount :integer;
tempArray1: array of integer;
i :integer;
begin
sReturn :='';
sSearch :='';
GsSearch :='';
//浏览ExpertGroup表
ADOTable1.First;
while not ADOTable1.Eof do
begin
SGroupId :=ADOTable1.FieldValues['组ID'];
IOptioncount :=ADOTable1.FieldValues['总人数'];
IExtracount :=ADOTable1.FieldValues['抽选人数'];
if IExtracount <=0 then
begin
ADOTable1.Next;
continue; // 抽选人数为0,则跳出本次循环
end;
if IExtracount > IOptioncount then
begin
Application.MessageBox('抽选人数不能大于总人数, 请重新输入. ', '提示信息', MB_OK);
exit;
end;
//计算出每组选中的专家总人数
with ADOQuery1 do begin
Close;
Connection :=frmMain.ADOConnection1;
SQL.Clear;
SQL.Add('select * from Expert where 组ID like :GroupID and 选中= True and 状态= :Status Order by 组ID asc,专家ID asc');
Parameters.ParamByName('GroupID').Value := SGroupId;
Parameters.ParamByName('Status').Value := '正常';
Prepared :=true;
Open; //select语句需用Open方法执行
end;
if IExtracount > ADOQuery1.RecordCount then
begin
Application.MessageBox('抽选人数不能大于该组选中的专家总人数, 请重新输入. ', '提示信息', MB_OK);
exit;
end;
setlength(TempArray1,IExtracount); //根据抽选人数为数组分配内存空间
//调用抽取函数, ADOQuery1.RecordCount是有效的专家总人数
sReturn :=ExtractFunc(ADOQuery1.RecordCount,IExtracount,TempArray1);
for i :=low(TempArray1) to high(TempArray1) do
begin
ADOQuery1.first;
ADOQuery1.MoveBy(TempArray1-1);
//构造查询字符串
sSearch :=sSearch + ''''+ ADOQuery1.FieldValues['专家ID'] + ''',';
GsSearch := GsSearch + '专家ID = ' + QuotedStr(ADOQuery1.FieldValues['专家ID']) + ' OR ';
end;
ADOTable1.Next;
end;
ADOTable1.First; //完成浏览ExpertGroup表
tempArray1 :=nil; //释放临时数组内存空间
if sSearch = '' then //如果没有构造查询字符串,则构造
begin
sSearch :='''' + '''';
GsSearch :='专家ID = ' + QuotedStr('')
end
else begin
sSearch :=copy(sSearch,1,length(sSearch)-1); //去掉结尾的逗号
GsSearch :=copy(GsSearch,1,length(GsSearch)-3); //去掉结尾的OR
end;
sSearch := 'select * from Expert where 专家ID in (' + sSearch + ') ' +
' Order by 组ID asc,专家ID asc';
//创建抽取结果窗口
frmExtractResult :=TfrmExtractResult.Create(Application);
with frmExtractResult.ADOQryResult do begin
Close;
SQL.Clear;
SQL.Add(sSearch);
Prepared :=true;
Open; //select语句需用Open方法执行
end;
frmExtractResult.ShowModal;
frmExtract1Rep :=TfrmExtract1Rep.Create(Application);
with frmExtract1Rep.ADOTable1 do begin
Close;
Connection:=frmMain.ADOConnection1;
TableName:='Expert'; //数据表名称!
Filtered := False;
Filter :=GsSearch ;
Filtered :=true;
Open; //打开数据库表
end;
button3.Enabled :=true;end;