Expand my Community achievements bar.

*** FormCalc syntax error? Looks correct by multiple people. What am I missing? ***

Avatar

Former Community Member
Hi All,



I have been stumped on this FormCalc code for a few days now. If anyone can give me a hand to get this back on track, that would be great. It's the only thing holding up the completion of this snow removal datasheet.



Here is the code:



if (SiteCode1 == "W400") then (if (Accum1 < 1) then (0) elseif (Accum1

<= 3) then (1300) elseif (Accum1 <= 6) then (2000) elseif (Accum1 <=

9) then (2700) elseif (Accum1 <= 12) then (3400) elseif (Accum1 <= 15)

then (4100) endif) endif



The error is as follows:



"Script Failed (language is formcalc; context is

xfa[0].form[0].#subform[0].Table1[0].Row1[4].PerPush1[0])

script=if (SiteCode1 == "W400") then (if (Accum1 < 1) then (0) elseif

(Accum1 <= 3) then (1300) elseif (Accum1 <= 6) then (2000) elseif

(Accum1 <= 9) then (2700) elseif (Accum1 <= 12) then (3400) elseif

(Accum1 <= 15) then (4100) endif) endif

Error: syntax error near token 'then' on line 1, column 51.



Something I have noticed, if I take away the first <i> "If (SiteCode1

=="W400") then (" </i> and lose the last<i> ") endif ," </i> the

formula works fine. however, that would mean that I need to dedicated

a form per project location.



Any help would be greatly appreciated, I have spent hours on this

trying to get it figured out, and no matter the values and binding

things I change, I cant seem to get this working.



Thanks in advance!

Nick
9 Replies

Avatar

Level 5
Noticed you have values for different matching conditions however it is missing variable name that the value gets assigned to in each case. Also it would be simple and easy to understand the formula if we put it in the following format rather than using multiple elseif.



var valueA

if (SiteCode1 == "W400") then

if (Accum1 < 1) then

valueA = 0

endif

if (Accum1 >1 and Accum1 <= 3) then

valueA = 1300

endif

if (Accum1 >3 and Accum1 <= 6) then

valueA = 2000

endif

if (Accum1 >6 and Accum1 <= 9) then

valueA = 2700

endif

if (Accum1 >9 and Accum1 <= 12) then

valueA = 3400

endif

if (Accum1 >12 and Accum1 <= 15) then

valueA = 4100

endif

endif



try simple changes and let us know the results.

Avatar

Level 7
I do not recommend this approach. Else is a much cleaner and safer way

to code, without leaving errors, especially cases that match twice or

not at all. Notice that this code does not change valueA if it is

exactly 1. This is easy correct, but in my view it's better to avoid a

style which invites the mistake.



Coming back to the original code, I don't know the full rules for

FormCalc, but I'm surprised to see "then (if". What is the bracket

for?



Aandi Inston

Avatar

Former Community Member
I tried the above code, and it makes sense, but that doesnt work either.



Furthermore, I attempted to add similar code to repsective fields, and the code doesn't seem to stick to the field. I'm going to post my form on my website for people to look at it.



Here's the link



http://the-hitchcock-companies.com/WEBish/SnowRemovalDataSheet1.pdf



If anyone has some input, that would be great.



Thanks

Nick

Avatar

Level 5
I see many reasons why it does not work.

1. You are just using Accum1, Accum2.... rather than using relative reference to the actual field values.

Example:form1.#subform[0].Table1.Row1[0].Accum1.rawValue

2. If you want to calculate the some thing in multiple rows you should have code with reference to that particular row like:

$.parent.Accum1.rawValue

so that the same formula will be used over and over in multiple rows.

3. Calculate event is not my preference and never use myself. Rather I prefer using exit event of some input field. This field you are trying to calculate is not being accepting input.



Comment: This template looks like your one of the fist few to design using LC. It is a good start to learn the things.

I thought I could help get pass some hurdles. This template requires lot changes in code to meet requirements.

Avatar

Former Community Member
It is one of the first using calculations. I typically use MSExcel for my math items. I use LC alot for forms that have no linking between fields. I'm trying to convert because I like the "polished" look of Adobe items.



I have been working on this for a few days trying to get good FormCalc practice, also reading the scripting reference, but the examples in the reference aren't very good, and seem choppy.



Granted. I'm no programmer, however, I'm a stubborn Greek and will not give up until something is completed or resolved.



Any insight will be helpful at this point.



Thanks

Avatar

Level 7
One thought: FormCalc is designed to be simpler than JavaScript, for

simple tasks. But I'd say that your task is already beyond the point

where FormCalc offers any advantages. Your time would probably be more

profitably spend learning JavaScript. Another advantage would be that

many more people can help with JavaScript.



Be under no illusion: while you may say you are not a programmer, each

time you write a program you are learning to become a programmer. And

those are the skills you need to develop to complete your task.



Aandi Inston

Avatar

Level 7
Consider using the "if the elsif" variaiotn



var valueA = null

if (SiteCode1 == "W400") then

if (Accum1 < 1) then

valueA = 0

elseif (Accum1 >1 and Accum1 <= 3) then

valueA = 1300

elseif (Accum1 >3 and Accum1 <= 6) then

valueA = 2000

elseif (Accum1 >6 and Accum1 <= 9) then

valueA = 2700

elseif (Accum1 >9 and Accum1 <= 12) then

valueA = 3400

elseif (Accum1 >12 and Accum1 <= 15) then

valueA = 4100

endif

endif



Or just use one "if then esleif" statement"



var valueA = null



if ( (Accum1 < 1) and (SiteCode1 == "W400") ) then

valueA = 0

elseif ( (Accum1 >1 and Accum1 <= 3) and (SiteCode1 == "W400") )then

valueA = 1300

elseif ( (Accum1 >3 and Accum1 <= 6) and (SiteCode1 == "W400") )then

valueA = 2000

elseif ( (Accum1 >6 and Accum1 <= 9) and (SiteCode1 == "W400") )then

valueA = 2700

elseif ( (Accum1 >9 and Accum1 <= 12) and (SiteCode1 == "W400") )then

valueA = 3400

elseif ( (Accum1 >12 and Accum1 <= 15) and (SiteCode1 == "W400") )then

valueA = 4100

endif



A more advance approach would be to use the 'switch(){" statement.



var valueA = null



switch (true) {



case ( (Accum1 < 1) and (SiteCode1 == "W400") ) :

valueA = 0

berak

case ( (Accum1 >1 and Accum1 <= 3) and (SiteCode1 == "W400") :

valueA = 1300

break



case ( (Accum1 >3 and Accum1 <= 6) and (SiteCode1 == "W400") ) :

valueA = 2000

break



case ( (Accum1 >6 and Accum1 <= 9) and (SiteCode1 == "W400") ) :

valueA = 2700

break



case ( (Accum1 >9 and Accum1 <= 12) and (SiteCode1 == "W400") ) :

valueA = 3400

break



case ( (Accum1 >12 and Accum1 <= 15) and (SiteCode1 == "W400") ) :

valueA = 4100

break



default:

// code for out of test case code

break;

}

Avatar

Level 7
Did you really want a result value of 4100 when the input was 1?



>if (Accum1 < 1) then

>valueA = 0

>elseif (Accum1 >1 and Accum1 <= 3) then

>valueA = 1300

>elseif (Accum1 >3 and Accum1 <= 6) then

>valueA = 2000

>elseif (Accum1 >6 and Accum1 <= 9) then

>valueA = 2700

>elseif (Accum1 >9 and Accum1 <= 12) then

>valueA = 3400

>elseif (Accum1 >12 and Accum1 <= 15) then

>valueA = 4100



In fact, why are conditions repeated on the elseif? Again, looks like

a technique which makes coding errors more likely.





Aandi Inston

Avatar

Level 5
Hi Nick, nothing to worry about I can fix it for you but would like to understand what your objectives are and when this calculation code should get active (context)? Also do not forget to include your email address so that I can email you the attachment with updated code.