Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Creating and saving a variable
 
Creating and saving a variable
This mrScriptBasic example shows how to create a numeric variable named Spend in the MDM, and to save the metadata in a file called My Numeric Variable.mdd.
Dim MyDocument
Dim MyVariable

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

' Create a new MDM Variable object, tag it as a numeric
' variable with the name "Spend" and with a range of 0-1000
Set MyVariable = MyDocument.CreateVariable("Spend", _
"How much do you spend on our products per year?")

' Specify the data type of this variable (numeric)
MyVariable.DataType = mr.Long

'specify the range as 0 - 1000
MyVariable.MinValue = 0
MyVariable.MaxValue = 1000

' Add the newly created numeric variable to the MDM Document
MyDocument.Fields.Add(MyVariable)

' Save the MDM Document
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\My Numeric Variable.mdd")

' Release the Document
MyDocument.Close()
See Using Visual Basic to create and save a variable for a VB.NET example of this script and information about using the MDM in Visual Basic.
To use mrScriptBasic to interface to the MDM, you need to understand how to create and reference MDM objects.
Line by line analysis
Dim MyDocument
Declares the mrScriptBasic variable MyDocument. In mrScriptBasic, all variables are defined as type Variant, which is a special data type that can contain different kinds of information depending on how it is used. Variables can contain information such as numbers, text, and dates, but in this case, we will be using the variable to refer to an object.
Dim MyVariable
Declares another variable that we will also use to refer to an object. Note that we could have combined the first two lines into a single line:
Dim MyDocument, MyVariable
Set MyDocument = CreateObject("MDM.Document")
Uses CreateObject in the UNICOM Intelligence Function Library to create a new MDM Document object. In mrScriptBasic, you use the Set statement to assign object references to variables. The Document object is the main object of the MDM. The MDM has many other objects, but they must all be attached to a Document.
Set MyVariable = MyDocument.CreateVariable("Spend","How much do you spend on our products per year?")
Assigns the MDM Variable object to the mrScriptBasic variable MyVariable. It does this by using an MDM Document object method (CreateVariable), to create a new MDM Variable object. The parameters that you pass to CreateVariable specify the MDM variable name (Spend), and the MDM variable label (How much do you spend on our products per year?). Note that this label is an optional parameter. (See Displaying a numeric variable in UNICOM Intelligence Interviewer - Paper for more about MDM variable labels.)
MyVariable.DataType = mr.Long
Uses the mr.Long constant that is built into mrScriptBasic to set the DataType property of the MDM Variable object to indicate that this is a numeric (integer) variable. Constants associate values with names, which are generally easier to remember than numeric values. There is a set of constants built into mrScriptBasic that represent the various different possible data types. In UNICOM Intelligence Professional, you can see all the data type constants built into mrScriptBasic by pressing Alt+1 twice to display the Types pane and selecting built-in from the list in the top left corner of the pane.
MyVariable.MinValue = 0 and MyVariable.MaxValue = 1000
Set the numeric range of the variable.
Attaches the MDM Variable object to the MDM Document object's Fields collection (a collection is an object that contains other objects). If you omit this line, the variable would not be linked to the MDM Document and would not be saved when you save the MDM Document. Attaching the variable to the Fields collection makes it an integral part of the MDM Document.
MyDocument.Save("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\My Numeric Variable.mdd")
Saves the MDM Document to the file named My Numeric Variable.mdd in the UNICOM Intelligence Developer Documentation Library's default output folder. Note that .mdd is the file name extension for UNICOM Intelligence Data Model metadata files. The file is an XML file, which could be edited with any text or XML editor, such as Notepad. In general however, you should only edit .mdd files using the MDM or a UNICOM Intelligence product designed for this purpose, such as UNICOM Intelligence Professional or Metadata Model Explorer.
MyDocument.Close()
Closes the MDM Document and releases it from memory. Use this method to release the Document object if you have opened it in read-write mode.
Opening the file
Open the file My Numeric Variable.mdd in Questionnaire Viewer, which is an accessory that comes with the UNICOM Intelligence Data Model for viewing .mdd files. To do this, either:
Click the file in Windows Explorer
From the Windows Start menu, choose Programs > UNICOM Intelligence > Accessories > Questionnaire Viewer, and then use the Open command on the File menu.
Requirements
See Requirements.
Next
Using Visual Basic to create and save a variable
See also
Working with the Metadata Model: Tutorial