Results 1 to 5 of 5
  1. #1
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28

    limit number of selected items in listbox

    I have a variable that i store number of services available to choose based on the package purchased. let us say for 50 dollars you can chose 3 services. So in the listbox I have 15 services, the customer is allowed to pick 3 services only, is there a way in access 2007 listbox to do that?



    Thanks,

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You can use the properties which count and reference the selected items in the listbox control. I put this code in the On_Click event of a listbox, and it does just what you are looking for, including un-selecting the item which was just selected:

    Code:
    Private Sub List0_Click()
      Dim SelectedItem As Integer
      If List0.ItemsSelected.Count > 3 Then
        MsgBox "you cannot select more than three items"
        '
        ' reverse the selection
        '
        SelectedItem = List0.ListIndex
        List0.Selected(SelectedItem) = False
      End If
    End Sub
    Change List0 to the name of your listbox, change the message to what you need, and it should be fine.

  3. #3
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28
    Thanks for the reply, now for the selected items, how to capture their values?

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You can use the ItemsSelected and/or ItemData properties of the listbox. These references from MS explain how to work with them better than I could:

    https://msdn.microsoft.com/en-us/VBA...or=-2147217396

    https://msdn.microsoft.com/en-us/VBA...roperty-access

    You might have to use the Column property if your listbox bound column is not the one that is visible.

    https://msdn.microsoft.com/en-us/vba...or=-2147217396

  5. #5
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28
    thanks alot Mr. John, super

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

Similar Threads

  1. Replies: 16
    Last Post: 03-31-2020, 10:19 AM
  2. Replies: 25
    Last Post: 11-22-2017, 03:18 PM
  3. Replies: 4
    Last Post: 07-26-2016, 02:53 AM
  4. Replies: 2
    Last Post: 03-27-2015, 08:48 AM
  5. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 PM

Tags for this Thread

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