Solved

Specifying number of options selectable in Multi Select List Box

Posted on 2006-06-22
3
288 Views
Last Modified: 2008-03-17
Hi,

Just wondering if there is a method for specifying an exact number of options able to be selected in a multi select list box? For example: I have 2 list boxes on my form, lstAvailGenre and lstSelGenre. Between these list boxes I have two command buttons, cmdAddGenre and cmdRemGenre that add or remove items from/to lstSelGenre. If I have 20 options in the lstAvailGenre list box, I would like to limit users to a maximum of 3 selections. Not sure whether this would be better achieved by phyically limiting the number of items selectable in lstAvailGenre (maybe by dimming out other options until one or more of the selected options have been de-selected) or by limiting the number of items that can be added to lstSelGenre. Any suggestions?
0
Comment
Question by:n00bCoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 16966633
Are you saying on your multi select list, u want the user to select max 3 items only?

try this - listbox is called List0


Private Sub List0_AfterUpdate()

    Dim i As Integer
    Dim iSel As Integer
    Dim iIdx As Integer
   
    'Store current index
    iIdx = List0.ListIndex
   
    'iSel holds number of selected items
    iSel = 0
    For i = 0 To List0.ListCount - 1
        If List0.Selected(i) = True Then iSel = iSel + 1
    Next i

    'If more than 3 then, unselect last selected item
    If iSel >= 4 Then
        MsgBox "Sorry but you cannot select more than 3 items"
        List0.Selected(iIdx) = False
    End If

End Sub
0
 

Author Comment

by:n00bCoder
ID: 16966666
That will do nicely. Probably makes more sense to place the limit on the destination list box rather than the originating list box, but you have given me the headstart I needed to do that myself.
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 16966707
Although this has already been answered I'll offer the solution I was working on at the time.

You'll find it runs a little faster since it only looks at items selected instead of everything in the list.  It also avoids the need to consider whether the list starts counting at 0 or 1 (it starts at 1 if the list has column headers and 0 otherwise).  Hence For i = 0 to List0.ListCount -1 breaks down as soon as your list box uses column headers.  If you wanted to loop thru the entire list for some reason (not really needed here but if you wanted to and easy way to write code that works either way would be to write something like...

For i = abs(Me.List0.ColumnHeads) to Me.List0.ListCount = 1
   'Code goes here
Next i


Private Sub lstMyListBox_AfterUpdate()
    Dim varItem As Variant
    Dim lngCount As Long
    With Me.lstMyListBox
        If (.ItemsSelected.Count > 3) Then
            MsgBox "Only 3 items can be selected", vbCritical, " Update Canceled"
            For Each varItem In .ItemsSelected
                lngCount = lngCount + 1
                If lngCount > 3 Then .Selected(varItem) = False
            Next varItem
        End If
    End With
End Sub
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question