i have the following ms access 2003 table:
Invoice_Master
(
Invoice_Id Autonumber,
Customer_Id integer,
Invoice_Date Date/Time not null,
.
.
.
);
and following code (Delphi 7)
procedure TfmInvoiceMaster.adoMasterEditNewRecord(DataSet: TDataSet);
begin
inherited;
adoMasterEditInvoice_Date.AsDateTime := Date;
end;
when i try to add a new record i end with exception error sys:
Field 'Invoice_Date' cannot be modified.
pls what is wrong
You're doing it wrong. :)
You should be using the AfterEdit
event instead. From the help file on TDataSet.AfterEdit
:
Write an AfterEdit event handler to take specific action immediately after dataset enters edit mode. AfterEdit is called by Edit after it enables editing of a record, recalculates calculated fields, and calls the data event handler to process a record change.
Use it like this:
procedure TfmInvoiceMaster.adoMasterAfterEdit(DataSet: TDataSet);
begin
// No inherited call! This is an event handler, not an overridden method
adoMasterEditInvoice_Date.AsDateTime := Date;
end;
From your text, though, you seem to be wanting to do it when a record is inserted, not edited ("when i try to add a new record i end with exception error sys:"). If that's the case, use AfterInsert
the same exact way:
procedure TfmInvoiceMaster.adoMasterAfterInsert(DataSet: TDataSet);
begin
// No inherited call! This is an event handler, not an overridden method
adoMasterEditInvoice_Date.AsDateTime := Date;
end;
OnNewRecord
is in fact the right event handler to set defaults when using Insert/Appen - kobik 2012-04-03 20:51
inherited
part. could the OP explain? is TfmInvoiceMaster
sub-classed - kobik 2012-04-03 20:55
AfterInsert
is called immediately after a record is inserted (which means it's in editing mode and can be updated), and this event has worked just fine since Delphi 1 for me. I don't see any benefit to using OnNewRecord
instead; AFAICT, for use in initializing new records they're equally functional. :) I don't understand the OP's use of inherited
either, which is why I specifically mentioned NOT using it in my answer - Ken White 2012-04-03 21:35
AfterInsert
is called immediately after the new record is added, and will be there before the user sees the data entry controls. Of course, you could try it really quickly for yourself and see if it works or not. : - Ken White 2012-04-03 21:37
AfterInsert
should work just as well as OnNewRecord
. From the docs: Write an OnNewRecord event handler to take specific actions as an application inserts or appends a new record. OnNewRecord is called as part of the insert or append process. An application might use the OnNewRecord event to set initial values for a record or as a way of implementing cascading insertions in related datasets.
. I'm 99.99% sure that the field is read-only so he will get the same error with AfterInsert
- kobik 2012-04-03 21:41
Edit
in the event name instead of the NewRecord
part; that's what my "wrong" was referencing. I've always had the AfterInsert
work just fine, and just noticed that OnNewRecord
references AfterInsert
in the docs also, so they might be fairly equivalent. (Didn't check the source to find out what the diff might be, though; I'd suspect without looking that they're called in succession if they're assigned.) I guess we'll have to wait for the OP to tell us what the problem actually is, though - Ken White 2012-04-03 21:45
inherited
call in adoMasterEditNewRecord is unusua - mjn 2012-04-03 20:12