Grid Total

When we check what is searched for on the blog “Grid Total” has been coming up quite allot recently. So to address that search option this article will show you how to create a numeric grid and write some code to add up the response’s and make sure they reach a specific total.

So in this article we will create a numeric grid that will ask “What percentage do you spend on each?”, so the metadata we need for this is,

Metadata(en-AU, Question, Label)
QLoop "What percentage do you spend on each?" loop
    {
        Food "Food",
        Entertaining "Entertaining",
        Saving "Saving",
        Mortgage_Rent "Mortgage or rent",
        OtherLiving "Other living expenses"
    } fields -
    (
        PercentSpent ""
        long [0 .. 100];

    ) expand;

    ErrorCheck "{#ErrorMessage}"
    info;
End Metadata

we have also added a question in called errorcheck that will display the error saying the total does not add up. Next we need to have some routing that will ask the grid question and check that the total adds up to 100.

Routing(Web)

Dim ErrorMessage,iTotal, oSubQ

LoopCheck:

QLoop.Ask()

iTotal = 0

For each oSubQ in QLoop
 	iTotal = iTotal + oSubQ.PercentSpent.Response.Value
Next

If iTotal <> 100 Then
	ErrorMessage = "These values must add to 100% press Next to continue"
 	ErrorCheck.Ask()
 	GoTo LoopCheck
 End If

End Routing

So let’s explain the routing. First off we declare some variables to hold the text of the error message “ErrorMessage”, the total of the grid “iTotal” and then an object to hold all the sub questions of the loop “oSubQ”. The next line we have is a label called LoopCheck. This is the position in the script that we will use to jump back to if out total does not meet the desired value.

Next we ask the question and then start to calculate the grand total of all the numeric questions. We do this with the use of a for each loop, taking the individual question response value and add it to our iTotal value. Once we have looped all the sub questions we check the value of iTotal and if it is not equal to 100 we then set the error message , display the error question and then jump back to the beginning of the script so that we can ask the question again.

Error Question

If you don’t like the idea of having another question to display the error then you can use the built in error object to display an error so instead of having the ErrorCheck code you would have this,

If iTotal <> 100 Then
      Qloop.Errors.AddNew("Err1","These values must add to 100% press Next to continue")
      GoTo LoopCheck
End If

but this on its own whilst it works , it does not display the error. To get the error message to display we must have an template and tell the player where to add the message so if you create a template file with the following in,


Error Message





and then just before you ask the question assign the template,

QLoop.LayoutTemplate = "ErrorMessage.htm"
QLoop.Ask()

you will end up with a question that when entered incorrectly will display a message that looks like this.

Grid Total with error message

 

And there you have it. Some fairly simple code to validate the user responses and make sure that the desired total is met. So if you did a search on our blog for this i hope this helps , but if not then please leave a comment or drop us an email and we will be more than happy to help if we can.

Leave a Comment

%d bloggers like this: