Selecting Multiple Entries from a List Box (1 Viewer)

Wee

Shortest Board Member
Local time
Today, 15:56
Joined
Aug 11, 2004
Messages
13
I have been working on a database for some time now and on the whole have been able to solve problems on my own, but this one has me stumped at the moment. I had no previous knowledge of access before I started this.

I am looking to have a list box where I can select more than one entry which will put the selected entries into a text box, in a similar way to selecting e-mail recipients.

Any advice would be greatly recieved.
 

yellow

Registered User.
Local time
Today, 15:56
Joined
May 16, 2002
Messages
122
On the list box properties change the 'Multi Select' property to simple or extended. The following should give you a general idea of how to achieve what you want.

Private Sub Command4_Click()
Dim intCurrentRow as Integer

For intCurrentRow = 0 To List2.ListCount - 1
If List2.Selected(intCurrentRow) Then
Text0 = Text0 & List2.ItemData(intCurrentRow)
End If
Next intCurrentRow

End Sub
 

Wee

Shortest Board Member
Local time
Today, 15:56
Joined
Aug 11, 2004
Messages
13
Thank you!

Thanks very much that is exactly what I wanted. It works a treat :)
 

Wee

Shortest Board Member
Local time
Today, 15:56
Joined
Aug 11, 2004
Messages
13
Part 2...

As I said before my knowledge of Access isn't great and I have encountered another problem.

I have decided to give the list box two columns. I would like column one to input into one text box, as above, and column two to input into another. I am sure this is pretty easy to do by referring to the specific column but I really don't fully understand the code and so don't know where to put it.

Thanks for any help,

Wee
 

Mile-O

Back once again...
Local time
Today, 15:56
Joined
Dec 10, 2002
Messages
11,316
yellow said:
Private Sub Command4_Click()
Dim intCurrentRow as Integer

For intCurrentRow = 0 To List2.ListCount - 1
If List2.Selected(intCurrentRow) Then
Text0 = Text0 & List2.ItemData(intCurrentRow)
End If
Next intCurrentRow

End Sub
That's a bit of a time waster. ;)


Code:
Dim var As Variant
For Each var In Me.MyListBox.ItemsSelected
    Me.MyTextBox = Me.MyTextBox & Me.MyListBox.Column(1, var)
Next
 

yellow

Registered User.
Local time
Today, 15:56
Joined
May 16, 2002
Messages
122
Try this:

Private Sub Command1_Click()
Dim intCurrentRow

For intCurrentRow = 0 To List1.ListCount - 1
If List.Selected(intCurrentRow) Then
Text1 = Text1 & List1.Column(0, intCurrentRow)
Text2 = Text2 & List1.Column(1, intCurrentRow)
End If
Next intCurrentRow
End Sub

You are now just refering to the columns index and the current row.
 

Wee

Shortest Board Member
Local time
Today, 15:56
Joined
Aug 11, 2004
Messages
13
Brilliant, thank you. :)
 

Mile-O

Back once again...
Local time
Today, 15:56
Joined
Dec 10, 2002
Messages
11,316
yellow said:
Private Sub Command1_Click()
Dim intCurrentRow

For intCurrentRow = 0 To List1.ListCount - 1
If List.Selected(intCurrentRow) Then
Text1 = Text1 & List1.Column(0, intCurrentRow)
Text2 = Text2 & List1.Column(1, intCurrentRow)
End If
Next intCurrentRow
End Sub

Why prefix a Variant variable with int? That's just confusing.

And why are you going through every item in the listbox when it's much simpler to navigate through the listbox's ItemsSelected collection?

You also haven't referenced the form's Class.
 

yellow

Registered User.
Local time
Today, 15:56
Joined
May 16, 2002
Messages
122
The intCurrentRow is supposed to be an Integer. But for some stupid reason I didn't type that. Apologies for not being perfect.

I don't claim to be any sort of Access genius, I was just trying to help someone out. So I have learnt something new from this post as well. But there's no need to say, ...'why are you doing this' and ..'you haven't done that'. Why not just say that a more efficient method would be to blah blah blah.

Secondly by highlighting my vastly inefficient methods to Wee, they have two answers to their problem. But before I replied they had no answers.
 

dent

Registered User.
Local time
Today, 15:56
Joined
Sep 22, 2005
Messages
17
SJ McAbney, could you tell me how to add commas between the selections added to the list box! I used you r code and it works great but I need the seperations,

Dim var As Variant
For Each var In Me.MyListBox.ItemsSelected
Me.MyTextBox = Me.MyTextBox & Me.MyListBox.Column(1, var)
Next


thanks for your help :eek:
 

dent

Registered User.
Local time
Today, 15:56
Joined
Sep 22, 2005
Messages
17
Or preferably on seperate lines in the text box,thanks.
 

Users who are viewing this thread

Top Bottom