Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Items From ListBox to Array

Posted on 2006-07-13
14
Medium Priority
?
183 Views
Last Modified: 2010-04-30
Hi,

I'm trying to populate an array with selected items of the listbox and am getting this error: "Subscript out of range."
What am I doing wrong?

Here is my code:

       Dim Clients() As Integer
       Dim c as integer

        For i = 0 To Me.Client_List.ListCount - 1
            If Me.Client_List.Selected(i) Then
                c = c + 1
                redim preserve Clients(1 to c) as integer
                Clients(i) = Me.Client_List.ItemData(i)
            End If
        Next i
0
Comment
Question by:Yury Merezhkov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17104070
on which line, which part?
0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 17104097
This line: Clients(i) = Me.Client_List.ItemData(i)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17104165
on which part, Clients(i) or Me.Client_List.ItemData(i) ?
put the cursor over the 2 items to tell on which the error occurs (without actually executing the line)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
Ravi Singh earned 800 total points
ID: 17104215
Hi,

Your getting the error because your resizing the array and still using the loop index "i" to store the data in. Also, use the List property to get the selected item, try this:

       Dim Clients() As Integer
       Dim c As Integer

        For i = 0 To Me.Client_List.ListCount - 1
            If Me.Client_List.Selected(i) Then
                c = c + 1
                ReDim Preserve Clients(1 To c) As Integer
                Clients(UBound(Clients)) = Me.Client_List.List(i)
            End If
        Next i
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17104227
good catch:


       Dim Clients() As Integer
       Dim c As Integer

        For i = 0 To Me.Client_List.ListCount - 1
            If Me.Client_List.Selected(i) Then
                c = c + 1
                ReDim Preserve Clients(1 To c) As Integer
                Clients(c) = Me.Client_List.List(i)
            End If
        Next i
0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 17104258
"Method or data member not found" that's what i get if i use List instead of ItemData. However, it's working with ItemData now. And how do i display items from array in different listbox?

    For k = 0 To UBound(Clients())
        Me.List50.AddItem (k)
    Next k
 
???
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17104272
For k = 0 To UBound(Clients)
        Me.List50.AddItem (Clients(k))
Next k
0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 17104290
Dang, again that error. Here -> Me.List50.AddItem(Clients(k))
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17104310
sorry, forgot Clients' index starts from 1... just use the LBound function... much safer!

For k = LBound(Clients) To UBound(Clients)
      Me.List1.AddItem (Clients(k))
Next k
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17104321
List1 should be List50...

For k = LBound(Clients) To UBound(Clients)
        Me.List50.AddItem (Clients(k))
Next k
0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 17104334
Nope, same error.

I was trying to display Ubound in message box like this

MsgBox (UBound(Clients))

And the error occurs on this line.

I think it has smth to do with ubound.
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17104394
UBound effectively returns the length of the array, are you wanting to get last item from the array? try:

MsgBox(Clients(UBound(Clients)))

0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 17104453
No, I dont wanna get last item. I just wanna take selected items from one listbox, put them in array, and then display array items in another listbox. Error is still there. I marked error line with an arrow.

    If Me.rmbSettings.Value = 1 Then
        For i = 0 To Me.Client_List.ListCount - 1
            If Me.Client_List.Selected(i) Then
                c = c + 1
                ReDim Preserve Clients(1 To c) As Integer
                Clients(c) = Me.Client_List.ItemData(i)
            End If
        Next i
    End If
   
-> For k = LBound(Clients) To UBound(Clients)
        Me.List50.AddItem (Clients(k))
    Next k
   
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17104465

    If Me.rmbSettings.Value = 1 Then
        For i = 0 To Me.Client_List.ListCount - 1
            If Me.Client_List.Selected(i) Then
                c = c + 1
                ReDim Preserve Clients(1 To c) As Integer
                Clients(c) = Me.Client_List.ItemData(i)
            End If
        Next i
    End If
   
   'if there are no items selected, the array Clients is not initialized...
   ' use this instead
 
   For k = 1 To c
        Me.List50.AddItem (Clients(k))
    Next k
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question