If-statement to automatically choose answer based on a previous question

Hi there,

I am trying to build the following with one_select type questions:

Q1: Name of the enumerator

  1. Paul
  2. Lisa
  3. Marie
  4. Alain

Q2: Selection of district

  1. Mahajanga
  2. Marovoay

As Paul and Lisa only work in Mahajanga, I would like to automatically show in Q2 that as selected answer. Whereas Mary and Peter work in the other district.

I put the following if-statement the calculation column, and it works in Enketo but I receive an error message when used in KoboCollect
if((${Q1}=‘Paul’ or ${Q1}=‘Lisa’),‘Mahajanga’,if((${Q1}=‘Marie’ or ${Q1}=‘Alain’),‘Marovoay’,0))

Do you know how to solve this?

Thanks, Frieda

@Frieda, you could do it as oultined in the image shared below:

In the survey tab of your xlsform:

Image 1

In the choices tab of your xlsform:

Image 2

Data entry screen as seen in Enketo: when nothing is selected

Data entry screen as seen in Enketo: when Paul is selected

Data entry screen as seen in Enketo: when Lisa is selected

Data entry screen as seen in Enketo: when Marie is selected

Data entry screen as seen in Enketo: when Alain is selected

Reference xlsform:

Prefill (if-statement).xlsx (11.3 KB)

Hey @Kal_Lam
Thanks for your swift reply! The if-statement works for me in Enketo, but I get an error message in KoboCollect when I try to answer the first question, as seen in the screenshot:
image
After that, the form crashes and I am taken back to the main menu.

Did it work for you on KoboCollect?

Yes, it works fine for me. Check the exact xlsform that I have shared with you.

Hi @Kal_Lam ,
it took me a few days to get back to this.
Indeed, it works now with a similar if-statement like you provided in the example.
Initially I had tried to use another if-statement in a following question but that somehow caused problems in KoboCollect. Once I took out the choice_filter I had initially defined for that question, it worked.
Thanks again!!

1 Like