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