Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Loading and reading a variable
 
Loading and reading a variable
This mrScriptBasic script loads and reads the MDM variable created in Creating and saving a variable:
Dim MyDocument, MyVariable

Set MyDocument = CreateObject("MDM.Document")

' Open the file in read-only mode
MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\My Numeric Variable.mdd", , _
MDMLib.openConstants.oREAD)

Set MyVariable = MyDocument.Fields[0]

Debug.Log(MyVariable.Name)
Debug.Log(MyVariable.Label)
A similar example in VB.NET is:
Private Sub Loading_and_Reading_Variable()

Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2

MyDocument = New MDMLib.Document

' Open the file in read-only mode
MyDocument.Open("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\My Numeric Variable.mdd", , _
MDMLib.openConstants.oREAD)

MyVariable = MyDocument.Fields(0)

Debug.Write(vbCrLf & MyVariable.Name)
Debug.Write(vbCrLf & MyVariable.Label)
End Sub
Line analysis
MyDocument.Open
Loads into the MDM the .mdd file created previously. The MyDocument.Open method has three parameters, the second and third of which are optional. The second parameter specifies the version you want to open and has been omitted in this example, because the .mdd file does not contain any versions. The third parameter specifies whether you want to open the file for read only, for both read and write, or in no-save mode. If a file is open for both read and write, the Document is locked and other applications cannot access it. When you open the Document in read-write mode, you should call the Document.Close method to release the Document when you have finished with it, as shown in Creating and saving a variable. Calling Document.Close is not necessary in this example, because it opens the Document in read-only mode.
Set MyVariable = MyDocument.Fields[0]
Assigns the mrScriptBasic variable MyVariable to the first object in the MDM Document Fields collection object. Collection objects act as containers for other objects. The Fields collection object is an array of objects; the first of which has an index of zero, which in this example is the Spend variable.
An alternative way of accessing the Spend variable would be to use its name; for example, Set MyVariable = MyDocument.Fields["Spend"]. However, this technique means that you must know the object's name.
When you do not have this information, you can iterate through the objects in the collection. One way is to use the Count property of the collection to iterate through all of the objects:
Dim i

For i = 0 To MyDocument.Fields.Count - 1
Set MyVariable = MyDocument.Fields[i]
Debug.Log(MyVariable.Name + " : " + MyVariable.Label)
Next
The other way is to use the For Each statement to iterate through the objects:
For Each MyVariable In MyDocument.Fields
Debug.Log(MyVariable.Name + " : " + MyVariable.Label)
Next
A similar example in VB.NET is:
Dim i As Integer

For i = 0 To MyDocument.Fields.Count - 1
MyVariable = MyDocument.Fields(i)
Debug.Write(vbCrLf & MyVariable.Name & " : " & MyVariable.Label)
Next i

For Each MyVariable In MyDocument.Fields
Debug.Write(vbCrLf & MyVariable.Name & " : " & MyVariable.Label)
Next
The For Each statement is a powerful feature for accessing and manipulating objects and collections, and you need to understand it to take full advantage of the MDM. Note, however, that some collection objects in the MDM do not support the For Each statement. For those objects, such as the Goto, LanguageDefinitions, and Properties collections, you must use the object's Count property to iterate through the collection.
Using the Debug.Log method
The Debug.Log method is used in these examples to display the value of MyVariable's Name and Label properties in the UNICOM Intelligence Professional Output pane. To make this pane visible in UNICOM Intelligence Professional, press Alt+6 twice. If you are using the mrScript Command Line Runner, the output from Debug.Log is displayed in the console. These examples simply display the values, whereas in practice you would usually do something more interesting with this information. The values that are displayed are MyVariable's Name and Label properties, in this case Spend and How much do you spend on our products per year?.
You can use the same technique to access other properties of MyVariable, such as MinValue, MaxValue, and DataType. To see these other properties in the UNICOM Intelligence Professional Types pane, select MDMLib from the list in the top left corner of the pane and then select IVariable2 from the list of types. The properties are displayed on the right side of the pane.
Requirements
See Requirements.
Next
Traversing the fields and variables collections
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable