Getting all data from all items in a VB6 listbox into a string variable

Posted on 2007-04-09
Last Modified: 2013-12-25
I'm using VB6. How do I get the data from all items in a listbox, whether they are selected or not? I have multi-select set to Extended and I have columns set at 0. This listbox is not bound to anything. I populate it with code using the AddItem method.

For i = 0 to Me.lstStartup.ListCount
     strStartup = strStartup & Me.lstStartup.ListIndex(i)
Next i

The above code doesn't seem to work.
Question by:HKComputer
LVL 18

Accepted Solution

JR2003 earned 400 total points
ID: 18879262
You need to use the 'List' property instead of the ListIndex one. Also you need to go from zero to count-1

You might also want to separate each value with a new-line or a tab?

For i = 0 to Me.lstStartup.ListCount -1
     strStartup = strStartup & Me.lstStartup.List(i)
Next i

LVL 15

Expert Comment

ID: 18881385
Listboxes in Access don't have a List property - you need to use Itemdata if referring to the bound column, or the Column property if referring to a different column in a multicolumn listbox

For i = 0 to Me.lstStartup.ListCount -1
     strStartup = strStartup & Me.lstStartup.ItemData(i)  'the bound column
Next i

For i = 0 to Me.lstStartup.ListCount -1
     strStartup = strStartup & Me.lstStartup.Column(i, 4)  'the fourth column
Next i

Author Comment

ID: 18881675

I'm actually using the listbox control in VB6. And the first solution posted is what I needed. I had actually tried to use "List" already but I was referencing the wrong listbox on my form, an empty one, and it appeared that my code wasn't working.

But I am noticing that there are some differences between listboxes in VB6 and Access. I'm more accustomed to using MS Access so the listbox in VB6 has me baffled.

Like, I don't understand VB6's implementation of columns. I'm just separating my "columns" with commas, each text value with a given amount of spaces, placing a comma after each value, and then using a fixed width font in my list box. This way I'm creating my own "columns". Also, I can use arrays to parse the data this way.

But I also wish I could have a scroll bar on the bottom (horizontal) so I could scroll over to see all the data.

I welcome any suggestions.

Assisted Solution

nirojexpert earned 100 total points
ID: 18913258
use the flex grid. see the semi-pseudo code.
with grid
.rows=0:.cols=0 'clearing the grid
.rows=2:.cols=4 'defining the grid size
'writing headings
.textmatrix(0,0)="SN": .textmatrix(0,1) = "name" .....
'writing data
do while not rs.eof
 .rows=.rows+1 'adding new row for each record from database
.textmatrix(.rows-1, 0) = SN ...........
end with
LVL 18

Assisted Solution

JR2003 earned 400 total points
ID: 18913441
You should probably use a ListView control.
You can set the style to report and it is a control similar to the one on the right hand side of Windows Explorer.  When it is in report mode it is like the details view in windows explorer.  You can also define columns etc..

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
Cannot enter table caption text in Word protected document 6 126
VBA error replacing data 6 40
passing a value with stream reader AFTER a ";" 3 68
Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

803 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