Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
jduenyas
Specialist
Specialist

Need help with arrays

Hi all

Creating a macro (procedure) in the Edit module I can sucessfuly set an array like so:

ListOfNames = Array("Name 1","Name 2","Name 3")

However, when I try to make that dynamic and set the names into a string variable and then pass the string variable  to the array it does not work.

strOfNames = ("Name 1","Name 2","Name 3")

ListOfNames = Array(strOfNames)

Is there a way to pass the string variable to the array? (What am I doing wrong?)

Thanks

Josh

6 Replies
m_woolf
Master II
Master II

Try this:

strOfNames = """Name 1"",""Name 2"",""Name 3"""

jduenyas
Specialist
Specialist
Author

Thanks for your reply but the expression

strOfNames = """Name 1"",""Name 2"",""Name 3"""

is wrong and cannot be resolved to a string.

m_woolf
Master II
Master II

It works for me in QV 10 SR6

jduenyas
Specialist
Specialist
Author

What can I say? You learn something new every day!

Setting the variable this way does work. (I am surprised that it does work in the Edit module because in any VB IDE this statement produces an error "End of statement expected")

Never the less, it did not pass to the array.

m_woolf
Master II
Master II

The arguments of the Array() function need to be an array, not a string. Did you see Gysbert's reply? I don't see it in this thread:

What you should probably do instead is get a count of the items you want to add to the array, size the array accordingly and then use a loop to add the items. If you can't get a count of the items and are using something like a while or a for each loop then you can redim the array on each iteration of the loop and add the item. See http://windowsitpro.com/article/vbscript/understanding-vbscript-arrays-5627 for more information on arrays in vbscript.

jduenyas
Specialist
Specialist
Author

Thank you mwoolf.
Interesting that I could not see Gysbert's reply here either. I did receive it though in my email.
I am quite savvy programming in VB but it seems that the VB editor in QV does not function all together like a full fledged VB IDE.
I have a subroutine/function which upon receiving a name of a field and an array of names will deconstruct the array to its elements and put the elements into a list box of the FieldName and thus create a multi selection. Here is the subroutine:
Public function SelectInListBox(byval strFieldName, byval strArrayListOfNames)
     dim i,j
      i = lbound(strArrayListOfNames)
     j = ubound(strArrayListOfNames)
      set f = ActiveDocument.Fields(strFieldName)
      set fv = f.GetSelectedValues
       if fv.count > 0 then
              fv.RemoveAt 0
              f.SelectValues fv
         end if
      set fv = f.GetNoValues    'empty the array
     for i = 0 to j
              fv.Add
              fv(i).Text = strArrayListOfNames(i)
              fv(i).IsNumeric = false
     next
     f.SelectValues fv     ' pass the selected values to the field (ListBox)
End Function
To pass the array to the function I build the array in a subroutine as such:
Sub SelectNames()
           '  This  works fine
              ArrayListOfNames = Array("Name 1" ,"Name 2","Name 3")      ' "Name 1", "Name 2"... are elements in the FieldName
        
            ' Call the function to select in the listbox
          SelectInListBox "FieldName", ArrayListOfNames    ' FieldName is the name of the field in a list box

End Sub
This works just fine. But when trying to build the array dinamically (as follows) does not work:
Sub selectNames()
     Dim i
     Dim strElements
     i = 0
     For each Name in SelectedNames
          ' Option one that did not work
          strElements = strElements & "," & Name
          'Option 2 that did not work
           strElements = strElements & ", " & chr(34) & Name & chr(34)
          ' Option 3 that did not work
          strElements = strElements & "," & """ & Name & """
          i = i + 1
     next
     ' clean the starting  ","   (Comma separataor)
     strElements = right(strElements, len(strElements)-1)
     ' strElements now shold contain "Name1","Name2","Name3"  (etc)     
     Dim ArrayListOfNames( i )  ' dimention the array to the number of elements less 1 (0 based)
    ArrayListOfNames = Array(strElements)    ' Pass the elements to the array
    SelectInListBox "FiledName", ArrayListOfNames       'call the function
End sub
Do you see anything wrong with that?
Thanks