* THESE LITTLE UTILITY FILES HAVE SAVED ME A LOT OF HEADACHES.
* HOPE SOMEONE ELSE CAN GET SOME USE OUT OF THEM.

* Calvin Smith 


* Tired of checking if a DBF is open or not??
* USE : do to_use with "dbf1","dbf2","dbf3"
* sele dbf1
*********************
procedure to_use

parameters the_dbf1,the_dbf2,the_dbf3,the_dbf4,the_dbf5,the_dbf6
for x=1 to parameters()
	the_dbf=eval("the_dbf"+str(x,1))	
	if !used((the_dbf))
		sele 0
		use (the_dbf)
	endif
endfor

* Same as above but closes named dbfs
*********************
procedure to_close

parameters the_dbf1,the_dbf2,the_dbf3,the_dbf4,the_dbf5,the_dbf6
for x=1 to parameters()
	the_dbf=eval("the_dbf"+str(x,1))	
	if used((the_dbf))
		sele (the_dbf)
		use
	endif
endfor


* Reindexing routine that catches most errors and fixes them without
* the user being aware an error has occured
*****************
procedure re_index

set talk window
set talk on
close data
oldset=set("EXCLUSIVE")

set exclusive on

on error do err_open with error(),"auctions"
sele 0
use auctions
delete tag all
index on a_name tag a_name
* REPEAT FOR AS MANY INDEXED DBFS AS YOU HAVE
* restore original error handling routine

* Error handling routine for reindex procedure
***************
PROCEDURE ERR_OPEN
PARAMETERS T_NUM,T_DBF,FROM
if parameters()=2
	from="reindex"
endif
do case
	case t_num=19 OR T_NUM=114	&& index does not match
		close data
		del_file=t_dbf+".cdx"
		delete file &del_file						
		RETRY		
	case t_num=1707		&& structural index not found
		* ignore this and return unless called at beginning of prog
		* I have a section in the beginning of the program that checks
		* for the existance of certain CDX files.
		if from="start"			
			set exclu on		
			KEYBOARD "I"		&& IGNORE THE ERROR MESSAGE
			sele 0
			use &t_dbf			
			set talk window
			set talk on
			reindex			
			set exclu off			
			set talk off
		else			
			KEYBOARD "I"
			use &t_dbf			
		endif
	case 1=1
		if from="start"		&& try to fix the problem
			use_file=UPPER(t_dbf)+".DBF"
			if !file((use_file))
				do warn.spr with " The "+use_file+" file cannot be found",;
					" Call Tech Support Immediately to report this problem",;
					"If you DO NOT HAVE A BACKUP DO NOT RUN THIS",;
					"OR ANY OTHER PROGRAM BEFORE SPEAKING WITH TECH SUPPORT",;
					"", ;
					"If you have backup, call Support before RESTORING"
				QUIT
			else								
				* Shell to DOS here and run a dbf repair routine like HF
				* if this fixes the problem then reindex the files
				WAIT " Fixing problems with the "+t_dbf+" file " window nowait
				reset=.f.
				if SET("EXCLU")=="OFF"
					set exclu on
					reset=.t.
				endif
				on error DO NOTHIN		&& Shut off error handing entirely
				sele 0
				KEYBOARD "I"
				use &t_dbf			
				set talk window
				set talk on
				reindex
				set talk off
				if reset=.t.
					set exclusive off
				endif
			
			endif
		else
			do warn.spr with "A problem has been detected with your",;
			"files. Please run the FILE CHECK program and then",;
			"REINDEX your files",;
			"File Name : "+UPPER(t_dbf),;
			"If the problem persists, please call Technical Support",;
			""
			CLOSE DATA
			on error return
		endif
endcase
return
