Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Multiple List box selection

Posted on 2016-08-02
5
31 Views
Last Modified: 2016-08-05
I have a multi select list box on a form with a pretty intensive select query and I would like to use what the user selects as the criteria for the intensive query. I have a function that gets the information correctly but when it returns the selection to the query, it is like it returns nothing or something that the query doesn't like. Below you will see the function:

Function SQL_Criteria() As String
Dim varItem As Variant
Dim strCriteria As String
Dim ctrl As Control

Set ctrl = [Forms]![frmMain1].MPN
strCriteria = "'"

For Each varItem In ctrl.ItemsSelected
    strCriteria = strCriteria + ctrl.Column(0, varItem) & "','"
Next varItem
If strCriteria = "'" Then
    SQL_Criteria = "Like '*'"
Else
    SQL_Criteria = "IN(" & Left(strCriteria, Len(strCriteria) - 2) & ")"
End If
   
End Function

I put the call to this function in my where clause, but it doesn't seem to run correctly. It doesn't give me an error just an empty table.

Thanks in advance for all your help.
0
Comment
Question by:simpkinst
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41739609
You can't build a WHERE clause on the fly in a saved querydef.  When a querydef is saved, Access also saves the calculated execution plan.  Changing the WHERE clause would invalidate the execution plan.  

You will need to build the entire SQL string with code.  Then you can concatenate in the selections.

Dim strSQL as String
strSQL = "Select .... From .... Where "
strSQL = strSQL &  "IN(" & Left(strCriteria, Len(strCriteria) - 2) & ")"

Open in new window

Then use the SQL string
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 41739743
The appropriate way to close this would be to post your solution and then select that as the answer.
1
 

Author Comment

by:simpkinst
ID: 41740433
I noticed that the querydef was adding in an "=" sign to the code so I figured out that I had to build the SQL myself as noted above, I had not seen this answer by the time I had figured it out. Thank you, for your help.
0
 

Author Closing Comment

by:simpkinst
ID: 41740437
Thanks for your help.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question