473,508 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql langage with multiselect listbox

56 New Member
Hello,

I have a table and a multiselect listbox.
The table is on the beginning of the event on click of a cmd button.
The table is like :
Expand|Select|Wrap|Line Numbers
  1. strMain = "SELECT DISTINCT [A], [b], [C], [D], [E], [F] FROM TblR"
  2. Set rstMain = db.OpenRecordset(strMain, dbOpenDynaset)
After I have a code.
I would like to put a criteria like C=CLIST when you select multiple items in the list, to use it in my code
I know that I have to use a loop and .Itemsselected but I don't know how to write it
I am working on Acess 2010

Thank you very much
Apr 11 '13 #1
1 1227
ADezii
8,834 Recognized Expert Expert
The Logic is to iterate the List of Items Selected, if any, while at the same time building a Criteria String. I created some Code for you that uses the Sample Northwind Database. The List Box contains all unique Regions for Employees, while the generated SQL will represent only those Region(s) selected from the List Box by the User. This is only one of many approaches - any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim lst As ListBox
  2. Dim varItem As Variant
  3. Dim strCriteria As String
  4. Dim strSQL As String
  5.  
  6. Set lst = Me![lstRegions]
  7.  
  8. 'If no Items were selected, then get out
  9. If lst.ItemsSelected.Count = 0 Then Exit Sub
  10.  
  11. 'Iterate each Item Selected & build Criteria String
  12. For Each varItem In lst.ItemsSelected
  13.   strCriteria = strCriteria & "'" & lst.ItemData(varItem) & "',"
  14. Next
  15.  
  16. 'Strip Last "'"
  17. strCriteria = "[Region] IN (" & Left$(strCriteria, Len(strCriteria) - 1) & ")"
  18. strSQL = "SELECT * FROM Employees WHERE " & strCriteria
  19.  
  20. 'Test Logic
  21. Debug.Print strSQL
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Employees WHERE [Region] IN ('AR','NJ','WA')
Apr 11 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3277
by: arthur-e | last post by:
Hi I'm trying to use a multiselect listbox to limit records in a report. My version at work is 97 but now at home I'm using Access2002- I know I can't go backwards ( to use this or similar code at...
2
6350
by: Sally | last post by:
I have a simple multiselect listbox with a rowsorce of MemberID, MemberName, SendLetter. SendLetter is a Yes/No field. What is the code to set SendLetter to Yes when the user selects MemberName? I...
2
1907
by: Sally | last post by:
In a simple multiselect listbox, what is the code to return an item's index when it is selected? Thanks! Sally
2
3334
by: Cassie Pennington | last post by:
I am trying to write various items from a multiselect list box to an SQL statement to update a report, without success. SQL only appears to accept hard-coded data or control values from a form, not...
1
1894
by: | last post by:
I am having trouble getting the procedure to use a Multiselect listbox in a report to work. I am using Access 2000. Here's the code. The error I get is QueryDef "User-defined type not...
2
7938
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
1
7117
by: Mike P | last post by:
How do I get the values selected from a multiselect listbox? Cheers, Mike *** Sent via Developersdex http://www.developersdex.com ***
2
1580
by: ttime | last post by:
I've got a form that uses a multiselect listbox. When a user is selected from a combo box, values are populated into this listbox associated with that user. The problem is, if one person has say...
3
3612
by: kaosyeti via AccessMonster.com | last post by:
hey... i have an unbound multiselect listbox on a form that i want to use to populate text boxes on that form. so if a user selects the 3rd item in a list of 20, how can i have that item show up...
5
4218
by: martin DH | last post by:
Hello, The details are below, but I have a simple form (Form1) with two objects and a "search" command button. When the two objects are cascading combo boxes (the form creates the parameters for a...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
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...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.