Versions: 97 +We're all familiar with using a combo-box (also known as a drop-down list) to select data from a pre-defined or data-driven list of value. It's also quite common to use a combo-box to select a data value which can then be used in some context, for example selecting a value which is then used to filter a form or report. When you're using a combo-box in this way, it can often be good to add an "ALL" option at the top of the list, to show that you want everything... but how do you do this? Fortunately it's a piece of cake, but there are different methods depending on what the row-source of the combo-box is. Broadly speaking, there are four generic types of combo-box row-source:
DoCmd.OpenReport "rptSalesman", , , "RegionID = " & Me.cboRegionwhere Me.cboRegion represents the value of the combo-box. But to add an "ALL" option, we're going to have to change our thinking because what we're actually going to be adding is a "*" wildcard character option. So, in our subsequent use of the combo-box value, instead of using an Equals operator (=) we're going to have to think about using a LIKE operator. In other words, the previous example will have to change to something like DoCmd.OpenReport "rptSalesman", , , "RegionID LIKE " & Me.cboRegionThat's easy enough, isn't it? The important thing to remember is this works because the SQL WHERE constraint of "LIKE '*'" returns everything that isn't Null. Okay, enough theory - here are some examples of how to change each of the four combo-box row-source types to add that "ALL" option:
Okay, so you probably have a few questions now, such as: Q. Why does my combo-box now show an asterisk or a number, instead of my nice intuitive narrative text? Unless you tell it otherwise, the combo-box will still display the same number of columns, with the same widths, as before you made the changes outlined above. To restore the nice formatting, you need to do all the following:
Q. What's that "SortOrder" value all about? You want your "ALL" to appear at the top of your drop-down list, right? But what happens when sorting alphabetically doesn't put "ALL" at the top? For example, you might have a list of countries, in which you'd find that Albania came above "ALL" in your combo-box. By adding a simple numeric flag called SortOrder, this can be avoided: SortOrder has a value of 0 for your "ALL" option and 1 for everything else. SortOrder can then be used as the first item in your row-source SQL statement's ORDER BY clause, to ensure that "ALL" comes first - subsequent items can then be added to the ORDER BY clause to sort the remaining data items however you like. Q. Hows does it work again? Time to revisit the example. Let's say you have a combo-box called cboRegion on a form; previously it had a simple SELECT style row-source. You want to use this control to restrict the data included in a report - the report is opened by clicking a button on the same form. Previously, the button's OnClick event would have contained something like this: DoCmd.OpenReport "rptSalesman", , , "RegionID = " & Me.cboRegionIf you'd selected a RegionID of 1 from your combo-box, this would have parsed as DoCmd.OpenReport "rptSalesman", , , "RegionID = 1"Now you have something like: DoCmd.OpenReport "rptSalesman", , , "RegionID LIKE " & Me.cboRegionIf you select "ALL" from your combo-box, this will now parse as DoCmd.OpenReport "rptSalesman", , , "RegionID LIKE *"And, as we learnt earlier, "LIKE *" returns everything, and so gives you your "ALL" option. Hooray! Q. I still don't get it - can't I download an example database? Yes you can (I'm good to you, aren't I?). There's no difference in the code at all but, due to popular demand, I've created Access 97, 2000 and 2002 versions of a database to illustrate the example we've talked about. They have two tables (tblRegion and tblSalesman), one form (misleadingly entitled frmSalesman) and one report (rptSalesman). Take a look at the properties of cboRegion and the OnClick event of cmdRpt in frmSalesman - you can surely work the rest out for yourself! Download
You'll need unzipping software for these download, such as WinZip. |
Site requires Javascript and IFRAME support.Visitors:
|