1

Delphi tDataSet.Append seems not to call Post. In its reference it says

Dataset methods that change the dataset state, such as Edit, Insert, or Append, or that move from one record to another, such as First, Last, Next, and Prior automatically call Post.

But I don't see the incremented RecordCount in the following code.

Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // 0

FDMemTable1.Append;

Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // still 0

If I insert FDMemTable1.Post after Append the result shows the RecordCount is 1.

SHIN JaeGuk
  • 494
  • 1
  • 5
  • 14

2 Answers2

5

Why not tDataSet.Append automatically call Post?

Because it would defeat the object of calling Append in the first place; if it did call Post automatically, it would result in a blank record being posted to the table with no opportunity to set its field values first. Equally, automatically calling Post after Edit, or Insert would also prevent any field values being changed by user code

The statement you quote from http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_Post.html is, at best, badly worded imo, since it can be mis-read to give the impression that "Dataset methods that change the dataset state, such as Edit, Insert, or Append [...] automatically call Post" at the end of the method, which is not the case for the reason I have stated. What it should say, imo, is that in common with navigation methods such as First, Last, Next & Prior, these methods will start with Post automatically being called, if necessary, on the current record before the rest of the method executes. This is because these methods always call CheckBrowseMode as a first step and this contains the code

procedure TDataSet.CheckBrowseMode;
begin
  CheckActive;
  DataEvent(deCheckBrowseMode, 0);
  case State of
    dsEdit, dsInsert:
      begin
        UpdateRecord;
        if Modified then Post else Cancel;
      end;
    dsSetKey:
      Post;
  end;
end;
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Looking at the source code for TDataset, yeah, the statement is wrong about Edit, but not about Insert and Append. They both call Post. (Ok, only when dataset state is dsInsert/dsEdit... But Post doesn't do anything when it's not in one of those state anyway) – Ken Bourassa May 23 '19 at 13:14
3

So it does exactly what it says...

//Dataset.State = dsBrowse
Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // 0

FDMemTable1.Append;
//now Dataset.State = dsInsert

Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // still 0, previous record not yet posted.

FDMemTable1.Append; //because Dataset.State <> dsBrowse, it now post pending changes

Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // Now 1, Dataset.State = dsInsert 

FDMemTable1.Post;

Memo1.Lines.Add(IntToStr(FDMemTable1.RecordCount)); // Now 2, Dataset.State = dsBrowse 

The first time you call Append, there is just nothing to post.

Ken Bourassa
  • 6,363
  • 1
  • 19
  • 28