Results 1 to 10 of 10
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Multi Select List box


    I am attempting to make a multiselect list box where I can report out more than 1 selection at one time. Where I select 1 or multiple I get a form devoid of data.

    Field_Name Row Source

    SELECT DISTINCT Field.Field_Name, Field.Field_ID
    FROM Field
    WHERE (((Field.Field_ID)<>0))
    ORDER BY Field.Field_ID;

    Form VBA Code:

    Code:
    Option Explicit
    Private Sub Cancel_By_Field_Report_Click()
        DoCmd.Close acForm, "Select Field"
    End Sub
      
    Private Sub Open_By_Field_Report_Click()
        Dim oItem As Variant
        Dim sTemp As String
        Dim iCount As Integer
        sTemp = Nz(Me![My_Field].Value, " ")
        iCount = 0
        If Me![My_Field].ItemsSelected.Count <> 0 Then
            For Each oItem In Me![My_Field].ItemsSelected
                If iCount = 0 Then
                    sTemp = sTemp & Me![My_Field].ItemData(oItem)
                    iCount = iCount + 1
                Else
                    sTemp = sTemp & "," & Me![My_Field].ItemData(oItem)
                    iCount = iCount + 1
                End If
            Next oItem
        Else
            MsgBox "Nothing was selected", vbInformation
            Exit Sub
        End If
        Me![Field_Name].Value = sTemp
        DoCmd.OpenReport "By Field Report", acViewReport, "", "", acNormal
        DoCmd.Close acForm, "Select Field"
    End Sub
    By Field Query:

    Code:
    [SELECT Field.Field_ID, Field.Field_Name, System.[System Name], Nomenclature.Nomenclature, Component.Component, Version.Version, Accreditation.[ACC_#], Accreditation.Type_ACC, Accreditation.Accred, Accreditation.Expires, Accreditation.Compliance, Main.Hold
    FROM Version RIGHT JOIN (System RIGHT JOIN (Nomenclature INNER JOIN (Field INNER JOIN (Component INNER JOIN (Accreditation INNER JOIN Main ON Accreditation.Accred_ID = Main.[ACC_#]) ON Component.Comp_ID = Main.Comp_ID) ON Field.Field_ID = Main.Field_ID) ON Nomenclature.Nomen_ID = Main.Nomen_ID) ON System.SYS_ID = Main.SYS_ID) ON Version.Vers_ID = Main.Vers_ID
    GROUP BY Field.Field_ID, Field.Field_Name, System.[System Name], Nomenclature.Nomenclature, Component.Component, Version.Version, Accreditation.[ACC_#], Accreditation.Type_ACC, Accreditation.Accred, Accreditation.Expires, Accreditation.Compliance, Main.Hold
    HAVING (((Field.Field_Name) Like [forms]![Select Field].[Field_Name] & "*"))
    ORDER BY Field.Field_ID;

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't think it will work that way. I'd take the criteria out of the query and do this:

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Pbaldy,
    Thanks. I get the jist of what you are doing with the code, but if I select 1 or more than 1 item, the report lists all fields. It doesn't report the selected ones.

    Code:
    Private Sub Open_By_Field_Report_Click()
        Dim strWhere As String
        Dim ctl As Control
        Dim varItem As Variant
        Set ctl = Me.My_Field
    If Me.My_Field.ItemsSelected.Count = 0 Then
         MsgBox "Nothing was selected"
            Exit Sub
        End If
    For Each varItem In ctl.ItemsSelected
      strWhere = strWhere & ctl.ItemData(varItem) & ","
      strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
    Next varItem
        strWhere = Left(strWhere, Len(strWhere) - 1)
        DoCmd.OpenReport "By Field Report", acViewReport, "", "", acNormal
        DoCmd.Close acForm, "Copy of Select Field"
    End Sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You didn't use strWhere in the OpenReport line.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Not sure how to do that. I have the report directed to the [By Field] query for its data.

    In the Field_Name control of the [By Field] query I have - Like [forms]![Select Field].[Field_Name] & "*"))

    I am thinking I should change this one somehow.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you review the link? LIke I said, take the criteria out of the query, then combine the code above with

    DoCmd.OpenReport "By Field Report", acViewReport, , "Field_Name IN(" & strWhere & ")"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    OK, Thanks. I guessed on the right track in where you woould put some code in the DoCmd.Open line, but not the correct code. I also took out the like criteria in the Query. When I select the first 2 lines individually or together, I get a runtime error 3075. Syntax error (missing operator) in query expression'Field_Name IN(Digital Radios,'Digital Radios')' or both fields listed if both are selected.

    Click image for larger version. 

Name:	PO-Up.png 
Views:	12 
Size:	22.8 KB 
ID:	22290

    If I select the 3rd line "Fires", I get a Enter Parameter Value pop-up box titled Fires and requesting a variable input. I select OK and leave it blank and it opens up the form with the correct data for Fires. If I select more than 1 line from the 3rd row on down, I get the same pop-up box for each selected item. I select OK for each one and the report opens with the correct selected data.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Take this line out of your code:

    strWhere = strWhere & ctl.ItemData(varItem) & ","

    You want one or the other, not both. The other is for text values, this one for numeric.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Works as advertised!!!! Thanks PBaldy, much appreciated!!!!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Multi select List box
    By crowegreg in forum Forms
    Replies: 1
    Last Post: 12-10-2013, 02:58 PM
  2. Replies: 15
    Last Post: 11-20-2013, 04:30 PM
  3. Replies: 5
    Last Post: 07-26-2012, 12:59 PM
  4. Multi select list box
    By foxtet in forum Forms
    Replies: 1
    Last Post: 05-30-2011, 02:13 PM
  5. Replies: 1
    Last Post: 10-22-2010, 10:11 AM

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