You shouldn't be utilizing the same relationship as the UI and closing the set. Leave that to the UI framework to close when the user navigates away from the record. That should avoid your closed connection errors you're seeing.
Unrelated to your question, be aware that utilizing the UI sets is a mixed bag. It's great for most use cases because you can get the in memory values, such as a value from the header record to bring down to children records. But you run the risk of a user filtering the result set in a way that breaks your validation. For example, if I filter for polinenum=1 on the PO Line table in the PO application, my set is going to have 1 record even though there might be 10+ lines.
Without knowing your customization, you seem to be trying to validate that something exists in both tables. If they're added at the same time (IE you have to add the object to each child object before you can save) you'll be OK because a user can't filter a set when there are pending changes to save. But if you add the receipt to Maximo first and then add into your custom object later, just be aware that records may be filtered from your view.
When looping, avoid counts as they execute an unnecessary count query against the database when you're just trying to iterate. You can simply do:
i=0
while aSet1.getMbo(i):
pokMbo1=aSet1.getMbo(i)
# logic here
i+=1
------------------------------
Steven Shull
Director of Development
Projetech Inc
Cincinnati OH
------------------------------