Query Criteria based on Multi Selection of a List Box

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
Can a Query Criteria based on Multi Selection of a List Box?

Form = frmCustomerRecord
My List Box = "ListCustomer"
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Can you post some of the code you have already? That may help us provide you with a possible answer.

Brian

Brian J. Torreano
 
Upvote 0
I would if I had some. I'm vey New at Access VBA I asked the question if it's possible to make sure I wasn't chasing my tail.
 
Upvote 0
Take a look here: https://msdn.microsoft.com/VBA/Access-VBA/articles/listbox-itemsselected-property-access
What we also don't know is which column you want to get the criteria from, although the first is the logical guess. Instead of using debug.print in your loop, as the example shows, you'll concatenate the selected field parts to string (I'd call this strWhere) and append it to the beginning part of your sql statement. Depending on your need, that statement could end like " ... IN('" and you append each piece along with a comma separator. Or you can simply append AND or OR along with the selected field parts.
Hope that helps.
 
Upvote 0
Untested, but should be close to what you need to enumerate a list box. The assumption is that the IN clause would work. If not, adjustments will be needed:
Code:
Function BuildSql() 
Dim ctl As Control 
Dim varItem As Variant
Dim strCrit As String, strSql As String

Set ctl = Forms!frmCustomerRecord.ListCustomer
strCrit = ""
strSql = "SELECT SomeField, SomeField2, etc. FROM etc. WHERE SomeField IN ("

For Each varItem In ctl.ItemsSelected 
 strCrit = strCrit & ctl.ItemData(varItm) & ","
Next varItm

strCrit = Left(strCrit,Len(strCrit)-1)
strSql = strSql & strCrit & ");"

'what goes here depends on what you want to do next. 
'Make this the definition of a new or existing query?
'Run the sql? Set it to be the record source for a form or report?
'Execute the sql (if it is an Action query type)?

End Function
 
Last edited:
Upvote 0
Thanks! Very much appreciated. I'll give it a go in the morning
 
Upvote 0
OK, but you'll notice that I flagged one of your threads in the AccessForums.net site as being cross posted. I thought you would know the etiquette around that, and answered your question here. Now I see that you posted this question there also, and thanked them for an answer. This is the sort of behaviour that will very likely curtail the responses you get. Please check this out and see why
https://www.excelguru.ca/content.php?184
 
Last edited:
Upvote 0
Thanks, I didnt know. Just trying to learn and thought many teachers with various views was a good way. Most times there are more than one way to tackle a problem. Will curtail double posting without letting it be known. I almost always Thank the responder which I understand closes the posting so no one continues to work it. My apologies to all
 
Upvote 0

Forum statistics

Threads
1,221,687
Messages
6,161,287
Members
451,695
Latest member
Doug Mize 1024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top