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!

check existence of a field??

Status
Not open for further replies.

hmcheung

Programmer
Oct 13, 2000
66
HK
How can I check the existence of a field? I'd like to do something like this:

if <the field exists>=.f.
alter table <table_name> add column <col_name>
endif

Thanks! Please visit my WebCam!!
 
Try the afields() function to get the table fields into an array, and then use the ascan() function on that array to see if the field exists.



Pete Bloomfield
Down Under
 
hmcheung

The VARTYPE() function will return an error rather than &quot;U&quot;
if a field does not exist.

This error can be used to force the field addition, (this barebones code is untested)

ON ERROR DO AddField
WAIT WIND IIF(VARTYPE(TABLE.fieldname) # &quot;U&quot;,[Field exists],[])
ON ERROR &amp;&amp;Normal error handler

PROC AddField
alter table <table_name> add column <col_name>
ENDPROC


Chris
 
Hey guys, the most efficient way to check if field exists - fsize function. For example,
fsize('MyField') = 0 = .T. &amp;&amp; field does not exists
fsize('MyField') <> 0 = .T. &amp;&amp; field does exists

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
I too use the TYPE() command. If it returns &quot;U&quot; then the field doesn't exist.

IIF TYPE(&quot;Alias.FieldName&quot;) = &quot;U&quot; THEN
RETURN .F.
ELSE
RETUNR .T.
ENDIF
-Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top