How can I store in a table the data for each row selected
using Simple Multi-Select Listbox property?
The best way is to not use that approach at all as it is poor database design
and can be difficult to implement. Instead, use a child table and form to store
the individual values and a combo or list box to enter the one value per record.
That having been said, if you must move forward with this approach, you can
retrieve the individual selected values and concatenate them into a single
string to store in a field, using the following approach:
Copy this function to a standard module (global module, not a module behind a
form or report):
'****FUNCTION START
Public Function fncBuildListFromMSelectListbox(ctlListBox As Control, _
Optional strDelimiter As String = ",") As String
' Comments : Builds a CSV list from selections
' in multi-select listbox
' Parameters: ctlListBox - a reference to the listbox control
' strDelimiter - character to use as delimiter
' Default delimiter is ","
' Returns : String - the "list"
' Created : 11/12/03 14:15 Bruce M. Thompson
' Modified :
'
' --------------------------------------------------
On Error GoTo fncBuildListFromMSelectListbox_ERR
Dim varItem As Variant
Dim strList As String
'Build the string by obtaining the value of each
'selected row and concatenating
For Each varItem In ctlListBox.ItemsSelected
strList = strList + ctlListBox.Column(0, varItem) & strDelimiter
Next
'Remove extra delimiter from end of string
If Right(strList, 1) = strDelimiter Then _
strList = Left(strList, Len(strList) - 1)
'Assign list as function's return value
fncBuildListFromMSelectListbox = strList
fncBuildListFromMSelectListbox_EXIT:
Exit Function
fncBuildListFromMSelectListbox_ERR:
MsgBox "Error " & Err.Number & _
" occurred in fncBuildListFromMSelectListbox: " _
& Err.Description
Resume fncBuildListFromMSelectListbox_EXIT
End Function
'****FUNCTION END
You can assign the value to your field using the following syntax (you can use
the listbox's "OnExit" event procedure):
Me.txtValueList = fncBuildListFromMSelectListbox(Me.ListBox1)
You will need to parse the values from the field in order to use them (such as
reselecting the items in the listbox in the "OnCurrent" event to display the
values).
Now, you might, again, want to reconsider the child table approach. <g>