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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's command to check if already exist a column in a free table

Status
Not open for further replies.

pomodoro

Programmer
Apr 18, 2003
6
IT
Hello,
What is the command to check if already exist a column in a free table ?
Thanks


 
You can use VARTYPE() for this. If it returns U, then the field is not present. Example:

[tt]SELECT Customer
IF VARTYPE(Cust_Name) = "U"
* Field Cust_Name does not exist
ENDIF
[/tt]

But be careful. If Cust_Name exists as a variable, then the function won't return U.

An alternative would be to use AFIELDS() to get the field names into an array:

[tt]AFIELDS(laFields, "customer")
IF ASCAN((laFields, "CUST_NAME", -1, -1, 1) = 0
* Field Cust_Name does not exist
ENDIF[/tt]

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well, you can check VarType(Customer.Cust_Name). But even that might also be a property Cust_Name of an object Customer. Field existence can be determined with FIELD():
Code:
USE customer.dbf
IF EMPTY(FIELD("cust_name","customer"))
   * field cust_name doesn't exist in workarea customer
Endif

Bye, Olaf.
 
I made a function that i use often, especially when doing updates to the tables.

Code:
? FieldExist('ARCust','CustNO') && Table ARCust, Field = 'custNo



FUNCTION FieldExist(tcTable,tcField)

	IF NOT USED(tcTable)
                use (tcTable) in 0 
	ENDIF

	llFound	= .F. && not found
	SELECT (tcTable)
	FOR gnCount = 1 TO FCOUNT( )  && Loop for number of fields
		IF UPPER(ALLTRIM(FIELD(gnCount))) == UPPER(ALLTRIM(tcField))  && Display each field
			llFound = .T. && found it
			EXIT
		ENDIF
	NEXT
	RETURN llFound
ENDFUNC

Ez Logic
Michigan
 
You're ALMOST right Vartype(fdsjkhfsdkjh.fhjksdhfk) errors "Alias 'FDSJKHFSDKJH' is not found". Anyway, it will not have that problem if tha table is open. FIELD() too.

Bye, Olaf.
 
But if the alias is found, but the field is not, I think you will get "Variable not found".

So, if you want to know if your Customer table contains a field called Company, and if you do this:
[tt]
USE Customer
? VARTYPE(Customer.Company)[/tt]

then you will get C (or some other letter, but not U) if the test succeeds. But you will get the error message if the test fails.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I see, anyway, the final recommendation is FIELD(), not Varttype. I was just trying to avoid the problem of just testing Vartype(field) and getting a type that isn't a field but a variable type.

FIELD() always will look into fields, not variables. and Field("wrongfieldname") will not error, just be empty.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top