adoquery.locate是否有可能定位不到实际存在的数据?
本帖最后由 pengchao025 于 2012-12-26 09:50:47 编辑 本人用ACCESS做了一个数据库,现在程序上有个功能是删除里面的一条数据,用条码枪刷条码的方式,条码号ID是关键字。代码如下:
procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then
begin
if ADOQuery1.Locate('id',Edit7.Text,[loCaseInsensitive]) then
begin
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Text:='delete from mjkc where id='+''''+Edit7.Text+'''';
ADOQuery1.ExecSQL;
ADOQuery1.Close;
ADOQuery1.SQL.Text:='select *from mjkc';
ADOQuery1.Open;
end
else
ShowMessage('没有 '+Edit7.Text+' 这条记录!');
Edit7.Clear;
end;
end;
可是问题来了,有些数据ID是存在的,但是删除时用locate却定位不到,就提示没有这条记录,删除失败,我就有点怀疑可能有问题,就用查询语句去查,却又查到了这条ID,然后在用上述方式删除,就可以删除了。感觉像是之前删除没有这条数据,用了查询语句后就把这条ID添加上去了似的。查询代码如下,是多个条件查询,我只选择了其中的ID。
procedure TForm1.Button12Click(Sender: TObject);
var
tjid,tjlotno,tjbanbie,tjriqi,tj:string;
begin
if form3.ShowModal=mrok then
begin
tjlotno:='1=1';
tjid:='1=1';
tjbanbie:='1=1';
tjriqi:='1=1';
if Form3.Edit1.Text<>'' then //按ID
tjid:=' id like '''+'%'+Form3.Edit1.Text+'%'+'''';
if Form3.Edit2.Text<>'' then
tjlotno:=' 制令 like '''+'%'+Form3.Edit2.Text+'%'+'''';
if Form3.ComboBox1.Text<>'' then
tjbanbie:=' 班别='''+Form3.ComboBox1.Text+'''';
if Form3.CheckBox1.Checked then
tjriqi:=' 入站日期>='+'#'+datetostr(Form3.DateTimePicker1.Date)+'# and '+'入站日期<='+'#'+datetostr(Form3.DateTimePicker2.Date)+'#';
tj:='select *from mjkc where '+tjid+' and '+tjlotno+' and '+tjbanbie+' and '+tjriqi;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.add(tj);
ADOQuery1.Open;
Form3.Edit1.Clear;
Form3.Edit2.Clear;
end;
end;
数据比较多,有几万条吧,删除的时候定位不到是个别现象,有些ID的确不存在,查询的时候也查询不到。但是就那个别的ID定位不到,查询的到。
求高人指点下啊,哪里有问题、、
[解决办法]
执行这句前
if ADOQuery1.Locate('id',Edit7.Text,[loCaseInsensitive]) then
先看一下
1 确认ADOQuery1的查询结果中有没有你要删除的ID
2 有没有使用过滤器(filtered相关), 如果使用了,并过滤了Edit7.Text的ID, 就查不到了