Renaming primary key


i have a separate table which contains list of items. I also subscribed to a onAfterRename event on item table. So when a item number is renamed, the item on the list should also be renamed. I cannot get it to work with RENAME function:

 Itemlist.SETFILTER("Item No.",xRec."No.");

     Itemlist.RENAME(Itemlist."Item No.",Rec."No.");

    UNTIL Itemlist.NEXT = 0;

Rec is the item record.

does anybody know what’s the problem in above code?

Hi Lynix,
If the Item No. field is directly related to the Item table, then you shouldn’t need this code at all.
The rename of the Item table will automatically rename the content of all occurrences of the item no. in the database.

Hey Erik, thanks for the answer. Tha’ts correct it does rename it automatically. But in this solutions there are no relation between tables, so i need to get this code to work. When i run this code, after a item number is renamed, the item on the list gets deleted. Thats strange behaviour.

This maybe offtopic, but renaming filtered recordset with repeat…next will not always work as aspected as pointer moves and may skip some records.
I suggest to use While findfirst do rename…

You didn’t answer my question in regards to the primary key of that table?
You need to add all the fields of the primary table, when you rename. It doesn’t rename a specific field to a specific value. So for your Itemlist.RENAME(Itemlist.“Item No.”,Rec.“No.”); then it would require that you have two fields in your item list table, Item No. (old) and Item No. (new).

I don’t know your solution, but would it not be easier just to add a relation to the item table?

As in regards to Wertis comment, then that’s certainly true too.
Either use a while or create a second var of the ItemList table and rename that one instead.