Field 'Invoice_Date' cannot be modified

Go To StackoverFlow.com

2

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

2012-04-03 20:04
by BlueOcean
are the other fields writeable? (maybe the whole dataset is read-only). p.s. the inherited call in adoMasterEditNewRecord is unusua - mjn 2012-04-03 20:12


3

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;
2012-04-03 20:38
by Ken White
i want the user see the date before he post the record, i used inherited for other purpose not mentioned here.. thanks for hel - BlueOcean 2012-04-03 20:49
@Ken White, I disagree. OnNewRecord is in fact the right event handler to set defaults when using Insert/Appen - kobik 2012-04-03 20:51
...But I also fail to understand the inherited part. could the OP explain? is TfmInvoiceMaster sub-classed - kobik 2012-04-03 20:55
@kobik: OK. We disagree. :) The docs specifically say that 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
@BlueOcean, the user will see the data before it's posted. Read the documentation - 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
@KenWhite, Let me rephrase :) I disagree on "You're doing it wrong". 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
@kobik, OK. :) Fair enough. I saw the 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