Solved

Multiple List box selection

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

863 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

20 Experts available now in Live!

Get 1:1 Help Now