主题:请教高手
我想把listbox里面的(假如)2条记录,添加到数据库中去
执行时,错误出现在红色字体部分
错误“list index out of bounds(2)"
procedure Tchangeform.BitBtn5Click(Sender: TObject);
var i:integer;
str1,xh:string;//xh学号
xm:string;//xm姓名
begin
i:=listbox2.Items.Count;//listbox2里面的记录数
if listbox2.Items.Text='' then showmessage('请先选择符合条件的人');
if messagedlg('确认添加这'+inttostr(i)+'条记录吗?',mtconfirmation,[mbyes,mbno],0)=mryes then
begin
[color=FF0000]xm:=listbox2.Items[i];[/color]//把里面的姓名赋给xm
str1:='update t_info set zt="2" where xm="'+xm+'" ';//更新表t_info
for i:=0 to listbox2.Items.Count-1 do//循环把值添加到数据库中
begin
with tquery.Create(nil) do
begin
try
close;
databasename:='dbdemos';
sql.Clear;
sql.Add(str1);
prepare;
execsql;
sql.Clear;
sql.Add('select xh from t_info where xm="'+xm+'"');//查询出学号
xh:=fieldbyname('xh').AsString;把学号赋值给xh
sql.Add('insert into t_process values(:xh,"2",:fzsj)');//添加记录到表t_process
parambyname('xh').AsString:=xh;
parambyname('fzsj').AsDateTime:=datetimepicker1.DateTime;
finally
free;
end;
end;
showmessage('成功转为预备党员!');
listbox2.Items.Clear;
end;
end;
end;
执行时,错误出现在红色字体部分
错误“list index out of bounds(2)"
procedure Tchangeform.BitBtn5Click(Sender: TObject);
var i:integer;
str1,xh:string;//xh学号
xm:string;//xm姓名
begin
i:=listbox2.Items.Count;//listbox2里面的记录数
if listbox2.Items.Text='' then showmessage('请先选择符合条件的人');
if messagedlg('确认添加这'+inttostr(i)+'条记录吗?',mtconfirmation,[mbyes,mbno],0)=mryes then
begin
[color=FF0000]xm:=listbox2.Items[i];[/color]//把里面的姓名赋给xm
str1:='update t_info set zt="2" where xm="'+xm+'" ';//更新表t_info
for i:=0 to listbox2.Items.Count-1 do//循环把值添加到数据库中
begin
with tquery.Create(nil) do
begin
try
close;
databasename:='dbdemos';
sql.Clear;
sql.Add(str1);
prepare;
execsql;
sql.Clear;
sql.Add('select xh from t_info where xm="'+xm+'"');//查询出学号
xh:=fieldbyname('xh').AsString;把学号赋值给xh
sql.Add('insert into t_process values(:xh,"2",:fzsj)');//添加记录到表t_process
parambyname('xh').AsString:=xh;
parambyname('fzsj').AsDateTime:=datetimepicker1.DateTime;
finally
free;
end;
end;
showmessage('成功转为预备党员!');
listbox2.Items.Clear;
end;
end;
end;