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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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