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


Looping through listbox items..

Posted on 1999-10-06
Medium Priority
Last Modified: 2008-02-01
I have a simple unbound single-select listbox with a Value list (not a table or query) and I want to be able to loop through the values and capture them in an array.  Is it possible to loop through the values even when the items are not selected.  Please include code snippet...

Thx in advance...
Question by:kenadelglass
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
LVL 10

Accepted Solution

brewdog earned 400 total points
ID: 2104806
I'm not sure what you want to do with them after you get them, so here's the snippet:

    Dim strArray() As String
    Dim i As Integer
    For i = 0 To lstNames.ListCount - 1
        ReDim Preserve strArray(i)
        strArray(i) = lstNames.ItemData(i)
    Next i

lstNames here is the name of your list box. Hope that helps . . .

LVL 25

Expert Comment

ID: 2104834
Brewdog, seems like it would be loads more efficient to simply dimension based on the list size, before the loop.  Rather than using the Redim Preserve.

   Dim strArray() As String
   Dim i As Integer
   ReDim strArray(lstNames.ListCount)

   For i = 0 To lstNames.ListCount - 1
       strArray(i) = lstNames.ItemData(i)
   Next i

Expert Comment

ID: 2105084
How about this as one step further.

dim i as integer, j as integer

j = lstNames.ListCount - 1
if j >= 0 then
     dim strArray(0 to j) as string
     For i = 0 To j
         strArray(i) = lstNames.ItemData(i)
     Next i
     'No items in the list.
end if
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.


Author Comment

ID: 2105263
Sorry but I am going to go with brewdog as he was the fastest and did solve my problem..

Thanks to all!
LVL 10

Expert Comment

ID: 2106440
Great! Glad that helped.
LVL 44

Expert Comment

ID: 6924170
Hi kenadelglass,

Any update on this question?

in an effort to clean up old open questions

your options are:

1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the
participants why you wish to do this, and allow for Expert response.  This choice will include a refund
to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information
outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with
details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
 --> Post comments for expert of your intention to delete and why
 --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below, include the question


Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:
Click you Member Profile to view your question history and keep them all current with updates as the
collaboration effort continues.

------------>  EXPERTS:    Please leave any comments regarding this question here on closing recommendations
if this item remains inactive another seven (7) days.

LVL 25

Expert Comment

ID: 6925664
points to brewdog

Expert Comment

ID: 6943782
Per recommendation, force-accepted by
CS Moderator

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

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