Solved

Multiple List box selection

Posted on 2016-08-02
5
32 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 36

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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

749 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