Thanks Mike, that's a good extension of the cases, where you can't zap a cursor. In this case a cursor is used to add new data, so we cannot only assume, it's writable, it would be mysterious, if it was not and still could be used to append new records to it and be used in an editable grid.
If you tke the safe select code from Andy Kramek and delete all lines not necessary, as you start with an empty cursor and only want to clear/truncate it, what remains is:
Code:
lparameters tcsql, tcalias
local lnselect, lcsql
*** preserve work area
lnselect = select(0)
*** cursor does not exist
if not used( tcalias )
*** create it directly
create cursor (tcalias) (...fields....)
else
*** cursor does exist, truncate it
zap in (tcalias)
endif
*** restore work area and return status
select (lnselect)
return used( tcalias )
And if you create the cursor in the init or load of the form or the init of the grid you never switch current workarea, as ZAP in (tcalias) zaps the specified cursor and doesn't switch and also you don't create cursor as you did that in advance, then all which remains is
Code:
lparameters tcalias
zap in (tcalias)
You may add to SET SAFETY OFF before the ZAP and SET SAFETY ON, if you have saftey on, but that's all. And as you onyl have one alias, you can also hardcode that and not need oop or a grid class, you can do that where you want to start a new transaction.
What should be known is not all this code and the Andy Kramek article, but that a grid works well without the need to redefine column properties, if you work with a view and on top of that you should know that's because the view cursor is never destroyed but just refilled and then you can deduct how to do the same with a table or a readwrite cursor.
In case you use a view (local or remote), don't ZAP it, though, that would mean deletion of the records in it, and especially with updatable views would really delete these records in the backend table. But that's due to the updatable nature, you empty a view by setting view parameters in a way having no result and REQUERY(view) or you refresh it with current filters also by REQUERY(view). and in case of "normal" cursors you do so with ZAP and appending new data. Since VFP9 Andy Kramek could have updated his code using INSERT INTO cursor SELECT ... FROM tables, instead of using a temp alias curdummy, you can now directly insert query results into a table or cursor.
One more recommendation about using a form without restarting it: Split up code from the form load and init into portions only needed to be done once, eg creating the grid cursor, and code needed to reinitialise. The latter goes into a separate method reinit. Then init should call reinit at it's end and form reinitialisation can be done by calling reinit.
Bye, Olaf.