Desktop User Guides > Professional > Interview scripting > Sample management > Sample management facilities > Scripts that refer to non-existent sample fields
 
Scripts that refer to non-existent sample fields
Sample Management scripts often test the data held in fields in the sample records to determine how the record should be processed. For example, you might want to check whether the respondent has a Panel Identification code, and then take different actions depending on whether or not this is the case.
The usual way of making this test would be to copy the value of the PanelId sample variable into a script variable and then to use a pair of If statements to make the test and define the actions to be taken. For example:
On Error Resume Next
Set myField = SampleFields("PanelId")
LogMsg = LogStart & "Err.Number= & CStr(Err.Number)
Log.LogThisEx 21, LogMsg, LOGLEVEL_INFO
If (myField Is Nothing) Then
  LogMsg = LogStart & "No Panel Id"
  Log.LogThisEx 21, LogMsg, LOGLEVEL_INFO
End If
If Not (myField Is Nothing) Then
  LogMsg = LogStart & "Record has a Panel Id"
  Log.LogThisEx 21, LogMsg, LOGLEVEL_INFO
  LogMsg = LogStart & "PanelId=" & myField.Value
End If
However, the Is operator cannot be used with Empty and Nothing, so if a sample record has no PanelId field at all, both tests will fail and you will see the log messages for both tests. This is because when PanelId does not exist, myField is not initialized so in both cases the next statement is executed due to the On Error Resume Next.
To avoid this problem, test for an empty object instead. For example:
If IsEmpty(myField) Then
  LogMsg = LogStart & "No Panel Id"
  Log.LogThisEx 21, LogMsg, LOGLEVEL_INFO
End If
If Not (IsEmpty(myField)) Then
  LogMsg = LogStart & "Record has a Panel Id"
  Log.LogThisEx 21, LogMsg, LOGLEVEL_INFO
  LogMsg = LogStart & "PanelId=" & myField.Value
End If
See also
Sample management facilities