Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Help

Status
Not open for further replies.

DanSc0tt

MIS
Sep 17, 2001
30
0
0
DE
Dim varItm As Variant
Hi,

Not had much experience of arrays, but am trying to capture the data selected in a multi-select list in a number of array variables.

However the following piece of code is giving me a 'subscript out of range' error

Any help much appreciated - Dan

--CODE--


Dim i As Integer
Dim strArray() As String
i = 1

For Each varItm In Me.lstMonth.ItemsSelected

MsgBox Me.lstMonth.ItemData(varItm)
strArray(i) = Me.lstMonth.ItemData(varItm)


i = i + 1


Next varItm
 
Have a look at the ReDim instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Probably because you don't dimension it

Put in for instance

[tt]redim strArray(me!lstMonth.ItemsSelected.Count-1)[/tt]

(and usually arrays are zerobased, so start with i=0)

Roy-Vidar
 
Thanks guys - where would I put the redim? (sorry my array knowledge is vvv minimal!!)

Thanks

Dan
 
Dim i As Integer, varItm
Dim strArray() As String
i = 0
With Me.lstMonth
ReDim strArray(.ItemsSelected.Count - 1)
For Each varItm In .ItemsSelected
' MsgBox .ItemData(varItm)
strArray(i) = .ItemData(varItm)
i = i + 1
Next varItm
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
How are ya DanSc0tt . . . . .

Without have to worry about [blue]Redim[/blue] and the additional copies it makes in memory, try this:
Code:
[blue]   Dim strArray, Pack As String
    
   For Each varItm In Me.lstMonth.ItemsSelected
      If Pack <> "" Then
         Pack = Pack & ";" & Me.lstMonth.ItemData(varItm)
      Else
         Pack = Me.lstMonth.ItemData(varItm)
      End If
   Next varItm

   strArray = Split(Pack, ";")
   
   [green]'Note strArray index starts at zero.[/green][/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks guys - both worked as needed and my brain has grown a little largr to!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top