Changing the question order
The Routing object is an ordered set of references to questions in the Fields collection. To change the routing from, for example, {Q1, Q2, Q3} to {Q1, Q3, Q2}, do this:
1 Remove Q3 from the Routing collection. This does not remove the question from the questionnaire because the Routing collection is merely a reference to the question in the Fields collection.
2 Insert Q3 into the Routing object before Q2.
The following mrScriptBasic example creates three questions called Q1, Q2, and Q3, and adds them to the routing collection.
Dim MyDocument, MyVariable, MyRoutingItem
' Create a new MDM Document object
Set MyDocument = CreateObject("MDM.Document")
MyDocument.Contexts.Base = "Question"
MyDocument.Contexts.Current = "Question"
MyDocument.Contexts.Add("Paper")
' Create three variables
Set MyVariable = MyDocument.CreateVariable("Q1")
MyVariable.DataType = mr.Long
MyDocument.Fields.Add(MyVariable)
Set MyVariable = MyDocument.CreateVariable("Q2")
MyVariable.DataType = mr.Text
MyDocument.Fields.Add(MyVariable)
Set MyVariable = MyDocument.CreateVariable("Q3")
MyVariable.DataType = mr.Double
MyDocument.Fields.Add(MyVariable)
' Add Q1, Q2, and Q3 into the Routing collection
Set MyRoutingItem = CreateObject("MDM.RoutingItem")
Set MyRoutingItem.Item = MyDocument.Fields["Q1"]
MyDocument.Routing.Add(MyRoutingItem, "Paper")
Set MyRoutingItem = CreateObject("MDM.RoutingItem")
Set MyRoutingItem.Item = MyDocument.Fields["Q2"]
MyDocument.Routing.Add(MyRoutingItem, "Paper")
Set MyRoutingItem = CreateObject("MDM.RoutingItem")
Set MyRoutingItem.Item = MyDocument.Fields["Q3"]
MyDocument.Routing.Add(MyRoutingItem, "Paper")
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Question Order.mdd")
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Create_Questions_and_Add_to_Routing()
Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2
Dim MyRoutingItem As MDMLib.RoutingItem
' Create a new MDM document object
MyDocument = New MDMLib.Document
MyDocument.Contexts.Base = "QUESTION"
MyDocument.Contexts.Current = "QUESTION"
' Create three variables
MyVariable = MyDocument.CreateVariable("Q1")
MyVariable.DataType = MDMLib.DataTypeConstants.mtLong
MyDocument.Fields.Add(MyVariable)
MyVariable = MyDocument.CreateVariable("Q2")
MyVariable.DataType = MDMLib.DataTypeConstants.mtText
MyDocument.Fields.Add(MyVariable)
MyVariable = MyDocument.CreateVariable("Q3")
MyVariable.DataType = MDMLib.DataTypeConstants.mtDouble
MyDocument.Fields.Add(MyVariable)
' Add Q1, Q2, and Q3 into the Routing collection
MyRoutingItem = New MDMLib.RoutingItem
MyRoutingItem.Item = MyDocument.Fields("Q1")
MyDocument.Routing.Add(MyRoutingItem, "Paper")
MyRoutingItem = New MDMLib.RoutingItem
MyRoutingItem.Item = MyDocument.Fields("Q2")
MyDocument.Routing.Add(MyRoutingItem, "Paper")
MyRoutingItem = New MDMLib.RoutingItem
MyRoutingItem.Item = MyDocument.Fields("Q3")
MyDocument.Routing.Add(MyRoutingItem, "Paper")
MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Question Order.mdd")
MyDocument.Close()
End Sub
The routing is now defined as {Q1, Q2, Q3}. This mrScriptBasic code changes it to {Q1, Q3, Q2}:
Dim MyDocument, MyRoutingItem
Set MyDocument = CreateObject("MDM.Document")
' Open the file in read/write mode
MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Question Order.mdd", , _
MDMLib.openConstants.oREADWRITE)
' First remove Q3 from {Q1, Q2, Q3}
MyDocument.Routing.Remove(2, "Paper")
' Q3 is at index 2 in the routing collection (0-based index)
' Now insert Q3 into the Routing collection before Q2.
Set MyRoutingItem = CreateObject("MDM.RoutingItem")
Set MyRoutingItem.Item = MyDocument.Fields["Q3"]
' An index of 1 (which is the current index of Q2)
' specifies before "Q2"
MyDocument.Routing.Add(MyRoutingItem, "Paper", 1) ' <== Highlighted line
MyDocument.Save()
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Insert_Question_in_Routing()
Dim MyDocument As MDMLib.Document
Dim MyRoutingItem As MDMLib.RoutingItem
MyDocument = New MDMLib.Document
' Open the file in read/write mode
MyDocument.Open(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Question Order.mdd", , _
MDMLib.openConstants.oREADWRITE)
' First remove Q3 from {Q1, Q2, Q3}
MyDocument.Routing.Remove(2, "Paper")
' Q3 is at index 2 in the routing collection (0-based index)
' Now insert Q3 into the Routing collection before Q2.
MyRoutingItem = New MDMLib.RoutingItem
MyRoutingItem.Item = MyDocument.Fields("Q3")
' An index of 1 (which is the current index of Q2)
' specifies before "Q2"
MyDocument.Routing.Add(MyRoutingItem, "Paper", 1)
MyDocument.Save()
MyDocument.Close()
End Sub
This second example opens the .mdd file that was created in the first example. It is opened in read-write mode, which is the default mode of the Document.Open method. To open is for read-write, use:
MyDocument.Open("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Question Order.mdd")
Because the read-write mode prevents the Document from being accessed by other applications, include the Document.Close method at the end of the script to release the Document.
The Routing.Add method adds the routing item to the end of the collection by default. However, you can specify the position at which you want to insert the item, by specifying the index of an existing routing item before which you want to insert it (see highlighted line ).
Requirements
Optional: Microsoft Visual Basic .NET
Next topic
See also