Setting field properties programmatically


#1

Is there a way to assign a formula to a programmatically-created field programmatically? That is, I created a field by a procedure. Now I want to stick a formula in the field so it calculates automatically. I can do it by hand, of course, in the Field Properties panel.

How about an output pattern?


#2

Check out the setfieldproperties statement. Here is what you would use to add procedure code to the field options:

field YourField
setfieldproperties initializedictionary("PROCEDURE","your code entered here")

#3

Oops, I see you wanted the formula and not the code so replace “PROCEDURE” with “FORMULA” in the above example.


#4

Yes thanks, I figured that out. Can I set the output pattern in the same initializedictionary statement, or do I need a new initializedictionary statement?


#5

You can stuff that dictionary with as many properties as you like.


#6

Got it, thanks.


#7

So here’s the code that creates the field and assigns properties to it (the variables are set by a spinner and a menu respectively):

for i,1,fgAsgtNo
	addfield fgAsgtType+i
	field fgAsgtType+i
	fieldtype "float"
	endloop
addfield fgAsgtType+"Average"
field fgAsgtType+"Average"
setfieldproperties initializedictionary("FORMULA","sum(fgAsgtType)/count(fgAsgtType)","OUTPUTPATTERN","#.##","TYPE","Float")````

Everything works except the Formula. The field fgAsgtType+"Average" is created as a float field with the #.## output pattern, but nothing is calculated. The procedure does know what the fg variable is; it's creating the field and activating it. But it doesn't want to go from the variable to its contents. If I hand code it with the name of the line item field, it works fine; but somehow the dictionary doesn't get the message.  Well, I can see how that would be, I guess--how would the dictionary figure out a value that changes on the fly?

So is there a way to automate getting the correct formula in there when I'm creating a series of different line item fields? Seems as if I need to be passing a parameter, but where and how?

#8

The formula part should be written to use the fgAsgtType variable’s contents and not the variable’s name:

"FORMULA","sum("+fgAsgtType+")/count("+fgAsgtType+")"

#9

Bingo. I need to study this topic. Thanks as always!


#10

Gary, I can’t find the doc for using the “+…+” symbols to access the variable’s contents r/t its name. Can you point me in the right direction? Are the ++ just another kind of quotation mark?


#11

What Gary is doing is getting the fgAsgtType variable outside the quotes. Let’s say the content of fgAsgtType is the word “Exam”. Then his formula is concatenating 5 strings.

"sum("
+fgAsgtType
+")/count("
+fgAsgtType
+")"

You will notice that the first, third, and fifth strings are quoted text. The second and fourth are your variable, without quotes, so its content will be used there. The end result will be the string.

sum(Exam)/count(Exam)

#12

Got it. Funny how our eyes put things together; I incorrectly parsed ("+…+") as a unit rather than “sum(” as a unit. It’s obvious when you get the right gestalt.