Wiki Home

Test The Presence Of AField


Namespace: VFP
There are different ways to test the presence of a field in a table.
One of them is:
if fsize("FieldToTest") > 0
  ***The field exists in the table
endif

Note that the Hackers Guide says that "FSIZE() is a horror show." Set compatible makes or breaks fsize(). -- Tom Cerul


The point then is not to change SET COMPATIBLE. Leave it so that FSIZE can be used for fields and use ADIR to check for file size. It's a poor design decision to name something FSIZE - and then have the F stand for either Field or File. It would have been far clearer to have FILESIZE and FIELDSIZE. -- Mike Yearwood

This function will return a value of "U" if the specified field does not exist.
if type("alias.FieldToTest") # "U"
    ***The field exists
endif

The most reliable solution to test the presence of a field is:
for lnCount=1 to fcount()
    if upper(field(lnCnt))=="FIELDTOTEST"  && Note all capitals
    ****The field exists
      exit
    endif
next

It is the slowest way to do it.
This is another way in VFP 8...
IF NOT EMPTY(FIELD("FieldToTest"))
  ****The field exists.
ENDIF
-- Mike Yearwood

Mike, as far as I can tell, Field() takes an integer argument so this doesn't work. -- Tom Cerul

My version is
IF AFIELDS(flist)>0 AND ASCAN(FLIST, UPPER(ALLTRIM("FieldToTest")))>0
****The field exists.
ENDIF

Actually, my version is for VFP 8 and up. -- Mike Yearwood
Contributors Eduardo Díaz Guerrero, Mike Yearwood, Tom Cerul
Category Tips And Tricks
( Topic last updated: 2011.09.19 01:55:07 PM )