Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Traversing the fields and variables collections
 
Traversing the fields and variables collections
The following mrScriptBasic example lists all the objects in the Fields collection of the short_drinks.mdd file that comes with the UNICOM Intelligence Developer Documentation Library:
Dim MyDocument, MyObject

Set MyDocument = CreateObject("MDM.Document")

MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyObject In MyDocument.Fields
Debug.Log(MyObject.Name + " : " + MyObject.Label)
Next
A similar example in VB.NET is:
Private Sub Read_Short_Drinks()
Dim MyDocument As MDMLib.Document
Dim MyObject As Object

MyDocument = New MDMLib.Document

MyDocument.Open("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyObject In MyDocument.Fields
Debug.Write(vbCrLf & MyObject.Name & " : " & MyObject.Label)
Next
End Sub
The first three objects listed are UNICOM Intelligence System Variables and are named Respondent, DataCollection, and DataCleaning. System variables, which always appear at the start of the Fields collection, are used for storing information such as respondent serial numbers, the version of the metadata used to collect the data, and so on. However, As the text in their labels suggests, these objects are not MDM Variable objects at all but instead are MDM Class objects, which are a type of object that groups variable objects. Those variable objects are contained in the Class object's own Fields collection. For more information about the types of objects that can be contained in the Document.Fields collection, see MDM Document structure.
Extending the example
The following mrScriptBasic example extends the first example. As before, the example opens the short_drinks.mdd file and loops through the Document.Fields collection, displaying the names and labels of the objects in the collection. However, it also uses the MDM ObjectTypeValue property to test whether these objects are MDM Class objects. If they are, the example loops through the MDM Variable objects in the Class.Fields collection, again displaying the names and labels.
Dim MyDocument, MyVariable, MyObject

Set MyDocument = CreateObject("MDM.Document")

MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyObject In MyDocument.Fields
Debug.Log(MyObject.Name + " : " + MyObject.Label)

If MyObject.ObjectTypeValue = MDMLib.ObjectTypesConstants.mtClass Then
For Each MyVariable In MyObject.Fields
Debug.Log(" " + MyVariable.Name + " : " + MyVariable.Label)
Next
End If
Next
A similar example in VB.NET is:
Private Sub Read_Short_Drinks2()
Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2
Dim MyObject As Object

MyDocument = New MDMLib.Document

MyDocument.Open(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyObject In MyDocument.Fields
Debug.Write(vbCrLf & MyObject.Name & " : " & MyObject.Label)

If MyObject.ObjectTypeValue = MDMLib.ObjectTypesConstants.mtClass Then
For Each MyVariable In MyObject.Fields
Debug.Write(vbCrLf & " " & MyVariable.Name & " : " _
& MyVariable.Label)
Next
End If
Next
End Sub
You can use the MDM ObjectTypeValue property to test for other MDM object types that also have Fields collections, such as Compound, Array, and Grid.
The following example opens the same file and loops through the Document.Variables collection, which is a collection of the VariableInstance objects in the Document.
Dim MyDocument, MyVariableInstance

Set MyDocument = CreateObject("MDM.Document")

MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyVariableInstance In MyDocument.Variables
Debug.Log(MyVariableInstance.FullName + " : " + MyVariableInstance.Label)
Next
A similar example in VB.NET is:
Private Sub Read_Short_Drinks3()
Dim MyDocument As MDMLib.Document
Dim MyVariableInstance As MDMLib.VariableInstance

MyDocument = New MDMLib.Document

MyDocument.Open(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)

For Each MyVariableInstance In MyDocument.Variables
Debug.Write(vbCrLf & MyVariableInstance.FullName & _
" : " & MyVariableInstance.Label)
Next
End Sub
If you compare the output from this second example with the output from the first example in this topic, you will see that there are many more VariableInstance objects than there are objects in the Document.Fields collection. This is because there is one VariableInstance object for each column that is required to store the case data in the top-level virtual table. For example, for the childhh categorical grid question, there are three VariableInstance objects, one for each subquestion in the grid. And for the numdrnks numeric grid question, there is one VariableInstance object for each cell in the grid.
You can tell which top-level object a VariableInstance object relates to, because the FullName of a VariableInstance object combines its name with the name(s) of its parent object(s). For example, the FullName of a system variable's VariableInstance object combines the name of the system variable with the name of the parent Class object.
Requirements
See Requirements.
Next
Getting the VariableInstance objects for a field
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable