Solved

Items From ListBox to Array

Posted on 2006-07-13
14
172 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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 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…

752 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