Function IndexKey() Foundation

Returns the index expression of an index file.

Syntax
IndexKey( <nIndex> ) --> cIndexKey
Parameters
<nIndex>
<nIndex> is a positive integer specifying the ordinal position of the index file in a work area whose index expression is returned. The index files are numbered in the order that they were opened with DbSetIndex() or SET INDEX TO.
Return

The return value of IndexKey() is a character string containing the index expression of the index file specified by <nIndex> in a work area. If no index file exists corresponding to <nIndex>, a null string ("") is returned. If the value zero is specified for <nIndex>, the index expression for the current controlling index is returned.

Description

The function IndexKey() exists for compatibility reasons. The function OrdKey() should be used instead of IndexKey().

The index function IndexKey() determines the key expression of an index in a work area. When the function is used without the alias operator, it returns an index expression from the current work area.

Knowing the index expression of the controlling index is very important during data input routines. If data input changes the logical record order of a database file, the screen must be refreshed after the input. This is often required when data is displayed in the BROWSE mode either by the function DbEdit() or using a TBrowse object. The function IndexKey() allows the index expression and the current index value to be determined before and after the data input. If the index value after input differs from the index value before input, the screen display must be refreshed.

Examples
IndexKey()
// The example shows the return values of IndexKey(). 

PROCEDURE Main 
   USE Customer NEW EXCLUSIVE 

   INDEX ON CUSTNO                    TO CustA 
   INDEX ON Upper(LASTNAME+FIRSTNAME) TO CustB 

   SET INDEX TO CustA, CustB 

   ? IndexOrd()               // result: 1 
                              ** Controlling index is CUSTA 

   ? IndexKey(0)              // result: CUSTNO 
   ? IndexKey(1)              // result: CUSTNO 
   ? IndexKey(2)              // result: Upper(LASTNAME+FIRSTNAME) 
RETURN 

IndexKey() with data input

// In the example, a simple TBrowse object is shown which 
// displays the fields of a DBF file in BROWSE mode. 
// When the enter key is pressed, the current field can be 
// edited. Before data input, the current value of the index 
// expression is stored for comparison with the value after 
// data input. If the index values before and after data input 
// do not match, the RefreshAll() message is sent to the TBrowse 
// object in order to redisplay all data on the screen. 

#include "Inkey.ch" 

PROCEDURE Main 
   LOCAL cIndexKey, xIndexVal, cField, oTBrowse 
   LOCAL nKey, n, nMax 

   USE Customer NEW EXCLUSIVE      // open file 
                                   // generate indexes 
   INDEX ON CUSTNO                    TO CustA 
   INDEX ON Upper(LASTNAME+FIRSTNAME) TO CustB 

   SET INDEX TO CustA, CustB 
   SET ORDER TO 2 

   nMax     := FCount() 
   oTBrowse := TBrowseDB()         // generate Tbrowse object 
   FOR n:=1 TO nMax 
      oTBrowse:AddColumn(; 
          TBColumnNew( FieldName(n), FieldBlock( FieldName(n)) ) ; 
      ) 
   NEXT 

   nKey := 0 
   DO WHILE nKey <> K_ESC          // display fields with TBrowse 

      oTBrowse:forceStable() 

      nKey := Inkey(0) 

      DO CASE 
      CASE nKey == K_UP    ;   oTbrowse : Up() 
      CASE nKey == K_DOWN  ;   oTbrowse : Down() 
      CASE nKey == K_LEFT  ;   oTbrowse : Left() 
      CASE nKey == K_RIGHT ;   oTbrowse : Right() 

      CASE nKey == K_RETURN           // begin data input 
                                      // determine current field 
         cField    := FieldName( oTbrowse:ColPos ) 

         cIndexKey := IndexKey(0)     // determine index expression 
         xIndexVal := &( cIndexKey )  // determine index value 

         @ Row(),Col() GET &cField    // data input in the field 
         READ 

         IF xIndexVal <> &(cIndexKey) // compare index value 
            oTbrowse:refreshAll()     // redisplay everything 
         ENDIF 
      ENDCASE 
   ENDDO 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.