Depends on what plants they are infesting. If its something that can survive without water for a while like cacti, pelargoniums etc just stop watering them and the larvae will die. Anything that needs water you need to still water as little as possible.
If you cover the top with sand don't forget they will still try to live in the bottom of the pot too so you will need a dish to put some sand in there too. I have used
DE in the past as a barrier and its worked pretty well but again you have to reapply when wet.
Adding neem to water can also help kill the larvae too.
You can buy fungus gnat nematodes they are really effective but expensive.
In any situation you have to make sure you get every single pot or you might find the problem doesn't go away.
Good luck
I know someone who claims to have success with using salt petre too. Have not done it myself, but appeared to work for them anyway.