SETCURRENTKEY causes exit from loop

FommoFommo Member Posts: 138
Hi

I have a strange problem. I'm trying to optimize some code for a customer. It's based on a table that has a field "Status" and I only want to process those with a certain status. So I added a key for Status and then I try to run this code:
MySalesOrders.RESET;
MySalesOrders.SETCURRENTKEY(Status);
MySalesOrders.SETRANGE(Status,MySalesOrders.Status::OK);
NoOfRecs := ROUND((MySalesOrders.COUNT/100),1,'>');

IF MySalesOrders.FINDSET THEN BEGIN
  ..
UNTIL  MySalesOrders.NEXT = 0;

The problem is that when I run this it ends the loop after one iteration. NoOfRecs get a value of 66 so there are records to iterate.
If I remove SETCURRENTKEY it works, although bad performance.
I think the code is pretty simple. Do you have any ideas? What can make the key go crazy? Can it be fragmentation that gives the key wrong information about the dataset?
/Simon

Answers

  • vijay_gvijay_g Member Posts: 884
    I can't see REPEAT in your code. :)
  • FommoFommo Member Posts: 138
    vijay_g wrote:
    I can't see REPEAT in your code. :)

    True, of course there is a REPEAT.
    The complete code here again:
    MySalesOrders.RESET;
    MySalesOrders.SETCURRENTKEY(Status);
    MySalesOrders.SETRANGE(Status,MySalesOrders.Status::OK);
    NoOfRecs := ROUND((MySalesOrders.COUNT/100),1,'>');
    
    IF MySalesOrders.FINDSET THEN BEGIN
      // Doing some dialog updates that doesn't affect the dataset
      REPEAT
        ..
      UNTIL  MySalesOrders.NEXT = 0;
    END;
    

    The client is running NAV 2009 R2 (build 32012), that is NO application updates are implemented. I searched for some hotfixes that may affect SETCURRENTKEY or NEXT, but I didn't find any.
    Does anyone know about such a hotfix?
  • bbrownbbrown Member Posts: 3,268
    Are you changing the value of "Status" for the current MySalesOrders record in your REPEAT-UNTIL loop?
    There are no bugs - only undocumented features.
  • FommoFommo Member Posts: 138
    bbrown wrote:
    Are you changing the value of "Status" for the current MySalesOrders record in your REPEAT-UNTIL loop?
    Good question. Thanks a lot for the idea. I'll check that tomorrow. That may for sure be a logical reason.
  • vijay_gvijay_g Member Posts: 884
    And if you are changing the Status in REPEAT UNTIL then do this.
    NewSalesOrders.GET(MySalesOrders);
    NewSalesOrders.Status := NewStatus;
    NewSalesOrders.Modify;
    

    You can also use FINDSET(FALSE,TRUE); to know more about parameter of FINDSET see the help(F1).
  • FommoFommo Member Posts: 138
    vijay_g wrote:
    You can also use FINDSET(FALSE,TRUE); to know more about parameter of FINDSET see the help(F1).
    Yup, that was the reason. Silly me not thinking of that.
    Thanks a lot for the suggestion, works like a charm now. :D
Sign In or Register to comment.