Comparing 2 instances of a Table not working!

Saint-SageSaint-Sage Member Posts: 92
edited 2007-03-27 in Navision Financials
Hello Everyone,

I usually work in 4.0, and in that version, if you want to compare entries from the same table, you can simply nest a second instance of the table within a loop and can reset, setfilters on the second instance without affecting the instance in the outter loop.

Unfortunatly I am having to write a codeunit for 2.6 to repair some entries to the Item Ledger Table and I am having a problem. Here a pseudo example of the code...

ItemLedger.RESET;
ItemLedger.SETRANGE(blah blah)

IF ItemLedger.FIND('-') THEN BEGIN
  REPEAT
    ItemLedger2.RESET;
    ItemLedger2.SETRANGE("Item No.", ItemLedger."Item No.");

    IF ItemLedger2.FIND('-') THEN BEGIN
      REPEAT
        InvoiceBalance += ItemLedger2."Amount";
      UNTIL ItemLedger2.NEXT = 0;  // Terminate Inner Loop
    END;  //  End IF ItemLedger2.FIND

    //Increment some stuff...

    IF InvoiceBalance <> 0 THEN BEGIN
       // Do some stuff...
    END;

  UNTIL ItemLedger.NEXT = 0;  // Terminate Outter Loop

END;  // End IF ItemLedger.FIND

The problem I am having is that it enters an infinate loop. After some debugging, I realized that the Entry Number at the top of the outer loop is always the same each pass through.

This makes me think that when I setrange in the second instance to the single record in the outter loop, it is giving me a recordset that only has the single record in it, and navision is unable to determine NEXT = 0 for some reason.

If anyone can shed some light on why this is happening, or how you are supposed to compare nested instances of the same table in 2.6 please let me know.

Any help you can give will be greatly appriciated.

Have a great day!

No one loves you like the one who created you...

Answers

  • kinekine Member Posts: 12,562
    Sorry, but I can see just one repeat but two until keywords in your code. And please, use the [ code ] tag to format the code in your posts.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Saint-SageSaint-Sage Member Posts: 92
    Sorry for the poor formatting.

    The repeat is there in the original code, I must have left it off when typing out this algorithm, I figured it was pretty straight forward as to what I was doing.

    Thanks for the reply!

    No one loves you like the one who created you...
  • anna_sicosbtanna_sicosbt Member Posts: 7
    Are you sure that while you are doing some stuff, you aren't altering the bla, bla filter? :)
    Anna Perotti
    Dynamics NAV MVP (2005-2010)
  • kinekine Member Posts: 12,562
    If this is not COPY&PASTE code, look into your original code if there is correct variable in the UNTIL line ( ItemLedger2. in first one and ItemLedger. in second one) - it is common mistake when the code is created by Copy&Paste.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Saint-SageSaint-Sage Member Posts: 92
    I was modifying a single field (Quantity) in the composite key I had selected. Evidently this behaves as if I was modifying the primary key inside the loop. When I modified the key to not include quantity, it slowed things down, but it works as expected now.


    Thanks for all the help!

    No one loves you like the one who created you...
  • kinekine Member Posts: 12,562
    Saint-Sage wrote:
    I was modifying a single field (Quantity) in the composite key I had selected. Evidently this behaves as if I was modifying the primary key inside the loop. When I modified the key to not include quantity, it slowed things down, but it works as expected now.


    Thanks for all the help!

    Never modify the rec variable which is used in the loop as iterator. Use other rec variable for that. It is golden rule for NAV. :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.