-
[RESOLVED] Loop through listview adding items
Hi,
How would I loop through a list view control to add multiple items?
This is the code I use for a single items but I can't get it working in a loop.
vb Code:
'Retrieve mail from inbox
Dim iMsgID As Integer
Dim COLH As Integer
'Dim mSubitem As ListItems
For COLH = 0 To 2
If frmMain.LstNewMail.ListItems.Count = 0 Then
'For Each mSubitem In frmMain.LstNewMail.ListItems
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(iMsgID).From)
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Subject
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Date
' Next mSubitem
End If
Next COLH
The code I have commented out was my attempt to insert items using a loop.
Thanks,
Nightwalker
-
Re: Loop through listview adding items
I am sorry but I didn't get you...
Here is a simple way to add items to listview. This example adds one single item. You may amend this for a loop :)
Code:
Option Explicit
Dim colX As ColumnHeader, itmX As ListItem
Private Sub Form_Load()
ListView1.View = lvwReport
Set colX = ListView1.ColumnHeaders.Add(, , "Header1")
Set colX = ListView1.ColumnHeaders.Add(, , "Header2")
Set colX = ListView1.ColumnHeaders.Add(, , "Header3")
Set colX = ListView1.ColumnHeaders.Add(, , "Header4")
End Sub
Private Sub Command1_Click()
Set itmX = ListView1.ListItems.Add(, , Text1.Text)
ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , Text2.Text
ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , Text3.Text
ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , Text4.Text
End Sub
-
Re: Loop through listview adding items
Whatever is containing the pop3 messages that you want to add to the listview is where the loop needs to take place.
-
Re: Loop through listview adding items
The above code is in the "Done" event of the pop component.
vb Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim iMsgID As Integer
Dim COLH As Integer
'Dim mSubitem As ListItems
For COLH = 0 To 2
If frmMain.LstNewMail.ListItems.Count = 0 Then
'For Each mSubitem In frmMain.LstNewMail.ListItems
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(iMsgID).From)
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Subject
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Date
' Next mSubitem
End If
Next COLH
End Sub
However, when I use:
vb Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim iMsgID As Integer
Dim COLH As Integer
Dim mSubitem As ListItems
For COLH = 0 To 2
If frmMain.LstNewMail.ListItems.Count = 0 Then
For Each mSubitem In frmMain.LstNewMail.ListItems
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(iMsgID).From)
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Subject
COLH = COLH + 1
itmM.SubItems(COLH) = Pop3.Messages(iMsgID).Date
Next mSubitem
End If
Next COLH
End Sub
Nothing appears to happen the data does not show in the list view.
Edit:
I have managed to loop through the list of messages twice but if I use the following code my code is going to very messy. This is what I came up with:
vb Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim i As Integer
Dim s As Integer
Dim COLH As Integer
For COLH = 0 To 2
For i = 1 To Pop3.Messages.Count
If frmMain.LstNewMail.ListItems.Count = 0 Then
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
COLH = COLH + 1
itmM.ListSubItems.Add = (Pop3.Messages(i).Subject)
COLH = COLH + 1
itmM.ListSubItems.Add = (Pop3.Messages(i).Date)
Set itmS = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(s).From)
COLH = COLH + 1
itmS.ListSubItems.Add = (Pop3.Messages(s).Subject)
COLH = COLH + 1
itmS.ListSubItems.Add = (Pop3.Messages(s).Date)
End If
Next i
Next COLH
End Sub
-
Re: Loop through listview adding items
Try this:
Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim i As Long
Dim lvwItem As ListItem
For i = 1 To Pop3.Messages.Count
Set lvwItem = ListView1.ListItems.Add(, , Pop3.Messages(i).From)
lvwItem.SubItems(1) = Pop3.Messages(i).Subject
lvwItem.SubItems(2) = Pop3.Messages(i).Date
Next i
End Sub
....:wave:
-
Re: Loop through listview adding items
Is This what you are trying...
Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, _
ByVal HeadersOnly As Boolean)
'~~> Retrieve mail from inbox
Dim i As Integer, itmM As ListItem, lstCount As Long
For i = 1 To Pop3.Messages.Count
lstCount = frmMain.LstNewMail.ListItems.Count
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
frmMain.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Subject
frmMain.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Date
Next i
End Sub
-
Re: Loop through listview adding items
Quote:
Originally Posted by
akhileshbc
Try this:
Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim i As Long
Dim lvwItem As ListItem
For i = 1 To Pop3.Messages.Count
Set lvwItem = ListView1.ListItems.Add(, , Pop3.Messages(i).From)
lvwItem.SubItems(1) = Pop3.Messages(i).Subject
lvwItem.SubItems(2) = Pop3.Messages(i).Date
Next i
End Sub
....:wave:
Quote:
Originally Posted by
koolsid
Is This what you are trying...
Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, _
ByVal HeadersOnly As Boolean)
'~~> Retrieve mail from inbox
Dim i As Integer, itmM As ListItem, lstCount As Long
For i = 1 To Pop3.Messages.Count
lstCount = frmMain.LstNewMail.ListItems.Count
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
frmMain.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Subject
frmMain.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Date
Next i
End Sub
I receive an "Invalid procedure call or argument" on the "Set" line.
-
Re: Loop through listview adding items
NW: show the complete code that you are using?
-
Re: Loop through listview adding items
These are the declarations I am using:
vb Code:
Option Explicit
Public itmM As ListItem
Public itmS As ListItem
together with the code from post #4.
vb Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
Dim i As Integer
Dim s As Integer
Dim COLH As Integer
For COLH = 0 To 2
For i = 1 To Pop3.Messages.Count
If frmMain.LstNewMail.ListItems.Count = 0 Then
Set itmM = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
COLH = COLH + 1
itmM.ListSubItems.Add = (Pop3.Messages(i).Subject)
COLH = COLH + 1
itmM.ListSubItems.Add = (Pop3.Messages(i).Date)
Set itmS = frmMain.LstNewMail.ListItems.Add(, , Pop3.Messages(s).From)
COLH = COLH + 1
itmS.ListSubItems.Add = (Pop3.Messages(s).Subject)
COLH = COLH + 1
itmS.ListSubItems.Add = (Pop3.Messages(s).Date)
End If
Next i
Next COLH
End Sub
-
Re: Loop through listview adding items
Did your present code works ? :confused:
I mean, does it show the same error message ?
-
Re: Loop through listview adding items
No, the code in my previous post (post #9) works! However, you will notice I keep repeating the code when it comes to adding the items to the listview.
-
Re: Loop through listview adding items
Dont use your code. use the code in post 6 and it will work...Comment rest of the code and then try...
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
Dont use your code. use the code in post 6 and it will work...Comment rest of the code and then try...
I have already tried it as stated above! I receive an "Invalid procedure call or argument" on the "Set" line.
-
Re: Loop through listview adding items
NW: It works on my end... Can you upload your App. Let me test it myself for a faster resolution...
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
NW: It works on my end... Can you upload your App. Let me test it myself for a faster resolution...
Sure! Here it is.
Edit:
Uploaded an update of the attachment that was posted here to post #34.
-
Re: Loop through listview adding items
The highlighted code is wrong when you are adding subitems.
Code:
itmM.ListSubItems.Add = (Pop3.Messages(i).Subject)
try it like
Code:
itmM.ListSubItems.Add Text:=(Pop3.Messages(i).Subject)
-
Re: Loop through listview adding items
Quote:
Originally Posted by
dee-u
The highlighted code is wrong when you are adding subitems.
Code:
itmM.ListSubItems.Add = (Pop3.Messages(i).Subject)
try it like
Code:
itmM.ListSubItems.Add Text:=(Pop3.Messages(i).Subject)
I just tried it but didn't notice any difference! Why do it that way?
-
Re: Loop through listview adding items
Quote:
Originally Posted by
Nightwalker83
I just tried it but didn't notice any difference! Why do it that way?
I posted without testing it but when I tried your code it is also working and VB6 does not complain, its my first time to see such code and thought it was wrong, I'm sorry.
-
Re: Loop through listview adding items
NW: When I tried it. It is asking me for a missing reference. Which reference are you using?
Sid
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
NW: When I tried it. It is asking me for a missing reference. Which reference are you using?
Sid
You will need to download the pop component from the link posted here, the We Only Do link and install the component and add a reference to it in the project.
-
Re: Loop through listview adding items
It is asking me to buy it :lol:
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
It is asking me to buy it :lol:
On the right-hand side of the page is a toolbox one of the options is to download the evaluation version of the component.
-
Re: Loop through listview adding items
This might sound funny to you but I am unable to find the wodPop3.dll on my pc... lolzzz
I have already downloaded and installed it...
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
This might sound funny to you but I am unable to find the wodPop3.dll on my pc... lolzzz
I have already downloaded and installed it...
Check the "System 32" (Vista/Win7) or whichever the folder is on the other Windows operating systems.
-
Re: Loop through listview adding items
sid, if you have installed the component, then check for this name in your components window:
WeOnlyDo! COM Pop3 Client Component
Cool company name :lol:
-
Re: Loop through listview adding items
I revisited the link that you gave above... Well there is some misunderstanding probably on my part... I was using a different link till now
I am now using this
http://www.weonlydo.com/Pop3/pop3-activex-component.asp
EDIT
Ok Try this... In your Class1.cls remove the existing code and copy and paste the below...
Code:
Option Explicit
Dim MsgCount As Integer
Private WithEvents Pop3 As wodPop3Com
Public Sub Connection()
Select Case Trim(Form1.cboHost.Text)
Case "Gmail"
With Pop3
.HostName = "pop.gmail.com"
.Login = ""
.Password = ""
.Port = 995
.Connect
End With
Case "Other"
With Pop3
.HostName = ""
.Login = ""
.Password = ""
'.Port = 110
.Connect
End With
End Select
End Sub
Private Sub Pop3_Connected()
Form1.Caption = Form1.Caption & " " & "You have " & MsgCount & " new messages!"
Pop3.Messages.GetAll
End Sub
Private Sub Pop3_StateChange(ByVal OldState As WODPOP3COMLib.StatesEnum)
Form1.Caption = Pop3.StateText
Debug.Print Pop3.StateText
End Sub
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, _
ByVal HeadersOnly As Boolean)
Dim i As Integer, itmM As ListItem, lstCount As Long
For i = 1 To Pop3.Messages.Count
Set itmM = Form1.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
lstCount = Form1.LstNewMail.ListItems.Count
Form1.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Subject
Form1.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Date
Next i
End Sub
Private Sub Class_Initialize()
Set Pop3 = New wodPop3Com
End Sub
Private Sub Class_Terminate()
Set Pop3 = Nothing
End Sub
See if this works...
-
Re: Loop through listview adding items
I receive an "Invalid procedure call or argument" error:
Code:
Set itmM = Form1.LstNewMail.ListItems.Add (, , Pop3.Messages(i).Form)
However, comparing that code with the last code in post #4 the line is the same.
Edit:
This code works! For some strange reason it didn't like the "For" statement.
Code:
Private Sub Pop3_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
'Retrieve mail from inbox
'Ín-order to view information correctly make sure the view property for LstNewMail is set to Report.
Dim i As Integer, itmM As ListItem, lstCount As Long
Do Until i = Pop3.Messages.Count
Set itmM = Form1.LstNewMail.ListItems.Add(, , Pop3.Messages(i).From)
lstCount = Form1.LstNewMail.ListItems.Count
Form1.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Subject
Form1.LstNewMail.ListItems(lstCount).ListSubItems.Add _
, , Pop3.Messages(i).Date
i = i + 1
Loop
End Sub
-
Re: Loop through listview adding items
Quote:
I receive an "Invalid procedure call or argument" error:
NW...
Did you do as I suggested?
Did you delete all the existing code from Class1.cls and paste the code that I posted AS IT IS in post 26 (without any changes)?
For once forget all your existing code. Forget your code which is in post 4 or any other posts in this thread... :)
Simply delete all the existing code from Class1.cls and paste the code that I posted AS IT IS in post 26
Now test it :)
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
For once forget all your existing code. Forget your code which is in post 4 or any other posts in this thread... :)
Yes, I did! I received the error posted in my previous post. I think it might be a problem with my Visual Basic installation since you aren't experiencing the same problems as I am when we both run the EXACT same code.
Edit:
I receive the same error as I did running the below code but in that case it was complaining that "i" had no value so I set a value for "i" and the problem resolved itself.
Quote:
Originally Posted by
chelledon
private Sub Command1_Click()
Dim x As Integer, ng As Integer, b As Integer, counter As Integer
Dim z As String, y As String, char1 As String, str1 As String
z = 0
For x = 1 To Len(Text1.Text)
y = Mid(Text1.Text, x, 1)
If (y) = "ng" Then
z = z + 1
Label.Caption = z
End If
Next
counter = 0
str1 = Text1.Text
For b = i To Len(str1)
char1 = char1 + Replace(str1, char1, "", i)
Form1.Caption = Form1.Caption + char1
Next
End Sub
-
Re: Loop through listview adding items
NW that's because that is not what I suggested :)
You are missing an "=" sign... see below...
Quote:
Originally Posted by
Nightwalker83
I receive an "Invalid procedure call or argument" error:
Code:
Set itmM Form1.LstNewMail.ListItems.Add (, , Pop3.Messages(i).Form)
-
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
NW that's because that is not what I suggested :)
You are missing an "=" sign... see below...
No! Sorry, that was a typo. Just tried your code by copying and pasting the code exactly how it appears in your post into my project and the above error happened.
Edit:
:lol: The only part that was missing from your code and why the error was occurring was this piece of code just before the "Next".
-
Re: [RESOLVED] Loop through listview adding items
NW, In a FOR loop, the counter variable is automaically incremented by 1 (default). There is no need of incrementing it by yourself !
Quote:
Originally Posted by http://www.vb6.us/tutorials/understanding-do-and-while-loops
By default, the variable used in the declaration of the For-Next loop is incremented by 1 each time through the loop; however, if you want to increment this value by a different amount each time through the loop, you can simply append Step (Integer) to the end of the For-Next loop declaration.
...:wave:
-
Re: [RESOLVED] Loop through listview adding items
Quote:
Originally Posted by
akhileshbc
NW, In a FOR loop, the counter variable is automaically incremented by 1 (default). There is no need of incrementing it by yourself !
Yeah, I know! However, for some reason it does not increment the value automatically.
Edit:
Although, the code using a "For" statement in my original post works as it should.
Edit:
I just tried this code and it works! I reckon the "done" event of the pop component is causing the problem or maybe I need to download the component again.
vb Code:
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
Me.Caption = LstNewMail.ListItems.Add(, , i)
Next i
End Sub
-
1 Attachment(s)
Re: Loop through listview adding items
Quote:
Originally Posted by
koolsid
I revisited the link that you gave above... Well there is some misunderstanding probably on my part... I was using a different link till now
I have attached my project using your code below.
Edit:
@ koolsid,
Did you get a chance to test the file I uploaded above?
-
Re: [RESOLVED] Loop through listview adding items
No NW. I have been pretty busy with my office work but will do it today evening :)
-
Re: [RESOLVED] Loop through listview adding items
Quote:
Originally Posted by
koolsid
No NW. I have been pretty busy with my office work but will do it today evening :)
Ah ok! I suppose there is no rush to do it now since I have resolved the original question. However, I would like to find out why the variable value wasn't being initialized in the code.