Creating array from listbox values

R

René

Hello,

I need to create an array from value in an listbox. So far I have the
following code:

_______
strSQL = "SELECT TblCallSLA.SLA_Number " & _
"FROM TblCallSLA " & _
"WHERE TblCallSLA.Practice='DDS'"

Form_Form1!lstSLA_Numbers.RowSource = strSQL

For lSelItem = 0 To Form_Form1!lstSLA_Numbers.ListCount - 1
Form_Form1!lstSLA_Numbers.Selected(lSelItem) = True
MyArray = MyArray & Form_Form1!lstSLA_Numbers.Value & ", "
Next
MyArray = Array(Left(MyArray, Len(MyArray) - 2))

For Each varArray In MyArray
etc.....
_________

But something goes wrong. 'varArray' holds the entire string instead of
individual values of the array. What am I doing wrong or is there a simpler
way to create an array from listbox values?

Thanks

René
 
D

Douglas J Steele

Dim strValues() As String

ReDim strValues(Form_Form1!lstSLA_Numbers.ListCount - 1)

For lSelItem = 0 To Form_Form1!lstSLA_Numbers.ListCount - 1
Form_Form1!lstSLA_Numbers.Selected(lSelItem) = True
strValues(ISelItem) = Form_Form1!lstSLA_Numbers.Column(1,
lSelItem)
Next lSelItem

The above assumes that you want the value from the 2nd column in the list
box (column numbering starts at 0)

(BTW, you can't use For Each with an array, at least not in VBA)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top