From a brief examination of the MultiPik class in the Access Developer's
Handbook it looks to me that as written it can only show one column in the
list. The code for the class is commented as follows with respect to storing
more than one column:
' This is the data type for the main array.
' It also contains the fSelected flag, indicating whether or not
' each row is currently selected.
' If you need to store more than one column
' of data, you'll need to add more members to this
' data structure.
Private Type DataRow
varData As Variant
fSelected As Integer
End Type
However, using the class as written, you could concatenate the values from
more than one column into a single column in a query. This won't show the
values from each column vertically aligned as separate columns but it will
show the values from both as a single string. This might even be
advantageous if the columns are something like FirstName and LastName as
concatenating these would simply show the full name, e.g. Ken Sheridan.
To try it out open the CH07.mdb file from the ADH CD and create the
following query as qryContacts:
SELECT[Contact Name] & ", " & Address AS [Contact Address]
FROM Customers
ORDER BY [Contact Name];
Then amend the Open event procedure of the frmMultiPik form so it uses this
query:
Private Sub Form_Open(Cancel As Integer)
' Four steps to using Multipik:
' 1. Instantiate the object
' 2. Set up the RowSourceType for the list boxes.
' If you did this at design time, you needn't
' do it now.
' 3. Register the 8 controls with the object
' 4. Tell it where your data comes from
Set mmp = New MultiPik
mmp.RegisterControls _
lstAvailable, lstSelected, _
cmdAddOne, cmdAddAll, _
cmdDeleteOne, cmdDeleteAll, _
cmdUp, cmdDown
' Modify the following line to
' match your own needs.
' Specify a recordsource (table
' or query name) and a field to display.
mmp.SetData "qryContacts", "Contact Address" ' this is the amended line
lstAvailable.RowSourceType = "FillLists"
lstSelected.RowSourceType = "FillLists"
' You can also pass in two arrays.
' mmp.SetArrays Array("Item1", "Item2", "Item3", "Item4", "Item5"),
Array("Item6")
' Or two collections, using the SetCollections method.
End Sub
While the MultiPik class is an elegant and fully featured solution you can
select items from one list box and place them into another much more simply.
The following is the code for the module of a simple form with two 2-column
multiselect list boxes, lstFrom and lstTo, the latter's RowSourceType being a
Value List, and two buttons, one for selecting and moving all rows from
lstFrom to lstTo, the other for clearing all the selections and emptying
lstTo. It works quite well with relatively short lists, but its not really
suitable fro very long lists:
Option Compare Database
Option Explicit
Private Sub cmdClearSelections_Click()
Dim n As Integer
For n = 0 To Me.lstFrom.ListCount - 1
Me.lstFrom.Selected(n) = False
Next n
lstFrom_AfterUpdate
End Sub
Private Sub cmdSelectAll_Click()
Dim n As Integer
For n = 0 To Me.lstFrom.ListCount - 1
Me.lstFrom.Selected(n) = True
Next n
lstFrom_AfterUpdate
End Sub
Private Sub lstFrom_AfterUpdate()
Dim varItem As Variant
Dim strSelectedItems As String
Dim ctrl As Control
Set ctrl = Me.lstFrom
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSelectedItems = strSelectedItems & ";" & _
ctrl.Column(0, varItem) & ";" & _
ctrl.Column(1, varItem)
Next varItem
strSelectedItems = Mid(strSelectedItems, 2)
Me.lstTo.RowSource = strSelectedItems
Else
Me.lstTo.RowSource = ""
End If
Me.lstTo.Requery
End Sub
The 'from' list box remains unchanged, i.e. it doesn't move items from one
to another, but simply copies all the currently selected rows from one into
the other. You can remove an item from the 'to' list simply by deselecting
it in the 'from' list. The order of rows in the 'to' list will always match
that in the 'from' list.
I expect that you could find other examples of how to do this sort of thing
with a bit of Googling around.
Ken Sheridan
Stafford, England