If you come to Thailand with a tourist visa and after 90 days want to stay in the country longer than these 90 days without having to leave the country, there is a form called TM86 that you can fill out and together with various other documents apply for changing a tourist visa into a Non Immigrant Visa. If you are not a magician and can remove the visa stamp and get a Visa Exemption stamp in your passport instead, you must use form TM86 NOT TM87.
The requirement is not that you HAD a tourist visa on arrival in Thailand and lost it when you passed the passport control, but that you actually need to have a tourist visa the moment you apply to change your visa. And this is what the Immigration Office says. Please NOTIS the word "has" NOT "had".
"1.1 Form TM.86 for the foreigner, who has Tourist or Transit Visa and want to applies for a change of type of visa (from Tourist Visa or Transit Visa to Non-Immigrant Visa);"