if adodataset.Locate('CpCode','01#',[]) then MsgDlg(‘已经存在该纪录’);
自己遍历一下吧,效率可能就比较低了//你这个问题可能是因为数据有空格的原因,自己遍历比较一下看看行不行 function LocateEx(DataSet: TDataSet; Field, Value: String): Boolean; var BK: TBookStr; AField: TField; begin Result := False; if DataSet.IsEmpty then Exit; AField := DataSet.FindField(Field) if AField = nil then Exit; DataSet.DisableControls; try BK := DataSet.Book; DataSet.First; Value := Trim(Value); while not DataSet.Eof do begin if SameText(Trim(AField.AsString), Value) then begin Result := True;//找到相应记录就退出吧 Exit; end; DataSet.Next; end; DataSet.Book := BK;//没有找到就把数据集滚到原来的位置 finally DataSet.EnableControls; end; end;
使用这句还是不能定位
if adodataset.Locate('CpCode','01#',[]) then MsgDlg(‘已经存在该纪录’);
function LocateEx(DataSet: TDataSet; Field, Value: String): Boolean;
var
BK: TBookStr;
AField: TField;
begin
Result := False;
if DataSet.IsEmpty then Exit; AField := DataSet.FindField(Field)
if AField = nil then Exit; DataSet.DisableControls;
try
BK := DataSet.Book;
DataSet.First;
Value := Trim(Value);
while not DataSet.Eof do
begin
if SameText(Trim(AField.AsString), Value) then
begin
Result := True;//找到相应记录就退出吧
Exit;
end;
DataSet.Next;
end;
DataSet.Book := BK;//没有找到就把数据集滚到原来的位置
finally
DataSet.EnableControls;
end;
end;