why are you setting the $counter back to 1 in each loop?, it looks like it should be incrementing, suggest you debug if you have FMPA..
I'm also assuming that you're using $id elsewhere in the script - otherwise why invoke it?
I created the two variables $id and $counter to keep track of HOSPID as it loops through the records and to count a maximum of 6 records per ID.
If [ HOSP::HOSPID ≠ HOSP::HOSPID ]
Will never be true any more than 5 ≠ 5 will ever be true.
and this step:
Set Variable [ $id; Value:"HOSPID" ]
Sets the variable $id to the letters HOSPID every time that it executes.
I am guessing that you meant to do this:
If [ HOSP::HOSPID ≠ $id ]
Set Variable [ $id; Value:HOSP::HOSPID ]
But there are much simpler ways to reduce your found set to just the first 6 records:
Sort Records [Restore ; No Dialog ]
Go to Record/Request/Page [first]
If [ Get ( FoundCount ) > 6 ]
Go to Record/Request/Page [7 ; no dialog]
Omit Multiple Records [ Get ( FoundCount ) ; no dialog ]