Results 1 to 8 of 8
  1. #1
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    36

    Use listbox values as input to SQL query

    Hi,

    I have a listbox that returns some data. I'd like my users to be able to select items from that list box and then use those selected items in a SQL query as a WHERE clause. What's the best way of going about this?

    For example, I have a table that contains timesheet data. I'd like my users to be able to select a few usernames, then use those usernames as a filter to only return the timesheets belonging to those users.

    Thanks in advance!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Use the IN() clause and concatenate the listbox values.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    36
    Thank you! That pointed me in exactly the direction that I needed

  4. #4
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    36
    Darn! So close. Some of the selected values may be present or may not be present (there's also a date filter being used and not all usernames will appear in every selected date range) so the IN() clause returns nothing if they're not present. Do you have any suggestions?

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    That is what you want surely?, else no point selecting items in the listbox, regardless of any other criteria?

    If a bunch of employees are selected for last month, and I only joined this month, then despite my being in the IN list, I am not going to appear as I do not have any timesheet for that period.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    did you fill your listbox with data from the table? to avoid wrong layout
    did you setup your database correctly? meaning text (name) is only once visible.
    did you translate the name into the linking #?

  7. #7
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Quote Originally Posted by ExcessionOCP View Post
    Darn! So close. Some of the selected values may be present or may not be present (there's also a date filter being used and not all usernames will appear in every selected date range) so the IN() clause returns nothing if they're not present. Do you have any suggestions?
    It's hard to tell from your posts up to now, but you can't just create a query and have this work. If you're using a form or report to filter the query, it's infinitely easier. Otherwise, you have to modify the SQL property of your query.

    So then you do the standard looping through the ItemsSelected collection of your multi-select listbox. Pretty sure Dev Ashish wrote an article on how to do that in like 1997. The gist of it is something like

    dim varItem as variant
    dim strFilter as string
    dim lbx as listbox

    set lbx = me.controls("MyListboxName")
    for each varItem in lbx.ItemsSelected
    strFilter = strFilter & "'" & varItem & "'," '-- wrap in single quotes, append a comma
    next varItem

    strSQL = "SELECT * FROM MyTable WHERE strField IN (" & strFilter & ")"

    Currentdb.Querydefs("MyQuery").sql = strSQL

    then you do whatever you're gonna do with the query... open it, base a form or report on it...

  8. #8
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    36
    Thanks for everyone's help here. I changed the workflow slightly by prefiltering the records based on the selected dates, then using the prefiltered data set as the source for the user selection, that way the users were always going to be present in the data set and so Welshgasman's plan worked perfectly. Thanks also to madpiet for his assistance.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2022, 02:03 PM
  2. Pass *multiple* listbox values into query
    By skydivetom in forum Forms
    Replies: 3
    Last Post: 11-05-2019, 05:22 PM
  3. Replies: 2
    Last Post: 02-06-2019, 09:16 AM
  4. Replies: 12
    Last Post: 05-05-2014, 09:23 PM
  5. Table Values As Query Input?
    By joolio in forum Access
    Replies: 2
    Last Post: 01-05-2010, 07:32 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