Developer Documentation Library > Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Creating a grid variable
 
Creating a grid variable
Creating a grid variable is a little more complex than creating a numeric or categorical variable. Suppose you want to create a grid that looks like this:
Grid variable
A grid defines a question, or set of questions, that will be asked more than once. In this example, the question asks the respondent whether they like or dislike something. The number of times that question (or set of questions) is to be asked is controlled by the categories in a category list. In this example, the controlling categories are product names.
Here is the mrScriptBasic code to create this grid variable:
Dim MyDocument, MyGrid, MyElement, MyVariable

' Instantiate a new MDM Document
Set MyDocument = CreateObject("MDM.Document")

' Set the user context to 'Question'
MyDocument.Contexts.Base = "QUESTION"
MyDocument.Contexts.Current = "QUESTION"

' Create the grid object including the question text
Set MyGrid = MyDocument.CreateGrid("MyGrid", _
"For the following products please indicate your opinion")

' Create the first controlling category
Set MyElement = MyDocument.CreateElement("A", "Product A")
' Add the category to the Elements collection
MyGrid.Elements.Add(MyElement)

' Create the next controlling category
Set MyElement = MyDocument.CreateElement("B", "Product B")
' Add the category to the Elements collection
MyGrid.Elements.Add(MyElement)

' Now add a question to the grid
' Add the variable named 'Pref' with the label "Preference"
Set MyVariable = MyDocument.CreateVariable("Pref", "Preference")
MyGrid.Fields.Add(MyVariable)

' Make the variable categorical
MyVariable.DataType = mr.Categorical
' Only one response is allowed
MyVariable.MaxValue = 1
' And it must be answered
MyVariable.MinValue = 1

' Create some categories for the categorical variable
MyVariable.Elements.Add(MyDocument.CreateElement("Like", "Like"))
MyVariable.Elements.Add(MyDocument.CreateElement("Dislike", "Dislike"))

' Finally, add the Grid to the Document's Fields collection
MyDocument.Fields.Add(MyGrid)

' Save the Document
MyDocument.Save("C:\Program
A similar example in VB.NET is:
Private Sub Create_A_Grid()
Dim MyGrid As MDMLib.Grid
Dim MyDocument As New MDMLib.Document
Dim MyElement As MDMLib.Element
Dim MyVariable As MDMLib.IVariable2

' Instantiate a new MDM document
MyDocument = New MDMLib.Document

' Set the user context to 'Question'
MyDocument.Contexts.Base = "QUESTION"
MyDocument.Contexts.Current = "QUESTION"

' Create the grid object including the question text
MyGrid = MyDocument.CreateGrid("MyGrid", _
"For the following products please indicate your opinion")

' Create the first controlling category
MyElement = MyDocument.CreateElement("A", "Product A")
' Add the category to the Elements collection
MyGrid.Elements.Add(MyElement)

' Create the next controlling category
MyElement = MyDocument.CreateElement("B", "Product B")
' Add the category to the Elements collection
MyGrid.Elements.Add(MyElement)

' Now add a question to the grid
' Add the variable named 'Pref' with the label "Preference"
MyVariable = MyDocument.CreateVariable("Pref", "Preference")
MyGrid.Fields.Add(MyVariable)

' Make the variable categorical
MyVariable.DataType = MDMLib.DataTypeConstants.mtCategorical
' Only one response is allowed
MyVariable.MaxValue = 1
' And it must be answered
MyVariable.MinValue = 1

' Create some categories for the categorical variable
MyVariable.Elements.Add(MyDocument.CreateElement("Like", "Like"))
MyVariable.Elements.Add(MyDocument.CreateElement("Dislike", "Dislike"))

' Finally, add the Grid to the Document's Fields collection
MyDocument.Fields.Add(MyGrid)

' Save the Document
MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Grid Variable.mdd")
MyDocument.Close
Requirements
See Requirements.
Next topic
Creating a categorical question with Other Specify
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable