Good suggestion. I did get it to work out but I ended up doing it a bit differently. I wrote an SQL statement and used it as a command. I'm not sure if this is the most efficient way, but it worked for me. I have an IBM iSeries Server. My statement in case anyone else needs it as an example:
SELECT ROW_NUMBER() over (partition by tb.ordn40 order by tb.adat40, tb.atim40
desc) as COUNT, tb.cono40, tb.adat40, tb.ordn40, tb.atim40
FROM osld1f3.oep40a as tb
WHERE tb.arct40=2 and tb.aprg40='OE340' and tb.actf40<>'X'