Setting field properties programmatically

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?

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")

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

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?

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

Got it, thanks.

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?

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

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

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

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?

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)

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.