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

Strange Compilation Error?

Status
Not open for further replies.

trphil

Programmer
Apr 5, 2006
1
GB
I created a function for use in a report to return address details from a table by the specified field name as follows:-

function cwgetbranch
lparameters lcbranch,lcfield
local lcalias,lcreturn
lcreturn = ""
lcalias = alias()
if sqopen("S","cware") &&Opens a table shared and selects it.
afields(lafields)
if ascan(lafields,upper(lcfield)) > 0
locate for alltrim(left(cw_code,2)) == alltrim(upper(left(lcbranch,2)))
if found()
lcreturn = &lcfield
endif
else
lcreturn = "#INVALID FIELD NAME#"
endif
sqclose("cware") &&Closes the table again.
if !empty(lcalias)
select &lcalias
endif
endif
return lcreturn

I compiled this and it worked fine. When I deployed it on a customer site it didn't work (returned "" for every enquiry). I checked through all the things that could cause the problem and they were all fine. I tested this on another PC in my office and it was the same as on the customer site.

I added an else clause on the found as follows:-

if found()
lcreturn = &lcfield
else
lcreturn = ""
endif

compiled again and it all worked fine.

I then removed the else clause again, recompiled and it still all worked fine.

Any ideas what could have happened?
 
Macro substitution (&lcfield) can sometimes be flakey. You should maybe use Evaluate(lcfield) instead.
Other than that, no telling. Corrupted compilation maybe?

I don't really see anything wrong with the logic.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
If SET EXACT isn't ON or a field name might be the same 'name' as other array items you'll get a false hit.

You should ASCAN only the 1st column of the array.

Code:
CLEAR 
CREATE TABLE test1 (a c(1), n n(1), field1 d)
CREATE TABLE test2 (a c(1), b n(1), field1 d)

INSERT INTO test1 VALUES ('X', 7, DATE())
INSERT INTO test2 VALUES ('X', 7, DATE())


AFIELDS(a1, 'test1')
AFIELDS(a2, 'test2')

SELECT test1
? test_for_field('n', @a1) && field N does exist
? test_for_field_exact('n', @a1) && field N does exist, 7 returned

SELECT test2
? test_for_field('n', @a2) && field N does NOT exist
? test_for_field_exact('n', @a2) && field N does NOT exist

PROCEDURE test_for_field
LPARAMETERS cField, aArray

cField=UPPER(cField)

nHit = ASCAN(aArray, cField)

IF nHit = 0
	RETURN "#INVALID FIELD NAME#"
ELSE
	LOCATE &&for condition
	RETURN TRANSFORM(aArray(nHit)) + ': EVAL not done because of false hit'
ENDIF 

PROCEDURE test_for_field_exact
LPARAMETERS cField, aArray

cField=UPPER(cField)

nHit = ASCAN(aArray, cField, 1, ALEN(aArray,1), 1, 2 + 4)

IF nHit = 0
	RETURN "#INVALID FIELD NAME#"
ELSE
	LOCATE &&for condition
	RETURN EVALUATE(aArray(nHit))
ENDIF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top