473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

filtering a query by values in multi-select list box ?

Hello.

How to reference selected values from a multi-select list box, as a
criteria in a query ?
Is it possible at all?

Regards,

Zlatko
Jan 17 '06 #1
2 14671
A query cannot read the values in a multi-select list box.

It is possible to write a VBA function that loops through the ItemsSelected
collection, and returns True if the value in the record is one of them (True
becoming the result of a condition in the WHERE clause), but it's messy.

What is the target for this query? If it is for a report, see:
Use a multi-select list box to filter a report
at:
http://allenbrowne.com/ser-50.html

If it is for a form, you could create the Filter string for the form in
exactly the same way as that article creates the WhereCondition for
OpenReport.

If it is for a Recordset, again you could create the entire SQL string that
way.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Zlatko Matif" <zl***********@sb.t-com.hr> wrote in message
news:dq**********@ss405.t-com.hr...

How to reference selected values from a multi-select list box, as a
criteria in a query ?
Is it possible at all?

Regards,

Zlatko

Jan 17 '06 #2
OK. I solved it in this way: I wrote a module with two functions, first one
creates a string to be included inside IN expression of WHERE clause, and
second one to synchonize any "child" listbox in correspondence to "parent"
listbox.

Option Compare Database

Function MultiselectListValues(ListFullName As Control) As String
'This function returns a string, as comma-separated list of values,
'based on selected values in a multi-select list box.
'This string can be used inside IN expression in WHERE clause of a query

Dim strWhere As String, varItem As Variant

'Request to edit items selected in the list box
'If no items selected, then nothing to do
If ListFullName.ItemsSelected.Count = 0 Then Exit Function
'Loop through the items selected collection
For Each varItem In ListFullName.ItemsSelected
'Grab the column for each selected item
strWhere = strWhere & "'" & ListFullName.Column(0, varItem) & "'" & ","
Next varItem
'Throw away the extra comma on the "IN" string
strWhere = Left$(strWhere, Len(strWhere) - 1)
'Finish creation of "IN" string
MultiselectListValues = strWhere

End Function

Function SynchronizeListBox(ParentListFullName As Control, ChildListFullName
As Control, SelectExpression As String, LinkField As String)
'This function synchronize recordset in child list box, based on selectde
values in parent list box.
'Parent list box has a query or SQL statement as its rowsource, while
recordset of child list box is created programmatically.

Dim db As Object
Dim qdf As Object
Dim qdfSQL As String
Dim rs As Object
Dim strWho As String
Dim strWhere As String
Dim strSQL As String

DoCmd.Hourglass True

Set db = CurrentDb

strWho = LinkField
strWhere = MultiselectListValues(ParentListFullName)

Set qdf = db.CreateQueryDef("")
If strWhere <> "" Then
strSQL = SelectExpression & " WHERE (((" & strWho & ")IN(" & strWhere &
")));"
Else
strSQL = "select plants.plant from plants WHERE " & strWho & "=""""" &
";"
End If
qdf.SQL = strSQL
qdf.returnsrecords = True
Set rs = qdf.openrecordset()

Set ChildListFullName.Recordset = rs.clone

rs.Close
qdf.Close

DoCmd.Hourglass False

End Function

"Allen Browne" <Al*********@SeeSig.Invalid> je napisao u poruci interesnoj
grupi:43***********************@per-qv1-newsreader-01.iinet.net.au...
A query cannot read the values in a multi-select list box.

It is possible to write a VBA function that loops through the
ItemsSelected collection, and returns True if the value in the record is
one of them (True becoming the result of a condition in the WHERE clause),
but it's messy.

What is the target for this query? If it is for a report, see:
Use a multi-select list box to filter a report
at:
http://allenbrowne.com/ser-50.html

If it is for a form, you could create the Filter string for the form in
exactly the same way as that article creates the WhereCondition for
OpenReport.

If it is for a Recordset, again you could create the entire SQL string
that way.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Zlatko Matif" <zl***********@sb.t-com.hr> wrote in message
news:dq**********@ss405.t-com.hr...

How to reference selected values from a multi-select list box, as a
criteria in a query ?
Is it possible at all?

Regards,

Zlatko



Jan 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
Im trying to build a graph of values from a counterTable. (Eg - get a list of how many times a page was requested in a week period with each y value being a day). If the table looks like...
3
by: I_was_here | last post by:
Hey if anyone is a query pro please showoff some knowledge thx. Ie: I have a table with : part price location qty 1 part repeats throughout the table and its price remains the same but it...
1
by: cong ngo | last post by:
Hi all If have table with the records below and these data need to be update to another table by the name field. name phone fax emal ted 619 mary 855
3
by: kathyburke40 | last post by:
Odd problem. I have a table in the following format: DocID Question1 Question2 Question3 ------------------------------------------------ 298 1, 2, 3 or 0 Each Question...
0
by: Sam | last post by:
Hi, Dim dvTables As New DataView Dim sql As String sql = "table_name is not NULL" dvTables.RowFilter = sql dvTables.Table = m_dsSysInfo.Tables("Tables") With cboViewTable .DataSource =...
2
by: cypriot | last post by:
Hi. I am developing an application program in java. I use MsAccess for keeping data. I had a problem with sql insert method. public void AddPatient() { String...
1
by: sshafer1 | last post by:
I have a form that is using query as the backend 'table'. I did this so that I could display query fields in my form that have been calculated. 1. How do I write those query field values to a...
2
by: justapawn | last post by:
Can someone assist me in using the correct syntax or method of assigning a variable to the Set portion of a DoCmd.RunSQL Update Query? The help file and most sites, including ms, seem to think the...
2
by: Vince Morgan | last post by:
Hi all, I have a POST form that also includes some anchors/links. If a user clicks a link, that link remains set so that pressing the submit button later not only sends the appropriate POST value,...
26
by: kpouya | last post by:
I am going to be as clear as possible about what i got right now and what i want to achieve Table1: Libary Title - memo Last Name - memo First Name - memo Company - memo Year - number Type -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.