Link to home
Start Free TrialLog in
Avatar of GePh
GePh

asked on

how to edit key-field values and avoid immediate resort

I'm using D4 with MS ACCESS 97 tables.
The table is indexed and the value of the indexed field is like a sequence number. Now I have to insert a set of new records into this table (at the position of the cursor in the DBGrid). To do so it is neccesary to edit all records which have a higher sequence number than the sequence number at cursor position, to make space for the new records.
My problem:
When I increase the key-field value of the first record after the cursor position (for example by 4), the changed record wil be resorted acc to the index and the method table.next will not find the second record after the original cursor position.
How can I avoid the resorting of the records until the modifications have been made?
Remark:
I have tried to start the modifications beginning from the bottom, but this works to slow.

does anybody have an idea??
Avatar of Motaz
Motaz

Befor you modify the all next records save current position using:

P:= Table1.GetBookMark;


After modifing all records return to that position again using:

Table1.GotoBookmark(P);

P: String;


Avatar of GePh

ASKER

ok, with bookmark I am able to go back to the original cursor position, but this does not solve my problem to modify all the following records.  
If you want to modify the following records write this:

Incerement:= 1;
while not Table1.Eof do
begin
  Table1.Edit;
  Table1.FieldByName('YourField').AsInteger:= Table1.FeildByName('YourField').AsInteger + Increment;
  Table1.Post;
  Table1.Next;
end;
Avatar of GePh

ASKER

That is what I tried, but this doesn't give the expected result.
Example:
2o records, the value of the key field is 1...20.
Expecting I want to insert 4 records after position 10, I have to increase record 11 to 20 by 4.
Starting with record 11, the new value is 15. Immediately after posting this modification, the former record 11 is now at position 15 and table.next will bring me to record 16 and not to 12, as I want. So I am looking for a method to access the records 11 ... 20 step by step to be able to modify them
ASKER CERTIFIED SOLUTION
Avatar of Igor UL7AAjr
Igor UL7AAjr
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You must go from the last record to the current such as:


for i:= 20 downto 11 do
begin
  Table1.Edit;
  Table1.FieldByName('YourField').AsInteger:= i + 4
  Table1.Post;
  Table1.Prior;
end;

This will ensure that the modified records will not moved form it's position.

Motaz
www.geocities.com/motaz1
Avatar of GePh

ASKER

Your proposal works fine!

Thank you for your help
Always wellcome;)
Igor.