Loop on table

-matrix--matrix- Member Posts: 103
Hi everybody!

I need to do a loop on table from the end to begin, do you think that this code is right?
TBTable.ASCENDING(FALSE);
TBTable.SETRANGE();
IF TBTable.FINDFIRST THEN BEGIN
   REPEAT
   ......... 
  ........... 
   UNTIL (TBTable.NEXT = -1);

Answers

  • SogSog Member Posts: 1,023
    No, I think the code is not good.
    Because you use a findfirst and choose a repeat afterwards (use findset or find('-'))
    and second: next = 0 instead of next = -1 (This might be good, but I never use it) I find it too ambigous with next(-1)
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • Troubles_In_ParadiseTroubles_In_Paradise Member Posts: 588
    -matrix- wrote:
    Hi everybody!

    I need to do a loop on table from the end to begin, do you think that this code is right?
    TBTable.ASCENDING(FALSE);
    TBTable.SETRANGE();
    IF TBTable.FINDFIRST THEN BEGIN
       REPEAT
       ......... 
      ........... 
       UNTIL (TBTable.NEXT = -1);
    

    try with:
    IF TBTable.FINDSET THEN BEGIN
       REPEAT
       ......... 
      ........... 
       UNTIL (TBTable.NEXT = 0);
    

    take a look to "Next function (Record)" in OnLine help.
    Edit: sorry to repeat what sog has already sad! =D>
    ~Rik~
    It works as expected... More or Less...
  • -matrix--matrix- Member Posts: 103
    Thanks for replies boys but is needful the ASCENDING with this sintax ?
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Did you try?
    Item.ASCENDING(FALSE);
    IF Item.FINDFIRST THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT = 0;
    
  • hemantOnehemantOne Member Posts: 98
    Hi,

    I think if you want records in Descending Order and you do not get by findset after Ascending(False).
    May be you need a key or doing some efforts by other way...
    Regards,
    Hemant
    They can conquer who believe they can
    .
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    -matrix- wrote:
    Thanks for replies boys but is needful the ASCENDING with this sintax ?

    FINDSET can only retrieve records in ascending order.
  • SogSog Member Posts: 1,023
    Why do you think we didn't comment on the ascending: that part is correct.
    ascending false will effectively sort the table in reverse.
    The alternative was using a find('+') and using until table.next(-1) = 0

    Mohanna, You ought not to use findfirst in a loop!
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • -matrix--matrix- Member Posts: 103
    Yes, I'm trying and It seems work with the FINDSET and with THE FINFIRST =D>
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Sog wrote:
    Mohanna, You ought not to use findfirst in a loop!
    Agree with you.
  • SogSog Member Posts: 1,023
    I'm starting to get confused. Help tells me findset can only be used in descending order, which tells me default ascending is already false: Could you try with ascending(true)?
    if that doesn't work, use find('-')
    And please don't use findfirst, it's not meant for looping. (findfirst does a select top(1) from table) if nav detects a loop it uses a second query. while findset and find('-') only use 1 query to get the data.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • -matrix--matrix- Member Posts: 103
    But so if I can't use the FINDSET what is the right instruction?
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Item.ASCENDING(FALSE);
    IF Item.FIND('-') THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT = 0;
    
  • BeliasBelias Member Posts: 2,998
    Sog wrote:
    I'm starting to get confused. Help tells me findset can only be used in descending order
    i think you misread the help (or i misread you rpost :mrgreen: )
    FINDSET can only retrieve records in ascending order
    [/quote]
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • SogSog Member Posts: 1,023
    I did not misread the help, just a different version :mrgreen:
    Furthermore, FINDSET only supports descending loops. If you want to loop from the bottom up, you should use FIND(‘+’).
    FINDSET can only retrieve records in ascending order. If you want to loop from the bottom up, then you should use FIND('+').
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • krikikriki Member, Moderator Posts: 9,110
    Item.ASCENDING(FALSE);
    IF Item.FIND('-') THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT = 0;
    
    This is the correct way. (or also ASCENDING(TRUE) and FIND('+') and NEXT(-1)
    But in certain cases, a better way may exist.
    The FIND('-') creates a cursor in SQL that is slow and heavy on the system.
    If you have few records (<= # of records in recordset ; defined in File=Database=Alter=>Tab advanced), it is better to use FINDSET and read them in ASCENDING and save them in a temptable and then read them in descending. If you code is run a lot of times by a lot of users, this makes it a lot less heavy on the system.
    Item.ASCENDING(TRUE); //Ascending!
    IF Item.FINDSET THEN
      REPEAT
        tmpItem := Item;
        tmpItem.INSERT(FALSE);
      UNTIL Item.NEXT = 0;
    
    tmpItem.RESET;
    tmpItem.ASCENDING(FALSE);
    IF tmpItem.FIND('-') THEN
      REPEAT
        Message('%1',tmpItem."No.");
      UNTIL tmpItem.NEXT = 0;
    

    BTW: see also http://www.mibuso.com/howtoinfo.asp?FileID=22
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • mabl4367mabl4367 Member Posts: 143
    Sog is right.

    Usually you sould try to avoid Find('-') and Find('+') but in this senario you can ither

    sort in descending order, find the first record and step forward:
    Item.ASCENDING(FALSE);
    IF Item.FIND('-') THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT = 0;
    

    or

    sort in ascending order, find the last record and step backward:
    Item.ASCENDING(TRUE);
    IF Item.FIND('+') THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT(-1) = 0;
    
  • -matrix--matrix- Member Posts: 103
    Thanks to Everybody for the numberous replies =D> , I used this solution :
    Item.ASCENDING(FALSE);
    IF Item.FIND('-') THEN
      REPEAT
        Message('%1',Item."No.");
      UNTIL Item.NEXT = 0;
    
Sign In or Register to comment.