Accessing DataSet records

I'm trying to read all fields and records in a dataset to update some ohter objects with data from the dataset. Is there a simple way to read all fields for all records without changing the user's current (acitve) record? - Also which event would you suggest I use to trigger saving of data?
LVL 1
runebjAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MotazConnect With a Mentor Commented:
If you want to use calculated fields see this example:

Suppose that you have two integer fields A and B, and you want a summation of A+B into new calculated field (C) you can do this:

- Double click in Table1 object, then right click mouse button, then click on add all fields.

- Right click again Table1 then click New field, name it C, Integer type, the name will be Table1C, of type calculated

- At OnCalcFields event of Table one write this code:

  Table1C.AsInteger:= Table1A.AsInteger+ Table1B.AsInteger;

Motaz
0
 
victor_christovCommented:
You can use additional TQuery with SQL statement 'SELECT * FORM ....', or you can use BookMark.

Second part of your question is not clear enough for me. Pls send more details.

Regards, Victor
0
 
MotazCommented:
What did you want to do ?

You may use Calculated fields, did you know how to use it?

Motaz
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
victor_christovCommented:
Sorry ... 'SELECT * FROM ...'
0
 
kretzschmarCommented:
hi,

victor said the both possibilities.
i would prefer the query solution, because its mostly faster than an iterating through the table.

meikl
0
 
DonBartholomewCommented:
I don't think there is another way if you want to operate on the same dataset. But you can assign the dataset.ActiveRecord prop to a var, then browse through the dataset, and then set the ActiveRecord prop back to the var.

<Lastindex: integer>
Lastindex := dataset.ActiveRecord;
Dataset.First;
while not dataset.Eof do
begin
<Assign field values to other object>
Dataset.Next;
end;
Dataset.ActiveRecord := Lastindex;

But I'm not sure I understand your question properly. What other objects do you have to update? Why not do it the obvious way: use databound controls for this purpose?
Anyway, events for saving: afterscroll event of dataset for individual records.

Ciao
0
 
yk030299Commented:
yeah, updating on the same dataset may cause dead-locking.
0
 
runebjAuthor Commented:
I guess I was pretty unclear about this - sorry for that...

I have a rather complex structure of non visual objects that exists in memory only. I'm using a TdxMemData component to "fake" a database so that I can update some of the information from databound controls. My problem is catching the changes and storing them back to the objects in memory.

I have now used Motaz' idea of catching all changes in the OnCalcFields event and it seems to work. But, I would still like to know if it is possible to access the data in memory directly.
0
 
MotazCommented:
>But, I would still like to know if it is possible to access the data in memory directly

If I can understand you, you can access all records and fields without using any database controls such as:

var
  i: Integer;
begin
  Table1.First;
  while not Table1.Eof do
  begin
    for i:= 0 to Table1.Fields.Count - 1 do
      Memo1.Lines.Add(Table1.Fields[i].AsString);
    Memo1.Lines.Add('--------------');
    Table1.Next;
  end; // while
end;

this will display all records in a memo1

you do not need DataSoure nor DataControl only you need a TTable

Motaz
0
All Courses

From novice to tech pro — start learning today.