Solved

Items From ListBox to Array

Posted on 2006-07-13
14
178 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
[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: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
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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses
Course of the Month8 days, 9 hours left to enroll

617 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