Deleting Records

From RAD Studio
Jump to: navigation, search

Go Up to Modifying Data

Use the Delete method to delete the current record in an active dataset. When the Delete method is called,

  • The dataset receives a BeforeDelete event.
  • The dataset attempts to delete the current record.
  • The dataset returns to the dsBrowse state.
  • The dataset receives an AfterDelete event.

If want to prevent the deletion in the BeforeDelete event handler, you can call the global Abort procedure:

procedure TForm1.TableBeforeDelete (Dataset: TDataset)
  if MessageDlg('Delete This Record?', mtConfirmation, mbYesNoCancel, 0) <> mrYes then
void __fastcall TForm1::TableBeforeDelete (TDataSet *Dataset)
  if (MessageBox(0, "Delete This Record?", "CONFIRM", MB_YESNO) != IDYES)

If Delete fails, it generates an OnDeleteError event. If the OnDeleteError event handler can't correct the problem, the dataset remains in dsEdit state. If Delete succeeds, the dataset reverts to the dsBrowse state and the record that followed the deleted record becomes the current record.

If you are caching updates, the deleted record is not removed from the underlying database table until you call ApplyUpdates.

If you provide a navigator component on your forms, users can delete the current record by clicking the navigator's Delete button. In code, you must call Delete explicitly to remove the current record.

See Also