Results 1 to 7 of 7
  1. #1
    trussell is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    5

    Listbox to Criteria for Query

    Hi Everyone,

    I'm sure there is a simple answer to this, but I am stuck atm.

    I have a "create report" form, where a user can set certain parameters like the date-range and what specific clients they want to look at.
    I have a list box where the user can select as many clients as they want to be included in the report. Now, the report requested by my boss is strange. A textfilter will not work for this. Long story short, I need the list of selected clients to be fed into a criteria for a query.
    I want to use the In() function for the query criteria.

    If someone could give me some advice for how to approach this, I would greatly appreciate it!


    Thanks.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    You need to loop through the selected items in the list box and build your IN list:
    Code:
    
        'build IN list
    Dim v, sIN as string,sSQL as string
    
    
        With Forms!frmYourForm!lstYourList
            For Each v In .ItemsSelected
                sIN = sIN & "'" & .ItemData(v) & "',"
            Next
        End With
    
    
        sIN = Left(sIN , Len(sIN ) - 1)
    
    sSQL=SELECT tblCustomers.* FROM tblCustomers WHERE CustomerID IN (" & sIN & ");"
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    trussell is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    5
    Hey Vlad,

    Thanks for your response!

    Would I be able to use this "In List" as the criteria for Customer ID in a query?

    Currently for my Criteria in a Query, I have written: In (Forms!CustomerForms!CustomerID)


    Within the Form, I have a button that I want to use to set CustomerID = sIN and then run the query. The button is able to set CustomerID = sIN. However, when I try to run the query, with the criteria statement listed above, I always get a blank result!

    Please let me know if you have any suggestions. Thanks!



    Quote Originally Posted by Gicu View Post
    You need to loop through the selected items in the list box and build your IN list:
    Code:
    
        'build IN list
    Dim v, sIN as string,sSQL as string
    
    
        With Forms!frmYourForm!lstYourList
            For Each v In .ItemsSelected
                sIN = sIN & "'" & .ItemData(v) & "',"
            Next
        End With
    
    
        sIN = Left(sIN , Len(sIN ) - 1)
    
    sSQL=SELECT tblCustomers.* FROM tblCustomers WHERE CustomerID IN (" & sIN & ");"
    Cheers,
    Vlad

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    No, you would need to create a custom function:
    Code:
    Public Function fnSelectedCustomers() as string
    'build IN list
    Dim v, sIN as string,sSQL as string
    
    
    
    
        With Forms!CustomerForms!lstCustomer  'this is your multiselect listbox with CustomerID as the bound column
            For Each v In .ItemsSelected
                sIN = sIN & "'" & .ItemData(v) & "',"
            Next
        End With
    
    
    
    
        sIN = Left(sIN , Len(sIN ) - 1)
    fnSelectedCustomers=sIN
    Now in your query use IN ( & fnSelectedCustomers() & ). Or use a hidden textbox on the form and make its control source the custom function and reference that in the IN clause.

    Or build the query string in VBA like I showed you in the previous post and use that as the record source for the report.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

  6. #6
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I can't tell if you (your boss) wants one report for ALL Customers or if you want one report report per Customer...

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Here's an example Db with a generic public function I use with all my multi-select listboxes.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Pass listbox value into query criteria
    By skydivetom in forum Programming
    Replies: 3
    Last Post: 10-16-2019, 02:23 PM
  2. Replies: 3
    Last Post: 08-16-2019, 05:01 PM
  3. Using a Listbox to build criteria for a query
    By RayMilhon in forum Queries
    Replies: 11
    Last Post: 03-06-2016, 10:44 AM
  4. Replies: 4
    Last Post: 02-14-2013, 09:33 PM
  5. Query criteria retrieved from a Form's listbox
    By blacksaibot in forum Programming
    Replies: 1
    Last Post: 01-29-2010, 10:38 AM

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