Follow @dporton

JustCode : Delete Empty columns of data

This mrs script shows you how you can use mrs files to make and run a dms file that will strip out any columns that do not have any data in.

Step 1: Open the VQ file and make an MDD FILE.

Dim mrDSCs, mrMdsc, oDoc,sVQ

sVQ = "C:\temp\FILE"
    Set mrMdsc = createobject("mrdscreg.components")
    Set mrMDSC = mrMdsc["mrScDsc"]

    Set oDoc = mrMdsc.Metadata.Open(sVQ + ".vq")
        oDoc.Save(sVQ + ".mdd")

Set oDoc = Null

Step 2: Take the MDD file and loop the variable instances

Dim oMDM, oVar
Dim sSQL,oConnection,oRecordset

' Create the MDM object
Set oMDM = CreateObject("MDM.Document")
    oMDM.Open("C:\temp\FILE.mdd", , 1)

    For Each oVar in oMDM.Variables
        'if oVar.IsSystem = False then
            debug.Log("Question Name : " + oVar.FullName)

            ' Start code to check if the field has data in it.
            Set oConnection = CreateObject("ADODB.Connection")
                oConnection.ConnectionString = "Provider=mrOleDB.Provider.2;Data Source=mrScDSC;Location=C:\temp\FILE.VQ;Initial Catalog=C:\temp\FILE.mdd"

            If ( oConnection.State = 1 ) Then
                Set oRecordset = CreateObject("ADODB.Recordset")
                    oRecordset.Open("SELECT count(respondent.serial) FROM VDATA WHERE " + oVar.FullName + " is not null" ,oConnection,3,1)
                    If ( oRecordset.EOF = true and oRecordset.BOF = true ) Then
                        sSQL = sSQL
                        sSQL = sSQL + oVar.FullName + ","
                    End if
                Set oRecordset = Null

            End If

            Set oConnection = Null
    ' end if

sSQL = Left(sSQL,len(sSQL)-1)

Step 3: Create the dms file with the select statement in it.

Dim oFSO, oFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFile = oFSO.CreateTextFile("C:\temp\FILE.dms", True)

oFile.WriteLine("InputDatasource(Input, """")")
oFile.WriteLine("    ConnectionString = ""Provider=mrOleDB.Provider.2;Data Source=mrScDSC;Location=C:\temp\FILE.vq;Initial Catalog=C:\temp\FILE.mdd"" ")
oFile.WriteLine("    SelectQuery = ""SELECT " + sSQL + " FROM VDATA""")
oFile.WriteLine("End InputDatasource")
oFile.WriteLine("OutputDatasource(Output, """")")
oFile.WriteLine("    ConnectionString = ""Provider=mrOleDB.Provider.2;Data Source=mrSavDsc;Location=C:\temp\Final.sav""")
oFile.WriteLine("    MetaDataOutputName = ""C:\temp\Final.mdd""")
oFile.WriteLine("End OutputDatasource")


Set oFSO = Null

Step 4: Run the file

Dim oDMOMJob

Set oDMOMJob = CreateObject("DMOM.Job")

oDMOMJob.Load("C:\temp\FILE.dms", null)


JustCode : Function to return the current connection string

This code shows us how to open up an MDD and read the conenction string properties to get the current connection string settings.

' ****************************************
' Designed by : Smarter Dimensions
' Last Updated : 5th July 2009
' Function to return the current connection string of an MDD
' ****************************************

Dim sConnection, oMDM

Set oMDM = CreateObject("MDM.Document")

oMDM.Open("c:\temp\short_drinks.mdd", , openConstants.oREAD)

sConnection = GetConnectionStringFromMDM(oMDM)



Function GetConnectionStringFromMDM(oDoc)
Dim oDataLinkHelper, sConnectionString

Set oDataLinkHelper = CreateObject("MROLEDB.DataLinkHelper")
sConnectionString = "Provider=mrOleDB.Provider.2;"
sConnectionString = sConnectionString + "Data Source=" + oDoc.DataSources.Current.cDscName + ";"
sConnectionString = sConnectionString + "Location=""" + oDoc.DataSources.Current.DbLocation + """;"
sConnectionString = sConnectionString + "MR Init MDM Document=" + oDataLinkHelper.CreateDocumentObjectString(oDoc) + ";"
sConnectionString = sConnectionString + "MR Init Project=" + oDoc.DataSources.Current.Project + ";"
GetConnectionStringFromMDM = sConnectionString

End Function

Creating a TOM table within an interview

Recently the following question was asked in LinkedIn :

KG: Some years ago I saw an interview script that produced a TOM table and presented the information to the respondent. You might use this to show poll results when a respondent has answered a question. For the life of me I can not find that script. Does anyone have an example of this that I can use?

and it got the following response from

CB : I haven’t found the example, but here’s code to connect to a database and pull in the results. You’d need to adjust the code inside the If but I’m sure you could take that result and display it:

Dim rsquery, sSQL, oConnection, oRecordSet
Set oConnection = CreateObject("Adodb.connection")
Set oRecordset = CreateObject("ADODB.Recordset")

    oConnection.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=MyDatabase;Data Source=MyServer"
    sSQL = "Select * FROM [MyDatabase].[dbo].[MyTable]"

    Set rsQuery=oConnection.Execute(sSQL)
    If rsQuery.BOF=False Then'Found record(s)
        sampBusunit =cText(rsQuery.Fields["Busunit"])
    End if

This is a great reply as it shows one way , not using TOM , but a way that will work. If however you realy need to use TOM then you can do something like this once your table is created. To see how to create the table take a look at this post , but once you have your table object you can use the HTMLRenderer to do the rest for you.

Dim HtmlRenderer,sTomHTML

Set HtmlRenderer = CreateObject("ExportHtml.Renderer")
    HtmlRenderer.DisplayAnnotations = False
    HtmlRenderer.DisplayOption = 0              ' 0 = Table Only
    HtmlRenderer.EmbedCss = true
    HtmlRenderer.UseFormattedLabels = True
    HtmlRenderer.IncludeTitleBlock = False
    HtmlRenderer.IncludeTOC = False
    HtmlRenderer.OutputBodyOnly = True
    sTomHTML = replace(HtmlRenderer.Render(oTableDoc.GetTablesXml(sTable)),mr.newline,"")
    Set HtmlRenderer = Null

I do this sort of stuff all the time and it works well , I normally take the output data and run it through the Highcharts api. to produce charts etc. Oh and don’t forget this will only work in your survey if you have TOM on the server. To get an idea of what I do , take a look at this.

JustCode : Make an MDD file from a VQ file using DSC

This code shows you how to open an surveycraft metatdata file using the Surveycraft dsc and save it as a MDD file.

' ****************************************
' Designed by : Smarter Dimensions
' Last Updated : 7th July 2009
' Code to make an MDD file from a VQ file using the SC dsc
' ****************************************

Dim mrDSCs, mrMdsc, oDoc,sVQ

sVQ = "C:\temp\MUSEUM"

Set mrMdsc = createobject("mrdscreg.components")
Set mrMDSC = mrMdsc["mrScDsc"]

Set oDoc = mrMdsc.Metadata.Open(sVQ + ".vq")

oDoc.Save(sVQ + ".mdd")


Set oDoc = Null

Just Code : Run MRS from an MRS

This shows us how we can run an MRS from within an MRS file.

Dim oScriptEngine, sScript, oProgram, sResult

' Define the test script
sScript = "Dim x, y, result" + mr.CrLf + _
"x = ""Hello """ + mr.CrLf + _
"y = ""World""" + mr.CrLf + _
"result = x + y"

' Create the script engine and parse
Set oScriptEngine = CreateObject("mrScript.ScriptEngine")

Set oProgram = oScriptEngine.Parsers[0].Parse(sScript, oScriptEngine, 0)

' Execute the script
oScriptEngine.Execute(oProgram, 0)

' Get the result variable value
sResult = oProgram.Variables["result"]  
%d bloggers like this: