Solved

Items From ListBox to Array

Posted on 2006-07-13
14
170 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:RealSnaD
  • 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:RealSnaD
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
Technology Partners: 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 200 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:RealSnaD
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:RealSnaD
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:RealSnaD
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:RealSnaD
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

Technology Partners: 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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

735 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