Shortening formulas

A relative newbie colleague of mine hasn’t gotten registered with LDD yet, so I agreed to post for her. Formula language is not my strength, so hopefully someone will have some ideas.

Here’s what she says:

How do I shorten this? I am assuming I would use an @For statement, but I just can’t seem to work out the expressions.

Tcost := @If(Course_1 = “” ; 0 ; cp_SchCost_1_1 + cp_SchCost_1_2 + cp_SchCost_1_3 + cp_SchCost_1_4 +

cp_SchCost_1_5 + cp_SchCost_1_6 + cp_SchCost_1_7 + cp_SchCost_1_8 + cp_SchCost_1_9 +

cp_SchCost_1_10 + cp_SchCost_1_11 + cp_SchCost_1_12 + cp_SchCost_1_13 + cp_SchCost_1_14 +

cp_SchCost_1_15 + cp_SchCost_1_16 + cp_SchCost_1_17 + cp_SchCost_1_18 + cp_SchCost_1_19 +

cp_SchCost_1_20 + cp_SchCost_1_21 + cp_SchCost_1_22 + cp_SchCost_1_23 + cp_SchCost_1_24 +

cp_SchCost_1_25 +



cp_SchCostTM_1_1 + cp_SchCostTM_1_2 + cp_SchCostTM_1_3 + cp_SchCostTM_1_4 +

cp_SchCostTM_1_5 + cp_SchCostTM_1_6 + cp_SchCostTM_1_7 + cp_SchCostTM_1_8 + cp_SchCostTM_1_9 +

cp_SchCostTM_1_10 + cp_SchCostTM_1_11 + cp_SchCostTM_1_12 + cp_SchCostTM_1_13 + cp_SchCostTM_1_14 +

cp_SchCostTM_1_15 + cp_SchCostTM_1_16 + cp_SchCostTM_1_17 + cp_SchCostTM_1_18 + cp_SchCostTM_1_19 +

cp_SchCostTM_1_20 + cp_SchCostTM_1_21 + cp_SchCostTM_1_22 + cp_SchCostTM_1_23 + cp_SchCostTM_1_24 +

cp_SchCostTM_1_25 +



	cp_SchCostTech_1_1 + cp_SchCostTech_1_2 + cp_SchCostTech_1_3 + cp_SchCostTech_1_4 +

	cp_SchCostTech_1_5 + cp_SchCostTech_1_6 + cp_SchCostTech_1_7 + cp_SchCostTech_1_8 +

	cp_SchCostTech_1_9 + cp_SchCostTech_1_10 + cp_SchCostTech_1_11 + cp_SchCostTech_1_12 +

	cp_SchCostTech_1_13 + cp_SchCostTech_1_14 + cp_SchCostTech_1_15 + cp_SchCostTech_1_16 +

	cp_SchCostTech_1_17 + cp_SchCostTech_1_18 + cp_SchCostTech_1_19 + cp_SchCostTech_1_20 +



		cp_SchCostIR_1_1 + cp_SchCostIR_1_2 + cp_SchCostIR_1_3 + cp_SchCostIR_1_4 + cp_SchCostIR_1_5 +

		cp_SchCostIR_1_6 + cp_SchCostIR_1_7 + cp_SchCostIR_1_8 + cp_SchCostIR_1_9 + cp_SchCostIR_1_10 +



		cp_SchCostCF_1_1 + cp_SchCostCF_1_2 + cp_SchCostCF_1_3 + cp_SchCostCF_1_4 + cp_SchCostCF_1_5 +

		cp_SchCostCF_1_6 + cp_SchCostCF_1_7 + cp_SchCostCF_1_8 + cp_SchCostCF_1_9 + cp_SchCostCF_1_10 );

@If(@IsError(Tcost) ; 0 ; Tcost)

This is what i did in a similar field, but it didn’t add up the values…

cost := @For( i := 1 ; i <= 25 ; i := i + 1 ;

@Sum(@GetField("cp_Cost_1_" + @Text(i))));

costtm := @For( i := 1 ; i <= 25 ; i := i + 1 ;

@Sum(@GetField("cp_CostTM_1_" + @Text(i))));

costtech := @For( i := 1 ; i <= 20 ; i := i + 1 ;

@Sum(@GetField("cp_CostTech_1_" + @Text(i))));

costIR := @For( i := 1 ; i <= 10 ; i := i + 1 ;

@Sum(@GetField("cp_CostIR_1_" + @Text(i))));

costCF := @For( i := 1 ; i <= 10 ; i := i + 1 ;

@Sum(@GetField("cp_CostCF_1_" + @Text(i))));

Tcost := cost + costtm + costtech + costIR + costCF ;

@If(@IsError(Tcost) ; 0 ; Tcost)

Subject: Shortening formulas

The pieces of code that look like this:

cost := @For( i := 1 ; i <= 25 ; i := i + 1 ;

@Sum(@GetField(“cp_Cost_1_” + @Text(i))));

Need to be something like this:

cost := @For( i := 1 ; i <= 25 ; i := i + 1 ;

                 sum := sum + @GetField("cp_Cost_1_" + @Text(i)));

Subject: My answer

Tcost:=0;

@For( i := 1 ; i <= 25 ; i := i + 1 ;

Tcost:=Tcost + @GetField((“cp_SchCost_1_” + @Text(i))));

@For( i := 1 ; i <= 25 ; i := i + 1 ;

Tcost:=Tcost + @GetField((“cp_SchCostTM_1_” + @Text(i))));

@For( i := 1 ; i <= 20 ; i := i + 1 ;

Tcost:=Tcost + @GetField((“cp_SchCostTech_1_” + @Text(i))));

@For( i := 1 ; i <= 10 ; i := i + 1 ;

Tcost:=Tcost + @GetField((“cp_SchCostIR_1_” + @Text(i))));

@For( i := 1 ; i <= 10 ; i := i + 1 ;

Tcost:=Tcost + @GetField((“cp_SchCostCF_1_” + @Text(i))));

@If(@IsError(Tcost) ; 0 ; Tcost)

Subject: sorry, ignore that post, it’s late!

Subject: Thanks for all the suggestions; I’ll pass them on

Subject: Shortening formulas

I’m giving this a shot. However, I got an answer of 5 when I put in your @For code. I see why, too. @For returns TRUE (1). You have five @For statements, each returning a 1, then you add them all up.

I will post anything else I find here.

  • Matt