473,385 Members | 1,732 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,385 software developers and data experts.

Help: Query for List Box in Access

3
I have a list box in Access that I have set to allow mulitple choices. Wondering what I need to do to my query in order to pull back the data from the multiple fields. I want to be able to select multiple Lvl1_Organization records from the form. Previously it was a combo box and everything worked. Here is the query I used for the combo box.
Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl1_Organization]
Apr 24 '07 #1
6 1743
ADezii
8,834 Expert 8TB
I have a list box in Access that I have set to allow mulitple choices. Wondering what I need to do to my query in order to pull back the data from the multiple fields. I want to be able to select multiple Lvl1_Organization records from the form. Previously it was a combo box and everything worked. Here is the query I used for the combo box.
Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl1_Organization]
Are you referring to multiple OR Criteria based on multiple selections for [Lvl1_Organization]? If you are, you will need to build a SQL String from items within the ItemsSelected collection. If you don't know how to do this, please reply and either myself or one of the other Moderators/Experts will assist you.
Apr 24 '07 #2
RDMRDM
3
Are you referring to multiple OR Criteria based on multiple selections for [Lvl1_Organization]? If you are, you will need to build a SQL String from items within the ItemsSelected collection. If you don't know how to do this, please reply and either myself or one of the other Moderators/Experts will assist you.


I am selectinging multiple fields on the form. For example, I may want to return all the records for HR, Operations, Admin. I would select these records in the list box, and it would return those records.

Here is the current SQL that is listed for this field in the query.
WHERE (((tbl_Master_Data.[Lvl 1 Org Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl1_Organization]) AND ((tbl_Master_Data.[Lvl 2 Org Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl2_Organization]) AND ((tbl_Master_Data.[Job category Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Job_Category]) AND ((tbl_Master_Data.[Employee Group Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Emp_Group]) AND ((tbl_Master_Data.[Personnel Area Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Personnel_Area]) AND ((tbl_Master_Data.[Personnel Subarea Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Personnel_SubArea]))
ORDER BY tbl_Master_Data.[Lvl 1 Org Description], tbl_Master_Data.[Lvl 2 Org Description], tbl_Master_Data.[Job category Description], tbl_Master_Data.[Employee Group Text], tbl_Master_Data.[Personnel Area Text], tbl_Master_Data.[Personnel Subarea Text];
Apr 24 '07 #3
RDMRDM
3
I am selectinging multiple fields on the form. For example, I may want to return all the records for HR, Operations, Admin. I would select these records in the list box, and it would return those records.

Here is the current SQL that is listed for this field in the query.
WHERE (((tbl_Master_Data.[Lvl 1 Org Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl1_Organization]) AND ((tbl_Master_Data.[Lvl 2 Org Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Lvl2_Organization]) AND ((tbl_Master_Data.[Job category Description]) Like [Forms]![Form_Distribution_Selection_Criteria]![Job_Category]) AND ((tbl_Master_Data.[Employee Group Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Emp_Group]) AND ((tbl_Master_Data.[Personnel Area Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Personnel_Area]) AND ((tbl_Master_Data.[Personnel Subarea Text]) Like [Forms]![Form_Distribution_Selection_Criteria]![Personnel_SubArea]))
ORDER BY tbl_Master_Data.[Lvl 1 Org Description], tbl_Master_Data.[Lvl 2 Org Description], tbl_Master_Data.[Job category Description], tbl_Master_Data.[Employee Group Text], tbl_Master_Data.[Personnel Area Text], tbl_Master_Data.[Personnel Subarea Text];

Anyone have any suggestions on how to handle this "List Box"?
Apr 25 '07 #4
ADezii
8,834 Expert 8TB
Anyone have any suggestions on how to handle this "List Box"?
  1. Let's assume your List Box is named lstOrganizations. You can call it whatever you like.
  2. Set the Multi Select Property of this List Box to Extended.
  3. Place this code block wherever appropriate.
  4. NOTE: The code has been debugged and, in my opinion, is logically sound. It has not, however, been tested on actual data.
  5. My friend, NeoPa is the real SQL Expert. Perhaps he can give you better guidance or offer a better approach to your problem. I'm sure he'll be stopping by to visit.
  6. The intention of the code is to dynamically build the Criteria portion of an SQL String based on multiple selections within a List Box.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control, strSQL As String
  2. Dim varItm As Variant, strBaseSQL As String
  3. Dim strCompletedSQLString As String
  4.  
  5. Set ctl = Me!lstOrganizations
  6. strBaseSQL = "Select * From <tblYourTable> Where <tblYourTable>.[Organization] IN("
  7.  
  8. If ctl.ItemsSelected.Count > 0 Then
  9.   For Each varItm In ctl.ItemsSelected
  10.     strSQL = strSQL & Chr$(34) & ctl.ItemData(varItm) & Chr$(34) & ","
  11.   Next varItm
  12.  
  13.   strSQL = Left$(strSQL, Len(strSQL) - 1) & ")"
  14.   strCompletedSQLString = strBaseSQL & strSQL
  15.  
  16.   Debug.Print strCompletedSQLString
  17. End If
Typical OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Select * From <tblYourTable> Where <tblYourTable>.[Organization] IN("Org 2","Org 3","Org 5","Org 7","Org 10")
Apr 25 '07 #5
NeoPa
32,556 Expert Mod 16PB
That all seems pretty sound.
Two minor points :
  1. <tblYourTable> should probably take the form [tblYourTable] as it's possible that there may be spaces or other non-standard characters in a table name.
  2. I would always recommend the use of (') as a quote char in SQL rather than (") (Chr$(34) in your code) as it's only Access converting it that makes it work. ANSI compliant servers would not recognise the (").
Other than that, the logic is as sound as a pound :)
Apr 25 '07 #6
ADezii
8,834 Expert 8TB
That all seems pretty sound.
Two minor points :
  1. <tblYourTable> should probably take the form [tblYourTable] as it's possible that there may be spaces or other non-standard characters in a table name.
  2. I would always recommend the use of (') as a quote char in SQL rather than (") (Chr$(34) in your code) as it's only Access converting it that makes it work. ANSI compliant servers would not recognise the (").
Other than that, the logic is as sound as a pound :)
Thanks for the Input, Neo. Your opinion is always appreciated and valued.
Apr 25 '07 #7

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

Similar topics

1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.