Developer Documentation Library > Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Creating text and categorical variables
 
Creating text and categorical variables
Now that you know how to create, save, and read simple numeric variables you will discover that other variable types are quite straightforward. Here's an mrScriptBasic example of creating a text variable:
Dim MyDocument, 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 a Text variable "Name" with a minimum length
' of 1 and a maximum length of 100
Set MyVariable = MyDocument.CreateVariable("Name", "What is your name?")
MyVariable.DataType = mr.Text
MyVariable.MinValue = 1
MyVariable.MaxValue = 100
MyDocument.Fields.Add(MyVariable)

' Save the Document
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Text Variable.mdd")
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Create_Text_Variable()
Dim MyDocument As MDMLib.Document
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 a Text variable "Name" with a minimum length
' of 1 and a maximum length of 100
MyVariable = MyDocument.CreateVariable("Name", "What is your name?")
MyVariable.DataType = MDMLib.DataTypeConstants.mtText
MyVariable.MinValue = 1
MyVariable.MaxValue = 100
MyDocument.Fields.Add(MyVariable)

' Save the Document
MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Text Variable.mdd")
MyDocument.Close()
End Sub
This essentially repeats the numeric variable example shown in Creating and saving a variable. The only differences are that the DataType is set to the mrScriptBasic built-in constant mr.Text, and the MinValue and MaxValue now indicate the minimum and maximum number of characters that the user is allowed to enter at data collection time. The MDM does not check the MinValue and MaxValue properties, but UNICOM Intelligence products, such as UNICOM Intelligence Interviewer, use these values to ensure that the data the respondent enters is valid.
MinValue and MaxValue play an important role in defining categorical variables. Defining a categorical variable with MinValue set to 1 tells UNICOM Intelligence Interviewer - Server Admin that the user must select at least one response. Setting MaxValue to 1 defines the variable as single response. Setting MaxValue to greater than 1 defines the question as multiple response.
Here is the mrScriptBasic code to create a categorical variable:
Dim MyDocument, MyVariable, MyElement

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

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

' Create the question Gender
Set MyVariable = MyDocument.CreateVariable("Gender")

With MyVariable
.MinValue = 1
.MaxValue = 1
.DataType = mr.Categorical
.Label = "Are you male or female?"
Set MyElement = MyDocument.CreateElement("male", "Male")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
.Elements.Add(MyElement)
Set MyElement = MyDocument.CreateElement("female", "Female")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
.Elements.Add(MyElement)
End With

MyDocument.Fields.Add(MyVariable)

' Save the Document to specified file
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Categorical Variable.mdd")
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Create_Categorical_Variable()
Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2
Dim MyElement As MDMLib.Element

' Create a new Document
MyDocument = New MDMLib.Document

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

' Create the question Gender
MyVariable = MyDocument.CreateVariable("Gender")

With MyVariable
.MinValue = 1
.MaxValue = 1
.DataType = MDMLib.DataTypeConstants.mtCategorical
.Label = "Are you male or female?"
MyElement = MyDocument.CreateElement("male", "Male")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
.Elements.Add(MyElement)
MyElement = MyDocument.CreateElement("female", "Female")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
.Elements.Add(MyElement)
End With

MyDocument.Fields.Add(MyVariable)

' Save the Document to specified file
MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Categorical Variable.mdd")
MyDocument.Close()
End Sub
Requirements
See Requirements.
Next topic
Creating a grid variable
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable