Using range from previous question to auto select option in the next question

Hello
Please I need help on how to use a range to auto-select a response in another question. Details below
Q1. How old are you? The constraint is .>=14 and .<=24 meaning age should be from 14 to 24

Q2. Is this a child or young adult? Hint child = 14-17years and young adult=18-24years.
option1: Child
option2: Young adult

Now, I want if the age in question 1 is from 14-17, then option 1 should be automatically selected but if the age in question 1 is from 18-24 then option2 should be automatically selected.
I am using xls form.
Thanks

Hello,
You can use a calculation for the select variable, based on the age value.
If( ${Age} >= 18 and ${Age} <= 24, β€˜2’, if(${Age} … , β€˜1’, β€œβ€) )
Adapt β€˜2’ and β€˜1’ to your choice names.
Even the ands might not be needed, as an if usually evaluates from left to right, stopping with the first true)…

You may also set your second variable to readonly.
If you don’t need to show it to the user,
you might use type calculate or hidden. Or the new feature disabled.

1 Like

Hello Wroos
Thanks for helping but when I use the provided command under calculation or constraint, I get an error when I try to validate the form. Can you please check it again.

I understand the read-only option

Thanks

See update. It’s in calculation column/cell to use…
For more knowhow you might take a look here

Hello Wroos
This is what worked for me
type -calculate, name-b3i and under calculation I used β€œif((${Age} < 18), β€˜1’, if((${Age} > 17), β€˜2’,β€˜0’))” where β€œ0” is the default as if statements require three arguments.

Then for question 2, which is select_one, the constraint is .=${b3i}, as 1=child (14-17) and 2= young adult (18-24)
Thanks for the help

1 Like

Hi @RayRay,

Glad you were able to solve your issue. However, you could also do the same as outlined in the image as shared below:

In the survey tab of your xlsfrom:

In the choices tab of your xlsform:

Image 2

Data entry screen as seen in Enketo: when a wrong entry is made

Data entry screen as seen in Enketo: when a wrong entry is made

Data entry screen as seen in Enketo: when a correct entry is made

Data entry screen as seen in Enketo: when a correct entry is made

Reference xlsform:

Auto Select.xls (29.5 KB)

1 Like

Hello,
Fine! Avoids additional calculate variable.
I would just add read_only = true for the select.

Question:
Even the ands might not be needed, as an if usually evaluates from left to right, stopping with the first true)… @Kal_Lam: Can you confirm this?
But I like it with the ands (safe and explicit programming). :relaxed:

1 Like

Hi @wroos,

Could you share a dummy so that we could have a closer look.

Thanks for this. I have tried it and it works as well

1 Like

Hi @wroos

That should be true

Stephane

2 Likes

Hello @Kal_Lam,
It is work well with Enketo but seem not work in KoBo collect App. How can I deal it to work in App?

Did you test your form with the Online validator, please?
Could you share the related part of your form, please, and a screenshot of the Collect problem?

CBCC_F1_1st Emotional Test Form.xlsx (25.8 KB)

Dear @wroos,

Here are the problem in Excel the red, survey line 34. I have tested in Online validator but it work only in Enketo but not auto select in Mobile App.

Values of selects are generally stored as string. You should try β€˜2’ … for the auto select values in the if expression. It might also be better to use explicit casting for the mathematical comparison. So, I would suggest:
if( number(${A1TScore}) >= 14, β€˜5’, … , β€˜2’))))

Please, check your form with the Online validator: β€œLanguage β€˜English (en)’ is missing the survey hint column.” This will result in missing hints for EN.

1 Like