Recent

Author Topic: How to scroll randomly to any index or record number in Tsqlquery  (Read 3158 times)

heejit

  • Full Member
  • ***
  • Posts: 245
is TSqlQuery.SetCurrentRecord is correct?

« Last Edit: December 03, 2018, 04:41:07 pm by jshah »

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: How to move to a index / record number in Tsqlquery
« Reply #1 on: December 03, 2018, 01:06:06 pm »
Explain.

Normally you use moveby() to jump to another record. Prior and Next are procedures to move 1 up or down. The best way is to use Locate().
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to move to a index / record number in Tsqlquery
« Reply #2 on: December 03, 2018, 01:20:31 pm »
moveby need distance that is move from current position to new position

locate is for search record by field value

I need to jump to a particular record number it is not related to the current position

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: How to move to a index / record number in Tsqlquery
« Reply #3 on: December 03, 2018, 01:46:12 pm »
I still not understand what you looking for. Where is the particular record located?
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

rvk

  • Hero Member
  • *****
  • Posts: 4288
Re: How to move to a index / record number in Tsqlquery
« Reply #4 on: December 03, 2018, 01:48:46 pm »
Yes, how do you determine what index-number to use?
Index-number (or position) within a dataset is never a certainty.
Closing and opening the dataset could result in a different position.

If you got the index-position earlier (after opening) it might be best to use bookmarks.
https://www.freepascal.org/docs-html/fcl/db/tdataset.bookmark.html

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to move to a index / record number in Tsqlquery
« Reply #5 on: December 03, 2018, 02:29:42 pm »
Actually I am writing few small function for TSqlQuery to easy the
access of field

Index is parameter to function
Code: Pascal  [Select][+][-]
  1. function ASQLQuery.get_field(idx: Integer; cname: string): TField;
  2. begin
  3.   self.SetCurrentRecord(idx);
  4.   Result := self.FieldByName(cname);
  5. end;
  6.  
« Last Edit: December 03, 2018, 02:43:34 pm by jshah »

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #6 on: December 03, 2018, 04:58:51 pm »
Scrolling to a record doesn't have to do with setcurrentrecord. Look at property fieldnames which is een TStrings where all fields are stored depending on the query. It has nothing to do with the location of the recordcounter.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #7 on: December 03, 2018, 05:28:59 pm »
Ok

is RecNo is the correct property

rvk

  • Hero Member
  • *****
  • Posts: 4288
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #8 on: December 03, 2018, 05:44:36 pm »
I need to jump to a particular record number it is not related to the current position
Maybe you can first explain what you mean by "jump to a particular record number"?
How did you determine that particular record number?

If it is a number/position saved from a different Open/Close session, you can't use that.

You could use CurrentRecord (integer) with SetCurrentRecord but that only works within the same session (open/close).


heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #9 on: December 03, 2018, 05:59:34 pm »
How did you determine that particular record number?
 -Provide by user

This is within open close of tsqlquery

Flow as follow
1. Open tsqlquery with a select sql
2. move cursor to given record number
3. get the field data work on it
4. close the tsqlquery.



rvk

  • Hero Member
  • *****
  • Posts: 4288
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #10 on: December 03, 2018, 06:42:36 pm »
How did you determine that particular record number?
 -Provide by user
Then you definitely can't use CurrentRecord. That's something you can only determine and use after opening the table. You can't re-use it  once the table is closed !!

You should search for the provided record another way. If the user wants a number you could create a field with a unique (autoincrement) number for that record and use that. Sqldb doesn't provide one. Even recno is not persistent for the same record once you close the table.



heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #11 on: December 03, 2018, 07:14:50 pm »
I can use moveby

moveby(0)
moveby(given_index)


rvk

  • Hero Member
  • *****
  • Posts: 4288
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #12 on: December 03, 2018, 07:20:10 pm »
I can use moveby
No, you can't. If there is another record inserted or deleted after you determined your "given_index", the "given_index" doesn't point to the same record anymore. Try it, delete a record before that given_index and see if moveby still works to go to that record.

If you want to make sure the same record is selected each time you need to use a unique identifier. Usually an ID field which is automatically increased with each insert.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #13 on: December 03, 2018, 08:32:14 pm »
all sql is SELECT only  there is no change
in record

rvk

  • Hero Member
  • *****
  • Posts: 4288
Re: How to scroll randomly to any index or record number in Tsqlquery
« Reply #14 on: December 03, 2018, 08:39:24 pm »
And what is the time-span between determining given_index and using it?
If you are certain the table has not changed, you can use it.

But normally, if it's user provided, it can change overtime.
Records get deleted and inserted and your given_index is invalid from that point onwards.
That's why I would recommend a dedicated ID field (like the whole SQL world is using).
But if you want to use a static position, go ahead and wait until it goes wrong.

« Last Edit: December 03, 2018, 08:41:17 pm by rvk »

 

TinyPortal © 2005-2018