你可以利用table的PostError属性
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
eKeyViol = 9729;
implementation
{$R *.DFM}
procedure TForm1.Query1PostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Errors
[0].Errorcode = eKeyViol then
begin
MessageDlg('出现重复记录!',
mtWarning, [mbOK], 0);
query1.Cancel;
Abort;
end;
end;