Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field exists on a view

Status
Not open for further replies.

Amferreira

Programmer
Mar 22, 2014
2
PT
How can check (programmatically) if a field exist on a view ?
 
FSIZE() will tell you the size of the column (or zero if not there).... but because of the DBASE history you need to have SET COMPATIBLE OFF first

so

Code:
SET COMPATIBLE OFF
IF FSIZE('mycolumn','myalias') > 0

or another way is to check

Code:
if !EMPTY(FIELD('mycolumn','myalias'))

in both cases if you omit the alias it defaults to the currently selected workarea.

hth

n
 
Hi,

You may want to have a look at the AFIELDS() function

hth

MarK
 
I'd vote against FSIZE(). HackFox explains the reason better than I could:

HackFox said:
FSIZE() is a horror show ... it's one of the few [functions] that is affected by the dreaded SET COMPATIBLE command ... Our advice: leave COMPATIBLE SMART (FoxPlus) and use ADIR() to get file sizes
.

... or, in this case, use AFIELDS() to get field sizes.

So you would use AFIELDS() to get all the field names from the view into an array. Then ASCAN() the array to see if the one you are interested in is present.

Mike





__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi,

or use the FCOUNT() and FIELD() functions

Code:
LOCAL lnFields

use lvMyView

lnFields = FCOUNT("lvMyView")

FOR i = 1 to lnFields
[indent]? FIELD(i, "lvMyView") [/indent]
ENDFOR

CLOSE ALL

hh

MarK
 
Mike,

fair point about set compatible.

i have it always off and don't change it ever (for fear of breaking other commands affected by it).

So FSIZE() is safe in that environment.

But FIELD() (or TYPE - hadn't thought of that) would be simpler than searching an array.

Odd in a way there's no direct command and we have to infer from other commands.

n
 
Hi,

...

or if you want a printout

LIST STRUCTURE NOCONSOLE TO FILE lvStructure.txt

hth

Mark

 
Hi

... would be simpler than searching an array.

hmm

Code:
LOCAL ARRAY laStructure[1]

use lvMyView

= AFIELDS(laStructure, "lvMyView")

IF ASCAN(laStructure, "MyFieldName", 1, -1, 1, 1) > 0
[indent]MyFieldName exists[/indent]
ELSE
[indent]MyFieldName does not exist[/indent]
ENDIF

CLOSE ALL

Btw, and in order to state the obvious, the two approaches differ: the combination FCOUNT() + FIELD() yields the name of the field whereas TYPE() and AFIELDS() + ASCAN() assume you know the field's name

hth

MarK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top