Deactivating a Dataset
Go Up to Determining and Setting Dataset States
A dataset is inactive when it is closed. You cannot access records in a closed dataset. At design time, a dataset is closed until you set its Active
property to True
. At runtime, a dataset is initially closed until an application opens it by calling the Open
method, or by setting the Active
property to True
.
When you open an inactive dataset, its state automatically changes to the dsBrowse
state. The following figure illustrates the relationship between these states and the methods that set them.
To make a dataset inactive, call its Close
method. You can write BeforeClose
and AfterClose
event handlers that respond to the Close
method for a dataset. For example, if a dataset is in dsEdit
or dsInsert
modes when an application calls Close
, you should prompt the user to post pending changes or cancel them before closing the dataset. The following code illustrates such a handler:
procedure IBTable.VerifyBeforeClose(DataSet: TIBCustomDataSet) begin if (IBTable.State = dsEdit) or (IBTable.State = dsInsert) then begin if MessageDlg('Post changes before closing?', mtConfirmation, mbYesNo, 0) = mrYes then IBTable.Post; else IBTable.Cancel; end; end;
To associate a procedure with the BeforeClose
event for a dataset at design time:
- Select the table in the data module (or form).
- Click the Events page in the Object Inspector.
- Enter the name of the procedure for the
BeforeClose
event (or choose it from the drop-down list).