Your 2-year-old dog is a young adult with better physical control over bladder and bowels than a puppy, making the task of housebreaking easier. With vigilance, consistency and compassion, you can help your 2-year-old dog to get with the program and eliminate only outside the house. If you have adopted the dog from a shelter, assume it is not housebroken. Even a dog that has been potty-trained can lose the habit in a shelter, due to stress and unfamiliar food.
If you can on the first day you bring it home, introduce your 2-year-old dog it to its special spot outside. Say "Do your business," or "Go potty now." Say the same thing every time; the content of the phrase doesn't matter. Speak in a bright, encouraging tone. Revisit the spot every half hour, whether it's a new dog or just new to potty training.
Take your dog out for a walk first thing in the morning, after each meal, when you get home from work, and right before bed. The minute the dog eliminates outdoors, praise it extravagantly and give it a treat. Don't be afraid to make a fuss and even to look silly. Smile, laugh, do a dance, pet the dog, show it how happy this makes you. Remember, most dogs really want to please you.
Supervise the dog when it is in the house and watch closely for physical "cues" that the dog needs to relieve itself. This is one of the most important things you can do to help your older dog develop or regain good housebreaking. Stay near the dog, or confine it where you can see it, using baby gates or other physical barriers.
Immediately take the dog outside to its special spot for a potty break whenever you notice visual cues, such as sniffing, circling, whimpering or barking. Praise it lavishly when it does its business there, and give it a treat. If the dog begins to squat, say "No" in a firm voice, pick it up if practical or quickly clip on the leash and hustle the dog outside to its special spot. The act of being picked up or leashed, along with the sound of your voice, may be enough to make the dog stop eliminating indoors.
Confine your dog when you can't watch it. Put it in a small, easily-cleaned place like a corner of a laundry room, or use a housebreaking crate. A crate can be a godsend for potty training an older dog, but it is not a place to just warehouse the dog. Don't leave your dog in the crate for longer than five hours, and take it out for a walk immediately before crating, as well as the instant you release it from the crate.
Clean up any accidents with an enzymatic cleaner formulated especially to remove pet stains and odors. Occasional accidents are to be expected during the first few weeks. By removing all traces of odor, you can make it less likely that the dog will repeat the mistake in that spot.
Consult a veterinarian if your dog continues to have accidents in the house in spite of your efforts. There may be a physical cause for the behavior.