 Level 1

0% to Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

SOLVED

## Calculate end date based on drop-down list  Level 2

I have a drop-down list with 8 different programs that the company is delivering and 2 date/ time fields called StartDate and EndDate. Depending on what is selected from the drop-down list, the EndDate is calculated accordingly. I have the scripts here but it doesn't seem to work. Can you help me point out where I got wrong please? Many thanks

form1.#subform.StartDate::exit - (FormCalc, client)

var date = Date2Num(StartDate.formattedValue,"DD/MM/YYYY")
var value = Programs.boundItem(xfa.event.newText)
if (value == "1") then
EndDate.formattedValue = Num2Date(date + 548,"DD/MM/YYYY") else

if (value == "2") then
EndDate.formattedValue = Num2Date(date + 457,"DD/MM/YYYY") else

if (value == "3") then
EndDate.formattedValue = Num2Date(date + 548,"DD/MM/YYYY") else

if (value == "4") then
EndDate.formattedValue = Num2Date(date + 457,"DD/MM/YYYY") else

if (value == "5") then
EndDate.formattedValue = Num2Date(date + 548,"DD/MM/YYYY") else

if (value == "6") then
EndDate.formattedValue = Num2Date(date + 457,"DD/MM/YYYY") else

if (value == "7") then
EndDate.formattedValue = Num2Date(date + 639,"DD/MM/YYYY") else

if (value == "8") then
EndDate.formattedValue = Num2Date(date + 548,"DD/MM/YYYY")

endif

1 Accepted Solution  Level 10

The formattedValue is defined by a display pattern. You can't set it via scripting.

And never use reserved names such as "value" or "date" for variables, this can cause unexpected results.

Here's a much shorter FormCalc script you can put into the calculate event of the EndDate field.

``````var nDate = Date2Num(StartDate.formattedValue,"DD/MM/YYYY")
var nSelection = Programs
var nAddDays = Choose(nSelection, 548, 457, 548, 457)

2 Replies  Level 10

The formattedValue is defined by a display pattern. You can't set it via scripting.

And never use reserved names such as "value" or "date" for variables, this can cause unexpected results.

Here's a much shorter FormCalc script you can put into the calculate event of the EndDate field.

``````var nDate = Date2Num(StartDate.formattedValue,"DD/MM/YYYY")
var nSelection = Programs
var nAddDays = Choose(nSelection, 548, 457, 548, 457)   