>I made a trace and I can see that VFP crash when after create a cursor via SQL I assign the cursor name to oGrid.RecordSource property.
You may make your code to work safer if you unlink grid from data before making any modifications with its recordsource and link it again when cursor is ready:
Grid.Recordsource=""
Select ....
...
Grid.Recordsource="mycursor"
Grid.Refresh
Just try it. This code does not guarantee that everything will look nicely, but at least it should work.
One more tip: it's reasonable to shift focus out of grid before you start rebuilding grid cursor.
Edward Pikman
Independent Consultant