Professional > Table scripting > Working with the Variables interface > Organizing questions with the variables interface
 
Organizing questions with the variables interface
Sample Script File: TomVariablesDemo.mrs
This example script includes functions that assist in modifying Table Object Model metadata. See Running the sample table scripts for information on running the example scripts.
For example:
Rename a variable
Set varItem = vars.Item["order[..].column"]
debug.LogIf(varItem.Name<>"Column", "Item Operation Error")

vars.Rename(varItem.MdmField, "NewColumn")
set varItem = vars.Item["order[..].NewColumn"]
debug.LogIf(varItem.Name<>"NewColumn", "Rename then Item Operation Error")
Create and rename a folder
' Create a folder as root
Set folderRoot = vars.AddNewFolder("RootFolder", 0)

' Rename the folder name as MyRootFolder
folderRoot.Rename(folderRoot.MdmField,"MyRootFolder")
debug.LogIf(folderRoot.Name <> "MyRootFolder", "Rename Error!")
folderRoot holds the reference to the folder. You can add other variables or folders to a folder through folderRoot.
Add a child folder and add variables
' Add a child folder to the root folder
Set folderGrid = folderRoot.AddNewFolder("MyGridFolder")
debug.LogIf(folderRoot.Item[0].Name <> "MyGridFolder", "Item[0] is not MyGridFolder")

' Add the question "Age" to the root folder
Set field = TableDoc.DataSet.MdmDocument.Fields["age"]
folderRoot.Add(field)
debug.LogIf(folderRoot.Item[1].Name <> "age", "Item[1] is not age")

' Add the grid question "Order" to the foderGrid folder
Set field = MDMDoc.Fields["Order"]
folderGrid.Add(field)
Add new variables to a specific folder
' Add a new variable to the root folder
vars.AddNewVariable("age2 ""Address "" text;")

' Add a new variable to the MyRootFolder folder
Set varItem = folderRoot.AddNewVariable("!
MyAge "Age of respondent"
categorical [0..1]
{
E1116_years "11-16 years" factor(13.5),
E1720_years "17-20 years" factor(18.5),
E2124_years "21-24 years" factor(22.5),
E2534_years "25-34 years" factor(29.5),
E3544_years "35-44 years" factor(39.5),
E4554_years "45-54 years" factor(49.5),
E5564_years "55-64 years" factor(59.5),
E65_years "65+ years" factor(69.5)
};
!")
Modify and update a variable
Set field = MDMDoc.Fields["Age"]
field.AxisExpression = "{base()}"
folderGrid.Update(field)
Iterate through the variables in a folder
' Test the forEach Interface of the IVariableListItem
for Each varListItem In folderRoot
debug.LogIf(varListItem.Name = "", "for Each error")
next
Retrieve a reference to IVariableListItem in a folder
' Test the Item property of IVariableItemList
Set varItem = folderGrid.Item["Order"]
debug.LogIf(varItem.MdmField.FullName <> "order", "error In VariableListItem.Item")
Create a derived grid variable
' Add a new grid based on categorical variables
Set varItem = vars.AddNewGrid("age,before", "MyDerivedCategoricalGrid", "Age and Before")
TableDoc.Tables.AddNewGrid("GridTable1", "MyDerivedCategoricalGrid")
' Add a new grid based on text variables
Set varItem = vars.AddNewGrid("address,name", "MyDerivedTextGrid", "Address and Name")
Obtain the count
count = folderRoot.Count
Remove a variable from a folder
folderRoot.Remove(1)
debug.LogIf(folderRoot.Count <> count -1, "Remove Error")
See also
Working with the Variables interface