Solved

Multiple List box selection

Posted on 2016-08-02
5
20 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 34

Accepted Solution

by:
PatHartman earned 500 total points
Comment Utility
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 34

Expert Comment

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

Author Comment

by:simpkinst
Comment Utility
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
Comment Utility
Thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now