Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Incorrect?

Status
Not open for further replies.

sgkdnay

Technical User
Aug 4, 2008
21
Im trying to export data from Listview. I don't think I have enough sleep atm LOL... aside that, goal is to read data from ListView (Clist) like:

Row Name Town
1 John Anywhere
2 Ann Somewhere
3 Jon Whereever

and export it to text, I got the export part parameter, just the reading from listview giving me headache for now. Don't think I got the logic of this correctly. This is what I have so far..

For j = 1 To .ListItems.Count
For k = 1 To .ColumnHeaders.Count
sData = sData + .ListItems(j).SubItems(k) & ";"
If k = 6 Then sData = sData + vbNewLine
Next k
Next j

thanks in advance
 
It's been a while since I've used ListView, but it's likely the collections are 0 based, so it may be:
Code:
For j = 0 To (.ListItems.Count - 1)
     For k = 0 To (.ColumnHeaders.Count - 1)
        sData = sData + .ListItems(j).SubItems(k) & ";"
        If k = 5 Then sData = sData + vbNewLine
     Next k
Next j

Joe Schwarz
Custom Software Developer
 
Tried that suggestion, got an error:

Run-time Error '35600'
Index Out of Bound

The Code is so simple, I just can't pinpoint it in VB itself cuz it pulls data from the web. I was testing to "save" data to the file but before I do that, I was testing it to textbox (with multiple lines setting). with this code as an "forced" "imaginary data", it works but only gives me 1 of 4 columns results:

Dim l As Integer
l = 2
For k = 1 To .ColumnHeaders.Count
sData = sData + .ListItems(k).SubItems(l) & ";"
Next k

Using the code you gave me and I incorporated into my code and gives me error, here is the code I used:

Dim i As Integer, j As Integer, k As Integer
Dim sData As String
With CList
'this line works fine (to check how many records I have)
For i = 1 To .ColumnHeaders.Count
sData = sData + .ColumnHeaders.Item(i).Text & ";"
If i = 6 Then sData = sData + vbNewLine
Next i
sData = sData + "Item Count: " & .ListItems.Count

'this line gives me error
For j = 0 To (.ListItems.Count - 1)
For k = 0 To (.ColumnHeaders.Count - 1)
sData = sData + .ListItems(j).SubItems(k) & ";"
If k = 5 Then sData = sData + vbNewLine
Next k
Next j
Text1.Text = sData
End With

Any idea?
 
Hi there,

what exactly does not work as expected? Do you get too few items? Is one column missing or is the format incorrect?
At first glance it looks OK, apart from two things:

a) When concatenating strings, always use "&", not "+". VB might interpret it correctly but you should use the concatenation operator consistently; not + in the first and & in the second half of your operation.

b) Why loop through "...count", when you hardcode the column number to 6 anyway? ==> No need for that extra check:

Code:
[b]sData=""[/b]
For j = 1 To .ListItems.Count
     For k = 1 To .ColumnHeaders.Count
        sData = sData [b]&[/b] .ListItems(j).SubItems(k) & ";"
     Next k
     sData=sData & vbNewLine
Next j

But apart from that I see no major issue yet.


[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top