Using Visual Basic to create and save a variable
This VB.NET 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.
Private Sub Create_Numeric_Variable()
Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2
' Create a new MDM document object
MyDocument = New MDMLib.Document
' Create a new MDM Variable object, tag it as a numeric
' variable with the name "Spend" and with a range of 0-1000
MyVariable = MyDocument.CreateVariable("Spend", _
"How much do you spend on our products per year?")
' Specify the data type of this variable (numeric)
MyVariable.DataType = MDMLib.DataTypeConstants.mtLong
'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(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\My Numeric Variable.mdd")
' Release the Document
MyDocument.Close()
End Sub
To use Visual Basic to interface to the MDM, you need to understand how to access and reference MDM objects.
Line by line analysis
Dim MyDocument As MDMLib.Document
Defines the Visual Basic variable
MyDocument as an object data type. This could be replaced by
Dim MyDocument As Object, but then you would lose the useful ToolTips and pop-ups (IntelliSense) that Visual Basic provides automatically when you declare an object variable in a referenced assembly or COM component. (For an example of an equivalent subroutine without this reference, see
Creating a variable without referencing the MDM component.)
Dim MyVariable As MDMLib.IVariable2
Defines the Visual Basic variable MyVariable as an object of type IVariable2. This object type was introduced into the MDM in UNICOM Intelligence Data Model 2.7 to give the Variable object additional features.
MyDocument = New MDMLib.Document
Creates a new MDM Document object. 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.
MyVariable = MyDocument.CreateVariable("Spend","How much do you spend on our products per year?")
Assigns the MDM Variable object to the Visual Basic 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?). This label is an optional parameter. (See
Displaying a numeric variable in UNICOM Intelligence Interviewer - Paper for more about MDM variable labels.)
MyVariable.DataType = MDMLib.DataTypeConstants.mtLong
Uses the mtLong enumeration constant that is built into the MDM to set the DataType property of the MDM Variable object to indicate that this is a numeric (integer) variable. Enumerations make it easy to work with sets of related constants, because they associate constant values with names, which are generally easier to remember than numeric values. In the MDM, the DataTypeConstants enumeration provides a set of constants that represent the various different possible data types. When referring to an enumeration constant in Visual Basic .NET, you normally need to qualify the constant name with the name of the enumeration (DataTypeConstants in this example) and when relevant, the name of the component (MDMLib in this example). This is called the fully qualified name. To avoid using the fully qualified name, add an Imports statement to the namespace declarations section of your code. Generally this is at the top of the code, before the declaration of the Class. For example:
Imports MDMLib.DataTypeConstants
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
...
#End Region
...
End Class
You can then change the line that sets the data type to:
MyVariable.DataType = mtLong
In Visual Studio, you can use the Object Browser to see a list of all of the constants in an enumeration. For example, to see a list of all of the possible MDM variable data types: From the View menu, choose Object Browser. If you are using Visual Basic .NET, expand the MDMLib folder. If you are using Visual Basic 6, select MDMLib. in the first list. From the list of objects, select DataTypeConstants. This displays a list of the MDM data types on the right side of the Object Browser.
MyVariable.MinValue = 0 and MyVariable.MaxValue = 1000
Set the numeric range of the variable.
MyDocument.Fields.Add(MyVariable)
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 is not 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; it avoids problems caused by potential delays before the MDM Document is released by the .NET garbage collector when using the MDM with .NET components.
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. The easiest way to open the file in Questionnaire Viewer is to double-click it in Windows Explorer. Alternatively, from the Windows Start menu, choose:
Programs > UNICOM Intelligence >
Accessories >
Questionnaire Viewer
And then use the Open command on the File menu to open the file.
Next
See also