Sometimes very simple things can help with this problem. It is all right for your loved one to wander in a safe place, such as in a fenced yard. By providing such a safe place, you may avoid a confrontation. If this doesn't work, remind your loved one not to go out a certain door by placing a stop sign on it or putting a piece of furniture in front of it. A ribbon tied across a door can serve as a similar reminder. Hiding the doorknob by placing a strip of cloth over it may also be helpful.
An alarm system will alert you that your loved one is trying to leave a certain area. Your alarm system may just be a few empty cans tied to a string on the doorknob. You might have to place special locks on the doors, but be aware that such locks might be dangerous if a house fire occurs. Don't use this method if your loved one will be left home alone. Make sure your loved one wears a medical bracelet, in case they do wander away from home.