Wiki Home

Question About Iterating Fields


Namespace: WIN_COM_API
for x = 4 to fcount() then
	if empty(evaluate("stud_with_conflict."+field(x))) then
		replace stud_with_conflict.field(x) with thisform.subject.list(thisform.subject.listindex)
	endif
endfor


How to replace the value in a field but if empty(field(x)) go to next field...


---
First portion of code is for testing (make it easy to cut and paste into your environment )
The FOR - ENDFOR uses an EVALUATE() example
A reference to the field is contructed with "lcField" so it can be used multiple times if needed.
The replace statement uses macro substitution (that's the use of the "&" )

***************************************
* add a form with a listbox to test the code
* create the cursor and add a record
OF = CREATEOBJECT( 'Form' )
OF.ADDOBJECT( 'list1', 'listBox' )
olist = OF.CONTROLS(1)
olist.ADDITEM( 'item1' )
olist.ADDITEM( 'item2_replaced' )
olist.ADDITEM( 'item3' )
olist.ADDITEM( 'item4_replaced' )

CREATE CURSOR stud_with_conflict(;
	field4 c(15),;
	field5 c(15),;
	field6 c(15),;
	field7 c(15))
INSERT INTO stud_with_conflict VALUES( 'itemx1', '', 'itemx3', '' )
*
*************************************

*************************************
* IF the field is empty -  replace field with value from listbox
LOCAL lcField

FOR x = 4 TO FCOUNT() + 3
	lcField = 'stud_with_conflict.field' + ALLTRIM( STR(x))
	IF EMPTY(EVALUATE( lcField ) ) THEN
		REPLACE &lcField WITH olist.listitem( x -3 )
	ENDIF
ENDFOR
*
*************************************


Life's good. Ron Olson

Indirection is quicker.

REPLACE (lcField) with olist.listitem( x-3 )
( Topic last updated: 2008.04.03 08:41:22 AM )