Here is procedure that I wrote a long time ago. I just spent a few minutes cleaning it up. It still could use a little more error-handling, like ensuring the table exists, but it is a good start.
Rather than using this type of logic all the time:
IF USED('myTable')
SELECT myTable
ELSE
SELECT 0
USE myTable
ENDIF
I just call the function to select the table, and open it if necessary. It also provides a nice message back if the index does not exist. Copy all this code in prg and run it. You will notice a message comes back saying the index does not exist. Is the type of trapping you are looking for?
CREATE table boo (name C(10))
selecttable('boo','myidx')
*****************************************************************************
PROCEDURE SELECTTABLE
PARAMETER tctbl, tcidx
* called to ensure a table is already open and the proper order is set
* if the table is not opened, it is opened.
* if the table is open - we ensure it is active and the order is
* checked to ensure it is correct
* if no order is passed, we will not do anything with the order
* Note: some of the macros may be able to be removed later.
* additional test for performance should be done when time permits.
LOCAL llFoundit, lctagname, lnCount
IF !USED('&tctbl')
SELECT 0
USE (tctbl)
ELSE
SELECT (tctbl)
ENDIF
llFoundit = .F.
IF PCOUNT() = 2
IF UPPER(ALLTRIM(ORDER())) <> UPPER(ALLTRIM('&tcidx'))
FOR lnCount = 1 TO 254 && vfp max # of tags is 254
lctagname = TAG(lnCount)
IF !EMPTY(lctagname) && Chks for tags in the index
IF UPPER(ALLTRIM(lctagname)) == UPPER(ALLTRIM(tcidx))
* We found the tag
llFoundit = .T.
EXIT
ENDIF
ELSE
EXIT && Exit the loop when no more tags are found
ENDIF
ENDFOR
IF llFoundit
SET ORDER TO TAG &tcidx
ELSE
MESSAGEBOX("WARNING!!!" + CHR(13)+CHR(10)+CHR(13)+CHR(10) + ;
"Tag: " + tcidx + " was not found on table: " + tctbl)
ENDIF
ENDIF
ENDIF
Jim Osieczonek
Delta Business Group, LLC