Vlad,
I think you missed the point here : it is not a matter of restoring the environment to the state as it was before executing the SQL !
The problem here is that when foxpro executes an SQL for which it can't find one of it's tables, foxpro asks where to find it using an Open table dialog, instead of raising an error. The best solution would be that you could control this behaviour : SET FINDTABLES ON or OFF
The solution Jim Munn proposes here is a workaround that might work ...
Pascal.
>Use aused() function to list all opened aliases. Do this before query running than after. Compare results of both calls - you will have list of all aliases opened during SELECT command running.
> This method also useful to control form environment and close all extra opened aliases during form running.