EPUB | CHM | PDF

Example: BeforeInsert, Insert, AsInteger, FieldByName

Top Previous Next

This example uses the BeforeInsert event to do data validation; if the StrToInt function raises an exception, the edit control's contents are set to a valid value so the assignment to the INTEGER field in the table will succeed.

procedure TForm1.PSQLTable1BeforeInsert(DataSet: TDataSet);
begin
  try
  // Make sure edit field can be converted to integer --
  // this will raise an exception if it can't
    StrToInt(Edit1.Text);
  except
    Edit1.Text := '0';
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  PSQLTable1.Insert;
  PSQLTable1.FieldByName('QUANTITY').AsInteger := StrToInt(Edit1.Text);
  PSQLTable1.Post;
end;