Solved

Items From ListBox to Array

Posted on 2006-07-13
14
164 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 142

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 142

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 142

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now