home *** CD-ROM | disk | FTP | other *** search
- *****************************************************************
- FUNCTION SELVALUE
- *****************************************************************
-
- * Creates a temporary file based on operator selections
-
- * Copyright(c) 1991 -- James Occhiogrosso
-
- # include 'inkey.ch'
- # include 'setcurs.ch'
- # include 'dl_keys.ch'
- # define MAX_NDX 5
-
- LOCAL old_cursor := SETCURSOR(SC_NONE)
- LOCAL mvar, selvar
-
- PRIVATE aborted, counter, db_file, fieldcnt, filt_strg, ;
- filt_word, fldnames, hilow, ndx_file, ndx_strg, ;
- ndx_word, num_flds, num_sels, pass, temp_file, var_prefix
-
- PARAMETERS say_proc, get_proc
- PRIVATE bSayProc := {|| &say_proc() }, bGetProc := {|| &get_proc()}
-
-
- * Initialize main variables
- db_file = ALIAS()
- aborted = .F.
-
- * Create unique temporary DBF file
- temp_file = TEMPFILE("DBF")
-
- IF EMPTY(temp_file)
- RETURN('')
- ENDIF
-
- * Display the screen
- CLEAR
- EVAL(bSayProc)
-
- * Outer loop is restarts only on an entry error
- DO WHILE .T.
- SELECT (db_file)
- * Initialize inner loop variables
- filt_strg := ndx_strg := hilow := ''
- counter := pass := 1
- num_flds := 0
-
- DO WHILE pass <= 2
- * Load array with field names
- fieldcnt = FCOUNT()
- fldnames := ARRAY(fieldcnt)
- AFIELDS(fldnames)
-
- CLRVARS()
- IF pass = 1
- hilow = ' LOW '
- filt_word = 'lofilt'
- var_prefix = 'lo_'
- filt_strg = ''
- ELSE
- hilow = ' HIGH '
- filt_word = 'hifilt'
- var_prefix = 'hi_'
- ENDIF
-
- * Get selection values from operator
- TONE(2000,1)
- GETSEL(fldnames)
-
- * Get number of selection field entries
- num_flds = counter - 1
-
- IF num_flds != 0
- * Fill array with selections
- num_sels := ARRAY(num_flds)
- ACOPY(fldnames, num_sels, 1, num_flds, 1)
-
- * Create lo/hi filt1 thru filt "n" variables,
- * each containing 15 parses (4 fields)
-
- MAKFILT(num_sels)
-
- * Create lo_mvar and hi_mvar variables
- FOR counter = 1 TO num_flds
- mvar = 'm' + num_sels[counter]
- selvar = var_prefix + num_sels[counter]
- &selvar = &mvar
- NEXT
- filt_strg = IF(pass = 1, filt_strg + ' .AND. ', ;
- filt_strg)
- RELEASE num_sels
- ELSE
- * No selections entered by operator
- IF pass = 2
- * If second pass remove ' .AND. '
- filt_strg = SUBSTR(filt_strg, 1, ;
- LEN(filt_strg) - 7)
- ENDIF
- ENDIF
- pass++
- ENDDO
-
- * Get operator's desired index fields
- TONE(2000,1)
- MAKNDX(fldnames)
- IF aborted
- EXIT
- ENDIF
- @ 20, 0 CLEAR TO 24, 79
- CENTERON(22, 'Creating report file. Please wait. ')
- IF LEN(filt_strg) > 0
- COPY TO (temp_file) FOR &filt_strg
- ELSE
- COPY TO (temp_file)
- ENDIF
-
- USE (temp_file) NEW
-
- * No records in temporary file, query operator
- IF RECCOUNT() = 0
- ?? CHR(7)
- CENTERON(24, 'No records selected. ' + ;
- 'Do you want to try again? Y/N ')
- ans = .T.
- @ 24, COL() -2 GET ans PICTURE 'Y'
- READ
- IF ans
- * Repaint screen if operator wants to reenter
- EVAL(bSayProc)
- @ 20, 0 CLEAR TO 24, 79
-
- * Close the temporary file and loop to beginning
- USE
- LOOP
- ELSE
- * Otherwise, return with aborted true
- aborted = .T.
- EXIT
- ENDIF
- ELSE
- * Create index.
- @ 24, 0
- CENTERON(22, 'Indexing report file. ' + ;
- LTRIM(STR(LASTREC())) + ' records.')
-
- ndx_file = SUBSTR(temp_file,1,8)
- INDEX ON &ndx_strg TO (ndx_file)
- ENDIF
- EXIT
- ENDDO WHILE .T.
-
- SETCURSOR(old_cursor)
-
- IF aborted
- * If aborted for any reason, erase all temporary files
-
- IF UPPER(ALIAS()) + '.DBF' = UPPER(temp_file)
- * If temporary file is still open, close it.
- USE
- * And reselect the original DBF file
- SELECT (db_file)
- ENDIF
-
- * Erase temporary files
- IF FILE(temp_file)
- ERASE (temp_file)
- ENDIF
-
- * Erase temporary DBT file
- temp_file = SUBSTR(temp_file, 1, 11) + 'T'
- IF FILE(temp_file)
- ERASE (temp_file)
- ENDIF
-
- * Erase temporary index file
- temp_file = SUBSTR(temp_file, 1, 8) + INDEXEXT()
- IF FILE(temp_file)
- ERASE (temp_file)
- ENDIF
-
-
- * Set return value to null to indicate no report file
- temp_file = ''
- ENDIF
-
- RETURN(temp_file)
-
-
- *****************************************************************
- STATIC PROCEDURE GETSEL
- *****************************************************************
-
- * Get low and high selection values
-
- LOCAL varname
-
- PARAMETERS fldnames
-
- CLRVARS()
- DO WHILE .T.
- * Get selections for all memory variables
- ans = .F.
- DO WHILE .NOT. ans
- EVAL(bGetProc)
- @ 21, 5 SAY 'Enter ' + SPACE(LEN(hilow)) + ;
- ' selection values. Press PgDn when done.'
- SETCOLOR(colbarhi)
- @ 21, 11 SAY hilow
- SETCOLOR(colstd)
- @ 23, 5 SAY 'You may enter data in a maximum of ' + ;
- '10 field areas'
- SETCURSOR(SC_INSERT)
- READ
- ans = .T.
- @ 21, 58 SAY ' Finished? Y/N ' GET ans PICTURE 'Y'
- READ
- @ 21, 58 CLEAR TO 23, 79
- ENDDO
- SETCURSOR(SC_NONE)
- @ 21, 0
- * Remove unused fields from array
- counter = 1
- DO WHILE TYPE("fldnames[counter]") # 'U'
- varname = 'm' + fldnames[counter]
- IF EMPTY(&varname)
- ADEL(fldnames,counter)
- IF counter = LEN(fldnames)
- EXIT
- ENDIF
- LOOP
- ENDIF
- counter++
- ENDDO
-
- * Loop if operator entered too many selection fields.
- * Maximum is 10 high plus 10 low.
-
- IF counter-1 > 10
- ?? CHR(7)
- CENTERON(24,'Too many selection fields. Maximum is 10.')
- LOOP
- ELSE
- EXIT
- ENDIF
-
- ENDDO WHILE .T.
- @ 24, 0
-
- RETURN
-
-
- *****************************************************************
- STATIC PROCEDURE MAKNDX
- *****************************************************************
- *
- * Create an index expression
-
- * Clear screen and redisplay memory variables
- CLRVARS()
- EVAL(bGetProc)
-
- @ 20, 0 CLEAR TO 24, 79
-
- @ 20, 7 SAY 'Position cursor on beginning of field(s) you ' + ;
- 'want to index on.'
- @ 21, 7 SAY 'Press plus key to select. Up to 5 fields can ' + ;
- 'be selected in '
- @ 22, 7 SAY 'any order. Selected order will display. Press ' + ;
- 'PgDn when done.'
-
- ans = .F.
- DO WHILE .NOT. ans
- counter = 0
- ndx_strg = ''
- ndx_word = ''
-
- * Set the "+" key to call FINDVAR procedure
- SET KEY K_PLUS TO FINDVAR
- EVAL(bGetProc)
- SETCURSOR(SC_INSERT)
- READ
- SET KEY K_PLUS TO
- ans = .T.
- TONE(2000,1)
- CENTERON(24, 'To abort, press Esc. -- Otherwise, ' + ;
- 'are index selections correct? Y/N ')
- @ 24, col()-4 GET ans PICTURE 'Y'
- READ
- @ 24, 0
- SETCURSOR(SC_NONE)
- IF ans .AND. .NOT. EMPTY(ndx_strg) .AND. LASTKEY() != K_ESC
- EXIT
- ELSEIF LASTKEY() = K_ESC
- aborted = .T.
- EXIT
- ELSEIF EMPTY(ndx_strg)
- ?? CHR(7)
- CENTERON(24, 'At least one index must be selected. ' + ;
- 'Please reenter. ')
- ans = .F.
- ENDIF
-
- EVAL(bSayProc)
- ENDDO
-
- RETURN
-
-
- *****************************************************************
- STATIC PROCEDURE MAKFILT
- *****************************************************************
-
- * Create filter statement from selected fields
-
- LOCAL filt_cnt := 1, filt_var := ''
-
- PARAMETERS num_sels
-
- counter := 1
- DO WHILE (counter <= num_flds .OR. counter = 1)
- filt_var = filt_word + LTRIM(STR(filt_cnt))
- &filt_var = ''
- FOR counter = counter TO counter + 3
- IF TYPE('num_sels[counter]') = 'C' .AND. pass = 1
- &filt_var = &filt_var + num_sels[counter] + ' >= ' +;
- var_prefix + num_sels[counter]
- ELSEIF TYPE('num_sels[counter]') = 'C' .AND. pass = 2
- &filt_var = &filt_var + num_sels[counter] + ' <= ' +;
- var_prefix + num_sels[counter]
- ENDIF
- IF (counter % 4) != 0 .AND. counter < num_flds
- &filt_var = &filt_var + ' .AND. '
- ELSE
- filt_cnt++
- counter++
- EXIT
- ENDIF
- NEXT
- ENDDO
-
- * Create filter string
- counter = 1
- filt_cnt--
- DO WHILE counter <= filt_cnt
- filt_var = filt_word + LTRIM(STR(counter))
- filt_strg = filt_strg + &filt_var
- IF counter # filt_cnt
- filt_strg = filt_strg + ' .AND. '
- ENDIF
- counter++
- ENDDO
- RETURN
- *
-
-
- *****************************************************************
- STATIC PROCEDURE FINDVAR
- *****************************************************************
-
- /* Mark field for indexing
-
- Loads the current GET variable into ndxfld, and displays
- a number next to the selected GET field (in highlighted color).
- The index expression is stored in ndx_word and then added to
- ndx_strg in the caller to form an index expression.
-
- To keep index sizes manageable, character fields are indexed
- on the first 10 characters only. Number of allowed indexes is
- limited to 5 to keep run times reasonable. If more are needed,
- change the MAX_NDX constant defined at the top of the file.
-
- */
-
- LOCAL fldlen := 0, fldlens := {}, ndxfld, subscrp
- LOCAL old_color := SETCOLOR(colblink)
-
- PARAMETERS callproc, linenum, inputvar
-
- * Clear the hot key
- SET KEY K_PLUS TO
- ndxfld = inputvar
-
-
- * Determine data type for index expression
-
- IF TYPE(ndxfld) = 'D'
- ndx_word = 'DTOS(' + SUBSTR(ndxfld,2) + ')'
- fldlen = LEN(DTOC(&ndxfld)) + 1
-
- ELSEIF TYPE(ndxfld) = 'C'
- * Index only on first 10 characters
- IF LEN(&ndxfld) > 10
- ndx_word = 'SUBSTR(' + SUBSTR(ndxfld,2) + ', 1, 10)'
- ELSE
- ndx_word = SUBSTR(ndxfld,2)
- ENDIF
- fldlen = LEN(&ndxfld) + 1
-
- ELSEIF TYPE(ndxfld) = 'N'
- ndx_word = 'STR(' + SUBSTR(ndxfld,2) + ')'
- fldnames := ARRAY(fieldcnt)
- fldlens := ARRAY(fieldcnt)
- AFIELDS(fldnames, '', fldlens)
- subscrp = ASCAN(fldnames, SUBSTR(ndxfld,2))
- fldlen = fldlens[subscrp] + 1
-
- ELSE
- ndx_word = ''
- fldlen = 0
-
- ENDIF
-
- counter++
- IF counter <= MAX_NDX
- * Display index number and force jump to next field
- SWAPCOLOR(ROW(), COL() - 1, LTRIM(STR(counter)))
- KEYBOARD CHR(13)
- ENDIF
-
- IF counter = 1
- ndx_strg = ndx_word
- ELSEIF counter <= MAX_NDX
- ndx_strg = ndx_strg + ' + ' + ndx_word
- ELSE
- * Stuff PgDn in keyboard to force exit from read
- KEYBOARD CHR(3)
- ENDIF
-
- SETCOLOR(old_color)
- SET KEY K_PLUS TO FINDVAR
- RETURN
-
-