Page 1 of 1

Problem synchronising data with file

Posted: Wed 19 Dec 2007 14:28
by JasonR
Hi

I'm trying to use the virtualtable as a sort of 'flat file database'.
The problem I have, is that I want to make sure that all changes to data are saved to file immediately, but if I try to SaveToFile on AfterPost, it causes problems. it seems that the OnPost event is called for every record when you first load the data from file so my SaveToFile procedure is called when I am trying to load the data initially, I can get around this with a Try .. Except condition, but it cant be the best way to do this, and also it makes debugging very difficult.

Code: Select all

//----------------------------------------------------------------------------//
procedure TDataModule1.TASKS_TAfterPost(DataSet: TDataSet);
begin
 Try
   TASKS_T.Filtered := False;
   TASKS_T.SaveToFile('TASKS.VTD');
   TASKS_T.Filtered := True;
 Except;
 End;
end;
//----------------------------------------------------------------------------//

Does anyone have any suggestions ? perhaps there is a different event I could use or a flag I could check to see if the table is loading from file ?

Thanks

Posted: Wed 19 Dec 2007 17:10
by Antaeus
There is no such flag in VirtualTable, but you can implement it by yourself.
Another way is to detach the event before loading, and assign it after records has been loaded from the file. It may look like this:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  AfterPost: TDataSetNotifyEvent;
begin
  AfterPost := VirtualTable1.AfterPost;
  VirtualTable1.AfterPost := nil;
  try
    VirtualTable1.LoadFromFile(FileName);
  finally
    VirtualTable1.AfterPost := AfterPost;
  end;
end;

Posted: Wed 19 Dec 2007 21:53
by JasonR
Thank you.
That should do nicely.