Dreaming occurs during the REM stage, at this stage you are in the deepest sleep mode. But REM differs from individual to individual meaning some sleep deeper than others yet still in the REM stage....OK that said, when medicated, the stage is altered or induced and can have an effect on the normal REM cycle, some times dream or nightmares occur, some vivid, some not, same goes with memory of them. The memory usually occurs during the latter stages when the body is in the final moments of the REM cycle. When this REM is over, the hormones will take over and proceed to wake the person up, this can take several hours. This is why so many people wake up in the morning carrying memories but the reason they remember is that at one point of the dream the person awakes, even if briefly, and tend to remember the dream for this reason. Others may not wake up in the middle of the REM stage during a dream and may not even known they dreamt....yes that is the past tense of dream.
As for medication, it can alter the brain waves and really no way to control how they may, so anything is possible!
FYI- REM...Rapid Eye Movement and is about 20% of your sleeping cycle, the other 80% consists of falling asleep and waking.