Problem synchronising data with file

Discussion of open issues, suggestions and bugs regarding Virtual Data Access Components for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
JasonR
Posts: 4
Joined: Fri 07 Dec 2007 12:28

Problem synchronising data with file

Post by JasonR » Wed 19 Dec 2007 14:28

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

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Wed 19 Dec 2007 17:10

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;

JasonR
Posts: 4
Joined: Fri 07 Dec 2007 12:28

Post by JasonR » Wed 19 Dec 2007 21:53

Thank you.
That should do nicely.

Post Reply