Proc with fieldname in variable doesn't work


#1

this is very similar to a problem i encountered before with a formulafill statement.

i have a proc that puts a fieldname in a variable named fld.
later there’s an arrayselectedbuild statement using that variable:
      arrayselectedbuild values, ¶, ‘’, datavalue(fld)

this doesn’t work. you get any array with all the elements being the fieldname.

i tried this because this is the trick that makes formulafill with a fieldname in a variable work:
      arrayselectedbuild values, ¶, ‘’, datavalue(’’ + fld)
this produces an array with all the elements being the value of the field in the current record.

so, i need a new trick.


#2

The surest way is to use the execute statement:

execute {arrayselectedbuild values, ¶, "",}+fld

#3

Or I think you could use the arrayselectedbuild( function.

values = arrayselectedbuild(cr(),"","«"+fld+"»")

The chevrons («») aren’t necessary if you’re sure the field name doesn’t contain any spaces or punctuation, but by including the chevrons you don’t have to worry about that.


#4

the execute statement works, but only if the variable is a global. if its a local you get a variable doesnt exist error.

i couldnt get the arrayselectedbuild( method to work at all.
the formula, in this case fld, has to be quoted.
this
      values = arrayselectedbuild(¶, ‘’, {«fld»})
gives you an array with every element being the fieldname.
same result with this:
      values = arrayselectedbuild(¶, ‘’, {datavalue(fld)})
this
      values = arrayselectedbuild(¶, ‘’, {datavalue("" + fld)})
yields an array with every element being the value of fld for the current record.

so, thanks, gary, and sorry, jim.

are we considering this something to be fixed or is this just the way its going to work?


#5

Is your variable fld a fileglobal in the same file as is active when the procedure is run? If it is from another file you could still use it with something like this:

setactivedatabase "MyOtherFile"
local fldname fldname=fld
setactivedatabase ""
executelocal {arrayselectedbuild values, ¶, "",}+fldname

#6

actually, the procedure is being farcalled from another database,
and the fld variable is local.
it’s the variable in the quoted formula (values) that had to be global, and i made it a fileglobal.


#7

Well, I guess I’m kind of lost as to exactly what you are doing and where the variables exist between the various files and their scope. Possibly you could use parameters with the farcall statement to transfer values back and forth between the files.


#8

Jim’s arrayselectedbuild( statement was missing the close parenthesis (since corrected). With that correction, his formula works for me.

The local variable wasn’t working in the execute, because an execute is a separate procedure, and the variable is not in scope in that procedure. It would probably work with a local if you used ExecuteLocal instead.


#9

i caught the omission of the close parentheses. that wasn’t the issue with me.

thanks for the explanation of why the local variable didn’t work. makes sense.