Ahmed,
Lets see if I am following - You are retrieving all the Patients which would be 9 in your example. You are tabbing through these patients and retrieving details for every one of them. But you also say some of the details you retrieved don't belong to the patient so in your print screen, what row in the detail does not belong to Patient Code 6? That would be the part that is confusing all of us.
As others have said I really think you need to rethink your logic. You should be retrieving only the rows for the patient OR filtering out the detail rows - either way you should only have valid detail rows for the active patient.
On another note - if you are retrieving all the patients in the master then you need other code to stop the user from using the Scroll Wheel. That will scroll the rows but the row does not change focus.
I have attached an example - see if this is what you are trying to do (you will need to rename the txt extension to srw and srd - we can't attach sr files). I did not spend a lot of time but if you click in the detail section for the first row and then try and tab out it will not let you. You can only get the detail row to change if you tab in the header.
Chris Craft