473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Getting the results for all values in a combo box

Seth Schrock
2,965 Expert 2GB
Introduction

A very common function that databases perform is the ability to make a selection from a combo box/list box and open a report or form based on that selection. For example, you could have a combo box that lists all the companies in your database. You choose one and find all the service requests that the chosen company has outstanding. This is very simple to do. But what if you wanted to find all outstanding service requests for ALL the companies? I have done this a few times, but I have found a new way that I think is much cleaner.

Step 1: Getting the ALL option in the combo box

In the past, the table behind my combo box couldn't have its primary key field be an auto number because I wanted the ALL value to be number 0 and auto number doesn't support that. I would then have to filter the ALL record out whenever it was used so that ALL couldn't be related to a record. Instead I now use a UNION query to append an ALL record to the row source of the search combo box, like this
Expand|Select|Wrap|Line Numbers
  1. SELECT CompanyID, Company
  2. FROM tblCompanies
  3. UNION
  4. SELECT 0, "--ALL--"
  5. FROM tblCompanies
This includes all the companies from my Companies table (tblCompanies) and tacks on the --ALL-- record so that the user can search for all companies.


Step 2: Setting up the query to find all values when ALL is selected

I prefer to base my reports and forms on querydefs as I think that they are easier to troubleshoot if the expected records are being found or unexpected records are being found, so my explanation of this step will refer to queries. However, this would work in any WHERE clause or criteria (ie. DoCmd.OpenReport...Criteria:=...)
So continuing with our example of finding records for the specified company or ALL companies, let's assume my combo box is called MyComboBox and is in a form named MyForm. The field that is being searched in is named CompanyID_fk and it is the foreign key in the one-to-many relationship between the Companies table and the Service Requests table. So the WHERE clause of the query would need the following criteria added to it
Expand|Select|Wrap|Line Numbers
  1. WHERE Forms!MyForm!MyComboBox In (0, CompanyID_fk)
Conclusion

This solution provides a way to easily find records that match all values in the combo box in a very clean and efficient way. By doing it this way, the primary key field in the Companies table can stay an Auto Number field and the --ALL-- value is only available when it is time to pull the report. It can also be added very easily after the database has been used for quite a while with no changes to the table design.
Mar 12 '13 #1
5 7580
zmbd
5,501 Expert Mod 4TB
Usually, I use the Union Query in the reverse order you have it to prepend a "blank" record to the row source.

Or, you can follow the article:
ACC2000: How to Include a Null Selection in a Combo Box List

The alternative is to have the value in the union (which is very similar to what is offered in OP):
Adding an (All) item to an Unbound Combo Box and have the sort set.

Then there's the vba method (note, about 1/2 way down, the union query option is offered as in the above link. I personally prefer the union query option):
How to add '(all)' to a combo box or a list box control in an Access 2000 application

and a few other methods.
Mar 12 '13 #2
Seth Schrock
2,965 Expert 2GB
In my implementation of this, I have an ORDER BY clause which puts the --ALL-- at the beginning of the alphabet. I left it out so that people could choose their own sorting. However, I didn't think about it that I could just switch the two sides of the UNION and put it on the top that way. Thats a good idea Z.

I'm glad that I don't have to use the VBA method to include the --ALL-- in the combo box. That is a bunch of code to do something simple. Especially when all you need to do is use the UNION query.
Mar 12 '13 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Its a nice way to avoid using VBA, as VBA used in queries can be a performance hit (although usually quite negligible), and also less maintenance, since there is no VBA)
Mar 13 '13 #4
Pierkes
64 64KB
Hi Seth Schrock,

It tried your code and the combobox now lists;

empty space
-All-
2013

However, on thr criteria part of my query i still have problems;

- at the start of the form "frm_trajecten", which is a split form where you can check a few comboboaxes, i only see the records where [tr_prod_jaar] is filled with a number. I would like all the records to be visible.

All comboboxes, but [cmb_prodjr] are comboboxes that are filled using the table "tbl_trajecten" and a specific table with for examples the names of accountmanagers "tbl_am" with the field [am_name]. When i use such a combobox i have, in the criteria part of the query a "like..." statement to have the query list all records of this particular accountmanager. This works perfectly using the code;

Expand|Select|Wrap|Line Numbers
  1. requery
on a button. The "split" part of the form is then populated with the right data.

However, with the new combo [cmb_prodjr] it does not work this way apparantly.

The rowsource for the combobox is now;
Expand|Select|Wrap|Line Numbers
  1. SELECT tr_prod_jaar  FROM Q_pijplijn_bron UNION SELECT "-All-"  FROM Q_pijplijn_bron;
  2.  
where;
[tr_prod_jaar] is the field in the query where the year is (or empty)
"Q_pijplijn_bron" is the queryname

Can you help me what to put in the criteria field so the records are sorted on the value of the combobox (or, if user chooses "-all-", it will list all recordds?

Thanks,
Pierre
Apr 16 '13 #5
Seth Schrock
2,965 Expert 2GB
You say you are using the "Like...". Did you try the "IN..."? I think that in your case it would be
Expand|Select|Wrap|Line Numbers
  1. WHERE cmd_prodjr IN (0, tr_prod_jaar)
Apr 16 '13 #6

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

Similar topics

2
by: Tripharn Teki | last post by:
Hi, I have the following code in the HTML <select size="1" name="newtitle"> <% // Need to fill select with available campuses while (titleit.hasNext()) { temp = (String)titleit.next(); //...
2
by: gregpinero | last post by:
Hey everyone, I'm trying to call a system command "svnlook log \arms" from within python and process the results. However I'm having trouble getting the results of the command back into python....
0
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, Could someone please help me with an issue I am having. I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a...
10
by: David C | last post by:
I have the following line of code in my asp page Dim ix For ix = 1 to Request.Form.Count fieldName = Request.Form.Key(ix) fieldValue = Request.Form.Item(ix) Response.Write fieldName...
4
by: s99999999s2003 | last post by:
hi the database "execute" function returns a list of logical results. Each logical result is a list of row tuples, as explained in the documents. everytime i use it to execute various...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
0
by: mrwoopey | last post by:
I did not see code for getting all values from a repeater control posted anywhere. So, I figured it out and I am posting for the next person that may need it (sorry if this seems obvious to you): ...
0
by: Anil Kumar Lakky Reddy | last post by:
I have seen lots of people complaining about not getting updated values from edit columns of datagrid. I had similar problem, I figured I was refreshing the data on each page load. I fixed it by...
1
by: Mitesh | last post by:
Hi, I have the following HTML code right now. Code: ------------------------------------------------------------------------------------------------------------------------- <td...
2
damonreid
by: damonreid | last post by:
Access 2003 Windows XP Pro Just wondering if there is a quick way to select unique values from a table for a search or for a Combo Box on a form?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
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...
0
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...

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.