Function FieldPos() Foundation

Returns the position of a field in a work area.

Syntax
FieldPos( <cFieldName> ) --> nFieldPosition
Parameters
<cFieldName>
<cFieldName> is a character string containing the name of a field in a work area.
Return

The return value of FieldPos() is an integer containing the position of the field named <cFieldName> within the work area. When <cFieldName> is not a valid field name in the work area, the value zero is returned.

Description

The database function FieldPos() returns the position of an individual field within a record of a work area. If the function is used without the alias operator, the field position from the current work area is returned. The counterpart of FieldPos() is the function FieldName() which returns the name of the field in the record from the field position.

The function FieldPos() is frequently used with FieldGet() and FieldPut(). For example, this is done in programming generic read and write functions which operate without knowledge of the underlying data structure.

Examples
FieldPos()
// The example demonstrates the most common use for FieldPos(). 

PROCEDURE Main 
   LOCAL cName 

   USE Customer NEW 

   ? cLastName := FieldGet(FieldPos("LASTNAME")) // result: King 

   ? FieldPos("LASTNAME")                        // result: 2 

   USE 
RETURN 

Generic code block to read values from fields
// In the example, a code block is assigned to a file-wide 
// visible STATIC variable which can return the value of 
// any field from any work area. The alias name of a work 
// area and the desired field name must be passed to it as 
// a character string. 

STATIC sbReadField := ; 
       { | cAlias, cFieldName | ; // parameter list for code block 
         ( Select(cAlias) )-> ;   // select work area 
         ( FieldGet( FieldPos(cFieldName) ) ) ; 
       } 

PROCEDURE Main 

   USE Customer  ALIAS Cust NEW 
   USE Invoice   ALIAS Inv NEW 
   USE InvItem   ALIAS Items NEW 

   ? Eval(sbReadField, "CUST", "LASTNAME")   // result: King 
   ? Eval(sbReadField, "INV" , "INVDATE")    // result: 12/6/94 
   ? Eval(sbReadField, "ITEMS", "Item")      // result: Xbase++ 

   CLOSE DATABASES 
RETURN 

Determine the structure of a field
// The example shows the UDF FieldStruct() which 
// determines the structure of a field using FieldPos() 
// and DbStruct(). 

PROCEDURE Main 
   USE Customer   ALIAS Cust NEW 
   USE Invoice    ALIAS Inv  NEW 

   ? FieldStruct("INVNO")          // result: {"INVNO","C",6,0} 

   ? Cust->(FieldStruct("LASTNAME")) // result: {"NAME","C",20,0} 

   CLOSE DATABASES 
RETURN 

FUNCTION FieldStruct(cFieldName) 
   LOCAL nFieldPos := FieldPos(cFieldName) 
RETURN IIf(nFieldPos== 0,{}, DbStruct()[nFieldPos] ) 

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.