How can I show current listbox selection? VBA for Access

F

FreakyJaBa

Hello All,

I've made a form with a listbox selection.

Can anybody tell me how I can display the current
selection from a Listbox1 into another Listbox?

Regards,

FreakyJaBa
 
P

peter walker

See sample...
www.papwalker.com/public/listbox.mdb
Much of the code below was used to demonstrate another post
however the code dealing with list2 and strItems2 is the key to
filling the 2nd list.
That part of the code could ported to the after update event and
used to populate list2 'live' as it were.



Private Sub Command2_Click()
Dim rst As Recordset
Dim strItems As String
Dim strItems2 As String
Dim LstItem As String
Dim intCurrentRow As Integer
'Empty the table
CurrentDb().Execute ("DELETE FROM ListSelectResults")

Set rst = CurrentDb().OpenRecordset("ListSelectResults")
' Go through the list box items
For intCurrentRow = 0 To List0.ListCount - 1
'Is selected?
If List0.Selected(intCurrentRow) Then
'Get value
LstItem = List0.Column(0, intCurrentRow)
'Add to Table
rst.AddNew
rst("Manufacturer") = LstItem
rst.Update
'Build list for msgbox
strItems = strItems & LstItem & vbCrLf
'Build list for 2nd box
strItems2 = strItems2 & LstItem & ";"
End If
Next intCurrentRow
MsgBox " Done! " & vbCrLf & strItems & vbCrLf & _
"Open the ListSelectResults table"

'If we have any items ...add them to the 2nd box
If Len(strItems2) > 0 Then
Me.List2.RowSource = strItems2
Else 'clear the 2nd box
Me.List2.RowSource = ""
End If
'We use a value list to populate the 2nd listbox.
'For a larger number of items we could set them into
'an array and use the call back or ...

'use a quick and dirty method of putting them
'into a temp table (not very elegent)
rst.Close
Set rst = Nothing
End Sub

--

peter walker MVP

Please post replies to the news group so everyone can benefit.
www.papwalker.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top