Expand my Community achievements bar.

SOLVED

Calculate end date based on drop-down list

Avatar

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[0].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

Avatar

Correct answer by
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)
$ = Num2Date(Sum(nDate, nAddDays),"DD/MM/YYYY")

 

View solution in original post

2 Replies

Avatar

Correct answer by
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)
$ = Num2Date(Sum(nDate, nAddDays),"DD/MM/YYYY")

 

Avatar

Level 2
Thanks so much for this. It tried and it worked and it's so much simpler. However, my initial idea was to add months and when I couldn't get it to work, I decided to use days instead. But actually 548 days isn't the same as 18 months. I learned the hard way and I managed to figure out how to add months. The script you sent works like magic though