Capture changes on Mstable Field

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
LeoMendez
Posts: 4
Joined: Tue 29 May 2012 21:37

Capture changes on Mstable Field

Post by LeoMendez » Tue 29 May 2012 22:04

Good evening:
I used ODAC VCL components many years ago and based on the good results now i'm evaluating the last release of SDAC 6.1 for Delphi 6 (my version of IDE is 6 Enterprise with all the updates), the app needs to interact with MS SQL server 2000.And i'm having an unsolved issue maybe for my lack of Knowledge about sdac.

In my application i have a Msconnection,( always connected without login prompt) ,
A Mstable related to the connection (The table is always active and permantely refreshed
) a datasource with the Mstable as dataset , and a Dbgrid reflecting the changes of the MStable.The MS sql table is constantly being updated by another applications,and the changes in runtime are perfectly reflected, but if i try to capture the changes to process it nothing happends.

For illustration purposes the table is something like this:

SYMBOL _DATE TIME _LAST BID ASK
-----------------------------------------------------------------
AUDUSD ; 20120529 ; 08:10:43 ; 0.9849 ; 0.98316 ; 0.98342
CLPUSD ; 20120529 ; 08:10:18 ; 512.6 ; 512.6 ; 513.1
EURUSD ; 20120529 ; 08:10:43 ; 1.2538 ; 1.2524 ; 1.25251
GBPUSD ; 20120529 ; 08:10:42 ; 1.5678 ; 1.5664 ; 1.56684

I need to capture the changes in the "_Last" field , for that purposes i entered to the dataset manager , and in the Field events i only set a message on the "OnChange" event, to test if this is the right way :

procedure TForm1.MSTable1_LASTChange(Sender: TField);
begin
ShowMessage(MSTable1.Fields[3].Text);
end;

Running the application , the data changes constantly but there's no meesage dislayed.
Can you Give me some idea about what i'm doing wrong with SDAC?
Best Regards
Leonardo.

AndreyZ

Re: Capture changes on Mstable Field

Post by AndreyZ » Wed 30 May 2012 08:32

Hello,

The OnChange event occurs immediately after the data in the field is written to the record buffer. Here is a code example that demonstrates it:

Code: Select all

procedure TMainForm.ButtonClick(Sender: TObject);
begin
  MSTable1.Open;
  MSTable1.Edit;
  MSTable1.FieldByName('_Last').AsString := 'test'; // here MSTable1_LastChange will occur
end;

procedure TMainForm.MSTable1_LastChange(Sender: TField);
begin
  ShowMessage(Sender.Text);
end;

LeoMendez
Posts: 4
Joined: Tue 29 May 2012 21:37

Re: Capture changes on Mstable Field

Post by LeoMendez » Wed 30 May 2012 18:37

Dear AndreyZ
Many thanks for your fast answer.

I implemented your sample and is reporting the error "Field Last cannot be modified"
previously a changed the Reandonly property to false.
Im supposing that cause is That Mstable is already connected and reading data, maybe sending the text on the button click event to the field i'm doing an Update to the table.

To be more explicit with my problematic i uploaded it to youtube , the link is :
http://www.youtube.com/watch?v=CwzYBY8h ... e=youtu.be

sorry form my Spanglish and many thanks in adavanced

AndreyZ

Re: Capture changes on Mstable Field

Post by AndreyZ » Thu 31 May 2012 09:42

The point is that SQL Server returns the readonly flag for fields that cannot be updated. If you try to update such fields, the "Field Last cannot be modified" error will occur. The OnChange event occurs only if the data in the field is written to the record buffer. It means that to make the OnChange event occur, you need to change the field value (on the client). When you refresh a dataset, the OnChange event will not occur because there were no changes on the client. When a dataset is refreshed, the data is obtained from the server.
To obtain data difference in a dataset before and after refresh, you should have two datasets, one containing data before refresh and another containing data after refresh.

LeoMendez
Posts: 4
Joined: Tue 29 May 2012 21:37

Re: Capture changes on Mstable Field

Post by LeoMendez » Thu 31 May 2012 14:25

Thanks , form example using a virtualtable connected to the Mstable?
Regards

LeoMendez
Posts: 4
Joined: Tue 29 May 2012 21:37

Re: Capture changes on Mstable Field

Post by LeoMendez » Thu 31 May 2012 15:08

I need to clarify that i dont need to change any data on Mstable1, y only need to launch calculations to another table using the field change as a trigger , i'm not familiarized with SDAC but in some level of the client the change of the value for _last field is refleced , cause the dbgrid is showing it.as you can see on my video posted

Another choice is reading the Transaction log..... but is a little hard :D
Thanks

AndreyZ

Re: Capture changes on Mstable Field

Post by AndreyZ » Fri 01 Jun 2012 10:20

If you want to launch some calculations based on refreshed data, you can use the AfterRefresh event. The AfterRefresh event occurs after an application refreshes the data in the dataset. Here is a code example:

Code: Select all

procedure TMainForm.MSTable1AfterRefresh(DataSet: TDataSet);
begin  
  // here you can launch your calculations
end;

Post Reply