Results 1 to 4 of 4
  1. #1
    ro88y09 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    8

    Listbox will work when multiselect=simple

    Listbox will work when multiselect=none, but it wont work when multi-select = simple or extended. After I have a listbox setup, I can select multiple values without any issue. As soon as I open up my query, it returns without any records. I can also clear the criteria, [Forms]![aaDashboard]![Location2], in the Bay column of my query and it will return all records as it should.



    I believe I have everything setup correctly:

    Form: aaDashboard
    Row Source: Values
    Listbox Name: Location2

    Query: aaDailyProductionComparison
    Bay: [Forms]![aaDashboard]![Location2]

    This is what I think could be the issue:
    -There is a weird value setting, in my query or form, that is messing me up.
    -I need to open up my form in such a way that it explains "Location2" on the form is linked to "Bay" in the query.

    Has anyone else come across this before? Any ideas?

    Thanks in advance

  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
    You can't simply refer to the listbox when it's multiselect, you need code to read the selected values. One option if you're using the query for a form or report:

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

  3. #3
    ro88y09 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    8
    Thanks for the response pbaldy. Much appreciated. I have put the following in my code. The only issue I face now is how to open the query with a "where" statement. See my code below:

    Code:
    Private Sub cmdOpenQuery_Click()
    Dim strWhere As String
     Dim ctl As Control
     Dim varItem As Variant
    'make sure a selection has been made
     If Me.Location2.ItemsSelected.Count = 0 Then
       MsgBox "Must select at least 1 machine location"
       Exit Sub
     End If
    'add selected values to string
     Set ctl = Me.Location2
     For Each varItem In ctl.ItemsSelected
       strWhere = strWhere & ctl.ItemData(varItem) & ","
       'Use this line if your value is text
       'strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
     Next varItem
    'trim trailing comma
     strWhere = Left(strWhere, Len(strWhere) - 1)
    'open the report, restricted to the selected items
     DoCmd.OpenQuery "aaDailyProductionComparison", acViewNormal, "Bay IN(" & strWhere & ")"
     
    Exit_cmdOpenQuery_Click:
      Exit Sub
    Err_cmdOpenQuery_Click:
      MsgBox Err.Description
      Resume Exit_cmdOpenQuery_Click
      
    End Sub
    I believe my command to open the query is wrong. Everytime I try to run this code, I get the error, "Run-time error '13':" If I simply command the query to open, it obviously opens the query without any results.

  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
    That technique is designed to work with OpenForm or OpenReport (OpenQuery does not have the same argument). You would take the criteria out of the query, and use OpenForm or OpenReport with strWhere.

    Also, most of us never expose queries or tables to users, just forms and reports.
    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. Multiselect Listbox
    By wwhit in forum Forms
    Replies: 19
    Last Post: 03-09-2015, 02:58 PM
  2. VBA Select All Rows in a MultiSelect ListBox
    By ItsMe in forum Code Repository
    Replies: 0
    Last Post: 04-09-2014, 01:52 PM
  3. Replies: 4
    Last Post: 06-24-2013, 07:34 AM
  4. Listbox multiselect status
    By Sam23 in forum Programming
    Replies: 5
    Last Post: 03-06-2012, 01:13 PM
  5. Append Records using MultiSelect Listbox
    By Ted C in forum Programming
    Replies: 14
    Last Post: 03-15-2011, 01:25 PM

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