Results 1 to 5 of 5
  1. #1
    zhangyu is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2014
    Posts
    3

    How to assign multi select listbox value to a field on the form

    Hello,

    I am new to design and program FORMs in ACCESS. what i was trying to do is build a multi select list box on the form and then assign user selected item to a field on the form. i found a code snippet online and modified a bit. i can use Messagebox to display selected items, but the value did not saved into my table.

    List133 is the listbox name. i am trying to assgin strValue to a field called "Language" in my table. if it is not a multi selected listbox, the selected item will be saved in to the Language field, but how to make it work for multi-select listbox?

    Thank you for your time and hope my question makes sense to you.

    Private Sub List133_Exit(Cancel As Integer)
    Dim valSelect As Variant
    Dim strValue As String ' just used for the demonstration



    For Each valSelect In Me.List133.ItemsSelected
    strValue = strValue & "'" & Me.List133.ItemData(valSelect) & "', "
    Next valSelect
    ' to remove trailing comma
    strValue = Left(strValue, Len(strValue) - 2)
    MsgBox strValue


    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    The code is not populating the field, it simply sets a variable.

    Instead of the MsgBox line:

    Me!Language = strValue

    This is assuming the form is bound to table and Language field is in the form RecordSource.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    zhangyu is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2014
    Posts
    3
    Thanks, June7. I tried your suggestion, but i got error. the form is bound to a table and language was set to be the CONTROL source for the listbox.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    The code should work. What is the exact error message?

    Don't need the apostrophes in the string concatenation, although should not be an issue.

    strValue = strValue & Me.List133.ItemData(valSelect) & ", "
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    zhangyu is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2014
    Posts
    3
    Hi, June7,

    i think i must have done something stupid. first time i tried, it was not working and gave a compile error, but now it is working. thank you so much for your patience and have a great weekend!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-24-2014, 02:11 PM
  2. Search using a multi select listbox
    By noobaccess in forum Access
    Replies: 13
    Last Post: 12-04-2012, 07:06 AM
  3. multi select listbox
    By crowegreg in forum Forms
    Replies: 3
    Last Post: 07-28-2012, 01:48 PM
  4. Looping through a multi select listbox - how do I do it?
    By shabbaranks in forum Programming
    Replies: 4
    Last Post: 04-02-2012, 11:56 AM
  5. Need Multi-Select listbox code example
    By Buakaw in forum Forms
    Replies: 4
    Last Post: 08-21-2011, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums