How to Open tables in EDIT/INSERT mode in Delphi

元气小坏坏 提交于 2020-06-18 12:10:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!