Re: Validate an active record.


Posted by webmaster Guido on June 08, 2000 at 15:20:46:

In Reply to: Validate an active record. posted by Hung on June 03, 2000 at 00:05:19:

: I attempted to validate an active record data when a user tries to post the active record to the database by scrolling to another record or hit the "commit" button on the DBNavigation bar. But I ran into the problem of not being able to abort the post action when one or more data field values is not valid.
: I used the TTable.BeforePost event to do my validation and called the Abort to cancel out the post action but it did not work.
------------

The examples below have been tested by myself under Delphi 4. Please let me know if it doesn't work for you.

The simplest example will result in blocking the Post, so the table stays in Edit or in Append mode:

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table1.FieldByName('ProductID').AsInteger < 0 then
    Abort;
end;

Because this is not very friendly towards the user, better tell him what is wrong:

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table1.FieldByName('ProductID').AsInteger < 0 then begin
    MessageDlg('ProductID must be positive.', mtWarning, [mbOK], 0);
    Abort;
  end;
end;

The last example gives the user the choice between trying again or cancelling the operation:

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table1.FieldByName('ProductID').AsInteger < 0 then begin
    if MessageDlg('ProductID must be positive. Try again?', 
       mtConfirmation, [mbYes, mbNo], 0) = mrNo then
      Table1.Cancel;
    Abort;
  end;
end;


Related Articles and Replies: