|
-
Mar 19th, 2007, 12:53 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Help with a For loop
Hi everyone,
I have data in a listbox, how do I loop through the data in the listbox and do a message box of each line of data in that listbox?
data in the listbox:
Frank
Bill
Jill
How do I code a button to loop through the listbox and give a message box of each item in the listbox?
For example, a message box should pop up for each person, Frank, Bill, and Jill.
-
Mar 19th, 2007, 12:58 PM
#2
Re: Help with a For loop
 Originally Posted by Giraffe Frenzy
Hi everyone,
I have data in a listbox, how do I loop through the data in the listbox and do a message box of each line of data in that listbox?
Try this:
VB Code:
Private Sub Command1_Click()
Dim a As Long
With List1
For a = 0 to .ListCount
MsgBox .List(a), vbInformation, "Listbox Printout"
Next a
End With
End Sub
Private Sub Form_Load()
Dim a As Long
For a = 1 To 10 'Add some items
List1.AddItem "Item " & a
Next a
End Sub
Last edited by Mark Gambo; Mar 19th, 2007 at 01:06 PM.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Mar 19th, 2007, 12:58 PM
#3
Re: Help with a For loop
Code:
Dim i As Integer
For i = 0 To List1.Listcount - 1
MsgBox List1.List(i)
Next i
-
Mar 19th, 2007, 01:00 PM
#4
Lively Member
Re: Help with a For loop
Do you want to display messageboxes for each item? For what purpose?
-
Mar 19th, 2007, 01:21 PM
#5
Lively Member
Re: Help with a For loop
or:
Code:
Private Sub Command1_Click()
Dim x As Integer, i As Integer
For i = 0 To List1.ListCount - 1
x = MsgBox(List1.List(i), vbOKCancel, "is this what you want?")
Next i
End Sub
Private Sub Form_Load()
List1.AddItem "Item A"
List1.AddItem "Item B"
List1.AddItem "Item C"
List1.AddItem "Item D"
List1.AddItem "Item E"
End Sub
-
Mar 19th, 2007, 01:22 PM
#6
Thread Starter
Addicted Member
Re: Help with a For loop
Thanks guy. That is exactly what I needed!
-
Mar 19th, 2007, 01:32 PM
#7
Re: Help with a For loop
 Originally Posted by Ethan37
or:
Code:
Private Sub Command1_Click()
Dim x As Integer, i As Integer
For i = 0 To List1.ListCount - 1
x = MsgBox(List1.List(i), vbOKCancel, "is this what you want?")
Next i
End Sub
Private Sub Form_Load()
List1.AddItem "Item A"
List1.AddItem "Item B"
List1.AddItem "Item C"
List1.AddItem "Item D"
List1.AddItem "Item E"
End Sub
Extending on this a little (cause I'm bored)
vb Code:
Private sub command1_Click()
Dim msgRet as vbmsgboxresult, i as integer
for i = 0 to List1.Listcount - 1
msgRet = MsgBox(list1.list(i), vbQuestion + vbYesNo, "Is this what you want?")
If msgRet = vbYes then exit for
next i
End sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|