How to program logic for an occurrence?
-
mpg
- Posts: 1
- Joined: September 7th, 2015, 11:45 am
How to program logic for an occurrence?
How do I program this occurrence (please see attached image) in such a way that for each option the user can enter only the corresponding number? For example, for the first option they can enter only the number 1. And if the entry is any other number, how do I code to have an error message pop up? Leaving an option blank should also be acceptable. I am relatively new to CSPro and am trying to learn how to code. Any help will be greatly appreciated!
You do not have the required permissions to view the files attached to this post.
-
josh
- Posts: 2403
- Joined: May 5th, 2014, 12:49 pm
- Location: Washington DC
Re: How to program logic for an occurrence?
Referring to occurrences in logic is different depending on which proc you are in. If you have a variable with occurrences, you would normally use a subscript to specify which occurrence number you are referring to. Subscripts are specified using parentheses. For example to refer the first occurrence (column 1 in your roster) you would use Q5(1), for the second occurrence Q5(2) etc... However if you are in the proc of the repeating variable itself then CSPro assumes you are referring the current occurrence, i.e. the current column/row, so you can just use Q5 without a subscript. Anywhere outside the proc of Q5 you would need to use a subscript.
So to show an error message if the user enters anything but the occurrence number you could do:
To also allow blanks you would need to allow the user to enter notappl which how blanks are represented in CSPro:
So to show an error message if the user enters anything but the occurrence number you could do:
if Q5 <> currocc() then
errmsg("Put an error message here");
endif;
Here currocc() is a CSPro function that returns the current occurrence number so if you are on column 1 it will return 1, on column 2 it will return 2, etc... errmsg("Put an error message here");
endif;
To also allow blanks you would need to allow the user to enter notappl which how blanks are represented in CSPro:
if Q5 <> currocc() and Q5 <> notappl then
errmsg("Put an error message here");
endif;
Note that if you are using system controlled mode the default behavior is to not allow blanks (even if notappl is in the value set). You can change this behavior with the following logic:
errmsg("Put an error message here");
endif;
set behavior(Q5) canenter(outofrange) on (noconfirm);
However, in CSPro it is more common to use blank/notappl for fields that are skipped or not yet entered and to use a missing code like "9" to indicate a non-response. In your example I would usually use 1 for "yes" and 0 for "no" for every column in the roster. This simplifies the logic so that you don't use curocc() or notappl. You can just create a value set for Q5 with 0 and 1 and then you won't need any logic at all since the system will automatically pop-up a message that the value is out of range.