Listbox with Multi-Select Output Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • google@myxware.com

    Listbox with Multi-Select Output Question

    Hello,
    I am trying to create a listbox that users can select multiple
    entries on. I want Access to put each on of those selections in a
    different row on a particular table. This table will also include the
    primary key of the parent form of the form that has the listbox. Here
    is the code that I have. Right now it will put all of the selections
    from the listbox into one row.

    Private Sub Category_Click( )

    Dim frm As Form, ctl As Control
    Dim varItem As Variant
    Dim strSQL As String
    Set frm = Me
    Set ctl = frm!Category
    'Category is the listbox, for some reason if I change the name of
    the listbox to lstCategory and I change the code here the code does not
    work, but this is a secondary issue.
    For Each varItem In ctl.ItemsSelect ed
    strSQL = strSQL & ctl.ItemData(va rItem) & " "
    Next varItem

    Me.catText.Valu e = Me.Category.Ite msSelected.Coun t
    Me.catText2.Val ue = strSQL
    'These two statements above are just for testing purposes so I can see
    what is going on.
    End Sub

    I am new to VB so this question may seem elementary. Thanks for the
    help in advance.

    - Michael

  • Bas Cost Budde

    #2
    Re: Listbox with Multi-Select Output Question

    Is the multiselect listbox really the way to go here? How about a
    subform with a checkbox appended to the records?

    Otherwise, for every pass in ItemsSelected execute an INSERT query. Much
    like
    [color=blue]
    > For Each varItem In ctl.ItemsSelect ed[/color]
    currentdb.execu te "INSERT INTO yourtable and whatsoever VALUES('"&
    ctl.itemdata(va ritem) &"')"[color=blue]
    > strSQL = strSQL & ctl.ItemData(va rItem) & " "
    > Next varItem[/color]
    --
    Bas Cost Budde, Holland


    Comment

    • google@myxware.com

      #3
      Re: Listbox with Multi-Select Output Question

      Ok, I have the data outputting into the table into separate rows.
      The problem is that it does not put one entry per selection. For
      example if I select item 3 and item 5 in the list box I get 3, blank,
      3, and 5 in separate rows (in that order). Is it possible for me to
      get just a 3 in one row and 5 in the next.
      A secondary issue is when I am selecting the data in the listbox and
      I move onto a new entry set I have to deselect the items I do not want
      (that were selected in the previous entry set). Is there some way
      that I can clear the selections in the listbox for each entry set?
      Thanks.

      Here is my latest code:

      Private Sub Category_Click( )

      Dim frm As Form, ctl As Control
      Dim varItem As Variant
      Dim strSQL As String
      Set frm = Me
      Set ctl = frm!Category
      For Each varItem In ctl.ItemsSelect ed

      CurrentDb.Execu te "INSERT INTO tblCategoryTest
      (SelectedCatego ries) VALUES('" & ctl.ItemData(va rItem) & "')"

      Next varItem

      Me.catText.Valu e = Me.Category.Ite msSelected.Coun t
      Me.catText2.Val ue = strSQL
      End Sub

      - Michael

      Comment

      • Bas Cost Budde

        #4
        Re: Listbox with Multi-Select Output Question

        Whose click event is this? Every selection you make in the listbox
        accounts for one click, which may cause the data you see.

        I suggest you put a button next to the listbox, caption "Update" or so
        (find something that matches what happens as exactly as possible).

        Even the Exit event of the listbox is unreliable, unless you start the
        procedure by removing all previous entries from the table.

        As for clearing all selections (which I'd rather hesitate to judge
        'secondary'):
        inside the loop, after you've done the INSERT, do
        ctl.selected(va ritem)=false

        Oh by the way: your frm and ctl references are completely superfluous.
        "Category" would do. I suggest you name it ctlCategory, though, or even
        lstCategory, that tells you from viewing the code what is going on.

        google@myxware. com wrote:
        [color=blue]
        > Ok, I have the data outputting into the table into separate rows.
        > The problem is that it does not put one entry per selection. For
        > example if I select item 3 and item 5 in the list box I get 3, blank,
        > 3, and 5 in separate rows (in that order). Is it possible for me to
        > get just a 3 in one row and 5 in the next.
        > A secondary issue is when I am selecting the data in the listbox and
        > I move onto a new entry set I have to deselect the items I do not want
        > (that were selected in the previous entry set). Is there some way
        > that I can clear the selections in the listbox for each entry set?
        > Thanks.
        >
        > Here is my latest code:
        >
        > Private Sub Category_Click( )
        >
        > Dim frm As Form, ctl As Control
        > Dim varItem As Variant
        > Dim strSQL As String
        > Set frm = Me
        > Set ctl = frm!Category
        > For Each varItem In ctl.ItemsSelect ed
        >
        > CurrentDb.Execu te "INSERT INTO tblCategoryTest
        > (SelectedCatego ries) VALUES('" & ctl.ItemData(va rItem) & "')"
        >
        > Next varItem
        >
        > Me.catText.Valu e = Me.Category.Ite msSelected.Coun t
        > Me.catText2.Val ue = strSQL
        > End Sub
        >
        > - Michael
        >[/color]

        --
        Bas Cost Budde, Holland


        Comment

        Working...