List Box multiple selections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CD Tom
    Contributor
    • Feb 2009
    • 495

    List Box multiple selections

    I've been looking for an answer to what seems like a simple question and haven't found one that makes sense to me. Here's what I'm trying to do. I have a table that has the twelve months of the year. Jan Feb Mar etc. I use this table in lost of different places where the user select the month that will be used in extracts. Now however I want to the user to be able to select multiple months. On my form I created a List box select the Months table and set the Multi select to simple. The list box shows up with the months and allows the user to select multiple months. I have set the Lost focus event with the following code.
    Code:
    Dim ctl As Control, varItm As Variant
    Set ctl = Me![LstMonths]
    For Each varItm In ctl.ItemsSelected
        VMon = ctl.Selected(Months)
    Next varItm
    I loop through the list but the months are not what I selected it's list nothing is selected. If I select three months it code loops through three times but with the first three items in the months table.
    There is a yes/no column in the months table that I would like to mark as true for those months that the user selects.
    Thanks for any help
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Assuming the following:
    1. Table Name is tblData, with Fields:
      1. Month {TEXT} - [Jan...Dec]
      2. Yes/No {YES/NO}
    1. The following Code, placed in the LostFocus() Event of your List Box will do the trick. Simply substitute your Table Name, Name of Field containing Month Values (Jan...Dec), and the Name of the YES/NO Field.
      Code:
      Private Sub lstMonths_LostFocus()
      Dim ctl As ListBox
      Dim varItm As Variant
      
      Set ctl = Me![lstMonths]
      
      If ctl.ItemsSelected.Count < 1 Then Exit Sub
      
      For Each varItm In ctl.ItemsSelected
        CurrentDb.Execute "UPDATE tblData SET tblData.YesNo = True WHERE tblData.Month = '" & _
                           ctl.ItemData(varItm) & "'", dbFailOnError
      Next varItm
      End Sub

    Comment

    • CD Tom
      Contributor
      • Feb 2009
      • 495

      #3
      Yes, the list box uses the tbldata but only shows the month in the box.

      Comment

      Working...