RecordRef: How to select best key programmatically

The following code shows how to programmatically get the best key given the filters set on a recordref variable. RecRefGetBestKeyNo(rr : RecordRef) : Integer //The function returns the index of the best key to use given the applied filters. intBestKey := 1; //Enumerate all fields with filter. Temporary record used for speed. FOR i := 1 TO rr.FIELDCOUNT DO BEGIN fr := rr.FIELDINDEX(i); WHILE (NOT loctmprecInteger.GET(fr.NUMBER)) AND (j <= 255) DO BEGIN rr.FILTERGROUP(j); IF fr.GETFILTER <> '' THEN BEGIN loctmprecInteger.INIT; loctmprecInteger.Number := fr.NUMBER; loctmprecInteger.INSERT(FALSE); END; j += 1; END; CLEAR(fr); END; //Loop through all keys to find best match. FOR i := 1 TO rr.KEYCOUNT DO BEGIN CLEAR(intScore); CLEAR(intQty); kr := rr.KEYINDEX(i); FOR j := 1 TO kr.FIELDCOUNT DO BEGIN fr := kr.FIELDINDEX(j); IF loctmprecInteger.GET(fr.NUMBER) THEN BEGIN //Score for Placement: intScore += POWER(2, 20 - j); //Score for Quantity: intQty += 1; intScore += POWER(2, 20 - j) * (intQty - 1) END; CLEAR(fr); END; IF intScore > intBestKeyScore THEN BEGIN intBestKey := i; intBestKeyScore := intScore; END; END; EXIT(intBestKey); You just MIGHT think: Hey isn’t there a difference between Native and SQL so score should vary by looking at rr.RECORDLEVELLOCKING. Your thoughts on this would be appreciated in the forum, even though the above code covers both key field placement and key field quantity.