0

My question is how I can do operations like addition and subtraction with TBookmark object. For example, let's say I want to go 4 rows higher in the dbgrid:

MyPoint:=Query1.GetBookmark;
...
//MyPoint:=MyPoint-4;
Query1.GotoBookmark(MyPoint);

Here the commented line is wrong. It produces a "Operator not applicable to this operand type" message. The question is what I should write in place of the commented line. Thanks in advance!

Rick77
  • 241
  • 3
  • 21

1 Answers1

8

You cannot perform arithmetic directly on a bookmark. To do what you ask you need to go to the bookmark, and then move relative to that:

Query1.GotoBookmark(MyPoint);
Query1.MoveBy(-4);

If you wish, you could then save another bookmark representing that record.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • This solution doesn't work in my case because I refresh the query and going to the bookmark can produce a "Record not found" error in case some records have been deleted. – Rick77 Mar 05 '13 at 11:40
  • 2
    Well that is only to be expected. – David Heffernan Mar 05 '13 at 11:47
  • @Rick77 what did you expect? Didn't know where to go, but from this point moving 4 back leeds to somewhere in nowhere. After a refresh everything could happen, also no data at all. – Sir Rufo Mar 05 '13 at 12:06
  • 3
    @Rick77, This answers your question perfectly. I don't understand your comment. You initially stated that you have reference to the bookmark (MyPoint), and wanted to move -4 records. You loose your bookmarks after your refresh your dataset. – kobik Mar 05 '13 at 12:06
  • @Rick77 maybe this http://delphi.about.com/od/delphitips2008/qt/dbgrid_row_pos.htm will help you in this case – Sir Rufo Mar 05 '13 at 12:17
  • 1
    Just to add that using a bookmark has nothing to do with what you want. Unless you intend to jump back to "MyPoint" there is no need for `Query1.GotoBookmark(MyPoint)`, and the short answer to your question is `Query1.MoveBy(-4)` - this will get you -4 records from your `current` position. – kobik Mar 05 '13 at 12:47
  • Thanks for all the answers! – Rick77 Mar 06 '13 at 14:59