Desktop User Guides > Professional > Troubleshooting > Data Management troubleshooting > Problems using global variables
 
Problems using global variables
In the GlobalSQLVariables section, is it possible to define a multiple-row, multiple-column record set as a GlobalSQLVariable?
You can define as a GlobalSQLVariable only a simple column or an expression based on one or more columns, such as:
SelectQuery = SELECT 2 * Q1 * Q2 As @MyGlobalSQLVariable FROM VDATA
I have added an array variable to the global variables collection? However, it doesn't work as I expected. Why is this? Here is the code where I set up the global variable:
Event(OnJobStart, Do the set up)
  Dim i, Switch[]
  For i = 0 To 16
    Switch[i] = 1
  Next
  dmgrGlobal.Add("Switch")
  Set dmgrGlobal.Switch = Switch
End Event
Adding an array variable to the GlobalVariables collection is not recommended because mrScriptBasic considers a variable to be a categorical when it contains an array but was not defined as an array. For example:
Dim x, y[], z

y[0] = 1
y[1] = 2
x = y

' At this point y is an array and x is seen as a categorical
' The following is possible
z = y[0] ' OK
z = x[0] ' OK

' The following is not possible: setting a categorical element
X[0] = 1 ' Error
I am using the following subroutine in my cleaning script to write to a text file set up as a global variable. But the script fails with the error “Reference type 'dmgrJob' is not an enum. Only enum types can be referenced”. Is it not possible to access a global variable in a subroutine?
EmptyCheck(age)

Sub EmptyCheck(vname)
  If vname.AnswerCount() < 1 Then
    dmgrJob.GlobalVariables.mytextfile.WriteLine(strDetails + ":
         " + vname + " has no answer")
  End If
End Sub
The variable scope rules in mrScriptBasic differ from Visual Basic. Variables that are available in the main script block are not visible in a Function or Sub procedure. This is to make it easier to share functions and subroutines between scripts. Therefore you need to amend your code as follows:
EmptyCheck(age, strDetails, dmgrJob.GlobalVariables.mytextfile)

Sub EmptyCheck(vname, strDetails, ReportFile)
  If vname.AnswerCount() < 1 Then
    ReportFile.WriteLine(strDetails + ": " + vname + " has no answer")
  End If
End Sub
When I attempt to run the following code, I get an error (“The 'WordApp' object does not support the 'Visible' member”). What am I doing wrong?
Event(OnJobStart, Open up Word)
  Dim WordApp, WordDoc
  Set WordApp = CreateObject("Word.Application")
     dmgrJob.GlobalVariables.Add("WordApplication", WordApp)
End Event

Event(OnJobEnd, Write cases to Word doc for Mail Merge Table and conduct the mail merge)
  dmgrJob.GlobalVariables.WordApp.Visible = True
End Event
The call to the GlobalVariables.Add method is causing the default property on the Word.Application object to be expanded. This means that the following lines are the same:
Job.GlobalVariables.Add("WordApplication", WordApp)
Job.GlobalVariables.Add("WordApplication", WordApp.Name) ' Name is the default property
The solution is to assign the object to the global variable after adding it to the GlobalVariables collection, like this (the changes are highlighted):
Event(OnJobStart, Open up Word)
  Dim WordApp, WordDoc
  Set WordApp = CreateObject("Word.Application")
  dmgrJob.GlobalVariables.Add("WordApplication")
  Set dmgrJob.GlobalVariables.WordApplication = WordApp
End Event
See also
Data Management troubleshooting