Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array (MSACCESS 2003)

    i have a listbox set as multiselect. In its onclick I have the code:

    Private Sub List225_Click()
    strList1 = ""
    For Each Item1 In Me.List225.ItemsSelected
    strList1 = strList1 & ",'" & Replace(Me.List225.ItemData(Item1), "'", "''") & "'"
    Next Item1
    End Sub

    I then have a command button that retrieves the selected data to put in another list.
    If I have one item selected, it extracts the one item and puts it in the second list, however if I select more than one item in the list, when I click the button is passes duplicated data, despite setting the rowsource of the target list to nothing before it gets refilled.

    Private Sub Command228_Click()
    Me.List229.RowSource = ""

    For Each Item1 In Me.List225.ItemsSelected
    Me.List229.AddItem Mid(strList1, 2) & ")"
    Next Item1

    End Sub

    Anyone know why? Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Array (MSACCESS 2003)

    strList1 contains a concatenated list of the items selected in List225. For example, if the user selected three items A, B and C, strList1 will be <code>",'A','B','C'"</code>

    The On Click event procedure of Command228 adds this concatenated string (minus the first comma) to List229 for each selected item, so if the user selected 3 items, the same string will be added 3 times.

    Do you see the error in your logic?

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array (MSACCESS 2003)

    Thanks Hans, yes I now see the logic. A loop of items selected was what I was looking for first before I cut the code from another part that uses it in SQL code. I will go back to the items selected method. Regards

  4. #4
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array (MSACCESS 2003)

    Three failed attempts to loop a list and get the selected items!! Whats the sectret, thanks

    Private Sub Command228_Click()
    Me.List229.RowSource = ""

    'For Each Item In Me.List225.ItemsSelected
    'If Me.List225.ItemsSelected Then
    'Me.List229.AddItem Me.List225.Value
    'End If
    'Next

    'For ty = 0 To Me.List225.ListCount - 1
    ''Me.List225.SetFocus
    'Me.List225.ListIndex = ty
    'If Me.List225.ItemsSelected(ty) Then
    'Me.List229.AddItem Me.List225 '.Column(ty)
    'End If
    'Next

    'Dim Item As ListItem
    'For Each Item In Me.List225.Items
    'If Item.Selected Then
    'End If
    'Next

    End Sub

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Array (MSACCESS 2003)

    Private Sub Command228_Click()
    Dim Item1 as Variant
    Me.List229.RowSource = ""
    For Each Item1 In Me.List225.ItemsSelected
    Me.List229.AddItem Me.List225.ItemData(Item1)
    Next Item1
    End Sub

    Note: I strongly recommend giving controls meaningful names instead of the default List225, Command228 and List229.

  6. #6
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array (MSACCESS 2003)

    Many thanks Hans. Just what I wanted. Thanks for the advice on control names, I do normally give them some identity in there name later, maybe I should be stricter doing it in the beginning to save time. Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •