Saving list box items as a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkP
    New Member
    • Dec 2010
    • 15

    Saving list box items as a string

    Is there anyway to save multiple items selected in a list box to a string? Is it then possible to save the string to a record field in a table? Or is there another way to capture multiple items selected and stored them in a table? For example: I have a list box with the following items:
    Listbox
    cat
    dog
    rabbit

    Table name = Animal_tbl
    Animal Selected - this is a column heading and a text field.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Is there anyway to save multiple items selected in a list box to a string?
    Yes there is but:
    1. What is your reason for storing multiple, selected Objects from a Multi-Select List Box to a Field in a Table?
    2. How do you wish the String to appear?
      Code:
      cat,dog,rabbit
      cat dog rabbit
      cat-dog-rabbit
      etc...

    Comment

    • MarkP
      New Member
      • Dec 2010
      • 15

      #3
      I'm trying to store multiple values in a field to reduce the amount of check boxes in a table. My present table has over 75 columns that are used to received check boxes from a form. But I need to capture each record/event from the user. So each record is unique and will be used in trending analysis. If there is another way that I can use to reduce the number of columns in a table then I will be open to that suggestion. Thank you and Happy New Year.

      Comment

      • MarkP
        New Member
        • Dec 2010
        • 15

        #4
        Sorry about not answering your last question. I was hoping to store the string with comas like in your first example above.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Assuming your List Box is named lstAnimals:
          Code:
          Dim ctl As ListBox
          Dim varItm As Variant
          Dim strBuild As String      'To build Animal Selected List
            
          Set ctl = Me![lstAnimals]
            
          If ctl.ItemsSelected.Count < 1 Then Exit Sub
            
          For Each varItm In ctl.ItemsSelected
            strBuild = strBuild & ctl.ItemData(varItm) & ","
          Next varItm
          
          strBuild = Left$(strBuild, Len(strBuild) - 1)
          
          CurrentDb.Execute "INSERT INTO Animal_tbl ([Animal Selected]) VALUES " & _
                            "('" & strBuild & "')", dbFailOnError
          Sample Results:
          Code:
          Animal Selected
          Rabbit
          Cat,Lion
          Cat,Dog,Rabbit,Lion
          Cat,Dog,Rabbit,Mouse,Lion
          Dog
          Dog,Rabbit,Mouse

          Comment

          • MarkP
            New Member
            • Dec 2010
            • 15

            #6
            Thank you for that. You should change your hobby (database programming) to the day job. But either way I'm glad you read my message.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Some things are fun as long as they are a hobby - once they become a job, then they are a burden! (LOL).

              Comment

              Working...