Adding multiple values in a listbox to a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Phille
    New Member
    • Jan 2007
    • 22

    #1

    Adding multiple values in a listbox to a table

    Hi

    I have a form with a few textboxes and some listboxes with multiple values allowed. I would like to use an append query to insert everything into a table but for some reason it just gives me no values at all for the listboxes (the textboxes are ok). Is there something that could be done.

    Thanks in advance
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by Phille
    Hi

    I have a form with a few textboxes and some listboxes with multiple values allowed. I would like to use an append query to insert everything into a table but for some reason it just gives me no values at all for the listboxes (the textboxes are ok). Is there something that could be done.

    Thanks in advance
    To retrieve values from a multiselect listbox:

    Code:
    Dim valSelect As Variant
    Dim strValue As String ' just used for the demonstration
    
       For Each valSelect In Me.listboxName.ItemsSelected
    	  strValue = strValue & ", '" & Me.listboxName.ItemData(valSelect) & "'"
       Next valSelect
    
       ' to remove last comma
       strValue = Left(strValue, Len(strValue)-2)
    Mary

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Originally posted by mmccarthy
      To retrieve values from a multiselect listbox:

      Code:
      Dim valSelect As Variant
      Dim strValue As String ' just used for the demonstration
      
         For Each valSelect In Me.listboxName.ItemsSelected
      	  strValue = strValue & ", '" & Me.listboxName.ItemData(valSelect) & "'"
         Next valSelect
      
         ' to remove last comma
         strValue = Left(strValue, Len(strValue)-2)
      Mary
      There's a small bug with this in that there are two common ways of stripping off the extra ',' and this mixes the two. It adds the extra comma at the front but strips it from the rear. I prefer the front way (oo er) because the code to strip it is shorter, but both ways work.
      Code:
      Dim valSelect As Variant
      Dim strValue As String ' just used for the demonstration
      
          For Each valSelect In Me.listboxName.ItemsSelected
              strValue = strValue & ", '" & Me.listboxName.ItemData(valSelect) & "'"
          Next valSelect
      
          ' to remove leading comma
          strValue = Mid(strValue, 3)

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by NeoPa
        There's a small bug with this in that there are two common ways of stripping off the extra ',' and this mixes the two. It adds the extra comma at the front but strips it from the rear. I prefer the front way (oo er) because the code to strip it is shorter, but both ways work.
        Code:
        Dim valSelect As Variant
        Dim strValue As String ' just used for the demonstration
        
            For Each valSelect In Me.listboxName.ItemsSelected
                strValue = strValue & ", '" & Me.listboxName.ItemData(valSelect) & "'"
            Next valSelect
        
            ' to remove leading comma
            strValue = Mid(strValue, 3)
        Sorry that was my error, meant to place the comma at the end not the front. Good catch Ade.

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Revised code doing it my way ...


          Code:
          Dim valSelect As Variant
          Dim strValue As String ' just used for the demonstration
          
              For Each valSelect In Me.listboxName.ItemsSelected
                  strValue = strValue & "'" & Me.listboxName.ItemData(valSelect) & "', "
              Next valSelect
          
              ' to remove leading comma
              strValue = Left(strValue, Len(strValue)-2)

          Comment

          Working...