There are two(2) other property that had not been mentioned and coz they have not been used as sample so far and these are as follows:
TableName - is useful because it let you chagne the table whose data your from displays on-the-fly. To change the TabelName to a TTable in an open form do the following;
with CustTbl do
begin
disableControls;
try
Active := false;
TableName := 'OldCust.db';
Active := true;
finally
EnableControls;
end;
end;
ReadOnly - is another property that can only be changed when the TTable is NOT active. The following code will make the Customer table ReadOnly;
with CustTbl do
begin
disableControls;
try
Active := false;
ReadOnly := true;
Active := true;
finally
EnableControls;
end;
end;
TTable has a rich set of events of which you can take advantage. They are:
AfterCancel AfterClose AfterDelete
AfterEdit AfterOpen AfterInsert
AfterPost BeforeEdit BeforeDelete
BeforeOpen BeforePost BeforeCancel
OnCalcFields
OnNewRecord
of these events the most useful are OnCalcFields, BeforePost and AfterInsert. You have already seen examples of using OnCalcFields in the creating Calculated fields section earlier here-in.
BeforePost, is a good place to compute the value of any fields that are calculated from other fields instead of being entered by the User. One example might be a computed primary key taht is derived from data in other fields.
procedure TMainData.CustTblBeforePost (DataSet : TDataset);
var
State : String;
begin
State := CustTbl.FieldByName('State').AsString;
if (length(State) = 2) and (CustTbl.FieldByName('Country').AsString <> 'America') then
begin
MessageDlg('Country must be America for this State.', mtError, [mbOk],0);
Abort;
end;
end;
|