Desktop User Guides > Professional > Data management scripting > Data Management Script (DMS) file > Sections in the DMS file > Event section > OnNextCase Event section
 
OnNextCase Event section
The OnNextCase Event section defines procedural code that is to be applied to each case. For example, code to clean the case data or set up case data for persisted derived variables.
An error occurs if you attempt to assign a value of the wrong type to a question, for example, attempting to assign a string value to a numeric question.
The OnNextCase Event section is not available when you have not specified an input metadata source. If you attempt to include an OnNextCase Event section in a case data-only transformation or when using a non-Data Model OLE DB provider, you will typically get an “Object reference not set to an instance of an object” error. For information about accessing objects in this section, see Using objects in the Event section.
Unbounded loops are available in the OnNextCase event with an HDATA view (available to read/write the response value and add/remove iterations).
Inserting new records into the input dataset, as part of OnNextCase, can lead to unpredictable results.
Examples
Data cleaning example
This example tests the response to the visits numeric question. This question asks how many times the respondent has visited the museum previously and is only asked if he or she selected Yes in response to the before question, which asks whether he or she has visited the museum before. If the visits question holds a NULL value or is zero, the response to the before question is automatically set to No, and otherwise the response to the before question is set to Yes. A string is set up to hold the respondent's serial number and the response to the before question after cleaning and this string is then written to the log file. The DMS file would need to have a Logging section (see Logging section.
Event(OnNextCase, "Clean the data")
Dim strDetails
strDetails = CText(Respondent.Serial)

If visits is not Null then
If visits = 0 Then
before = {No}
strDetails = strDetails + " Before = No"
Else
before = {Yes}
strDetails = strDetails + " Before = Yes"
End If
Else
before = {No}
strDetails = strDetails + " Before = No"
End If

dmgrJob.Log.LogScript_2(strDetails)
End Event
For more data cleaning examples, see Data cleaning examples. For examples of setting up data for new variables in the OnNextCase Event section, see Creating new variables.
Unbounded loop question example
This example uses the Household data set to read, modify, add iterations, and delete iterations in the person loop.
Event(OnNextCase, "Manipulate unbounded question")
Dim Quest, Level1Quest, Level2Quest, Level3Quest
'Numberic Loop for Person, and Unbound loop for Trip

'read and modify
for each Quest in person
      For each Level2Quest in Quest.trip
        For each Level3Quest in Level2Quest
          'set country value with 'canada'
          if Level3Quest.QuestionName = "country" then
            Level3Quest = "canada"
          end if
        Next
      Next
Next

'add new record
if person[2].age = null then
    'add person's iteration 2
    person[2].age = 35
    person[2].numtrips = 0
    person[2].name = "new added person"
    'add trip's iteration one in person iteration 2
    person[2].trip[1].country = "canada"
    person[2].trip[1].daysaway = 1
    person[2].trip[1].trip = 1  
end if

'delete existing record
for each Quest in person
      For each Level1Quest in Quest.trip
        if Level1Quest.QuestionName = 2 then
          'remove all value in current record will remove current record
          For each Level2Quest in Level1Quest
            Level2Quest = null
          Next
        end if
      Next
Next
End Event
See
Event section