问题
I tried to open my tables in Delphi with the following code:
for I := 0 to Datamodule1.ComponentCount - 1 do
if Datamodule1.Components[I] is TADOTable then
Begin
TADOTable(datamodule1.Components[i]).EDIT;
End;
But when I want to post it gives me an error that the tables is not in EDIT or INSERT mode. What have I done wrong here?
回答1:
One sample of standard code is like this:
// open the table
ADOTable1.Open; // Mode = dsBrowse
// Enter in Edit mode
ADOTable1.Edit; //Mode = dsEdit
// Change field values
ADOTAble1.FieldByName('NOM').AsString := 'Lou';
...
// Try to save
try
ADOTable1.Post;
except
// Capture the error
// Show Message
//...
end;
See help for methods: Post, Cancel, Edit,... of TDataSet.
It's simple to adapt this sample code to your working code.
Regards.
回答2:
Thanks for the feedback. I have managed to solve the problem. Before i set the table in the edit mode state, I firts check the state, as I have already put the table in edit state before calling this procedure
for I := 0 to Datamodule1.ComponentCount - 1 do
if Datamodule1.Components[I] is TADOTable then
Begin
if not (TADOTable(datamodule1.Components[i]).State in [dsEdit]) then
TADOTable(datamodule1.Components[i]).EDIT;
End;
来源:https://stackoverflow.com/questions/32824523/how-to-open-tables-in-edit-insert-mode-in-delphi