Is there a way to use an MS Access multiselect listbox control to filter records WITHOUT using VBA or iterating through the selected values? - ms-access

I think the question speaks for itself, but I'll use a trivial example. Consider a table of salespeople as shown below:
Now consider a simple search/filter form with a list box where the user selects a region and it lists the salespeople in that region:
This is trivial. Assuming the list box is named lstRegionFilter, the query used to build the subform is simply:
select
[tblSalespeople].[SalespersonName] as [Salesperson Name],
[tblSalespeople].[Region] as [Region]
from
tblSalespeople
where
[tblSalespeople].[Region] = [forms]![frmSearchForm]![listRegionFilter];
Now consider the list box is changed to a multiselection list box, so that the user can select multiple regions with the filter working accordingly, as follows:
While this can be done, every solution I've found involves a VBA procedure that iterates over the list box's selected items, constructs a comma-separated string, and feeds this string to the result query. This strikes me as being more work than ought to be necessary, and I figure I'm missing a simpler "built-in" solution, that ought to look something like this:
... where [tblSalespeople].[Region] in ([forms]![frmSearchForm]![listRegionFilter]);
or perhaps
... where [tblSalespeople].[Region] in (select * from [forms]![frmSearchForm]![listRegionFilter]);
The reason I figure there must be a simpler solution is because using a multi-selection list box to filter records would seem to be the most obvious purpose for the very existence of the multi-selection list box control in the first place. As such, it intuitively seems that there would need to be a built-in way of extracting the list box's selected values directly as a record-set-style object that would be natively understood by an Access query.
So, to make a long story short, my question is this:
Is there a simple built-in way to do this without writing code to iterate over the list box's selected items?

Related

Microsoft Report Builder: How to display selected chosen values from a multi-value parameter in a table?

I'm using MS Report Builder v3.0 to create a report. As part of this report, I have a multi-valued parameter (named #Diagnoses). The labels and some of the values (truncated by the size of the display boxes) of this parameter are shown below:
What I would like to be able to do is display the labels / values the user chooses in a 2-column tablix (I've tried using separate textboxes for the labels / values but the results are mis-aligned).
However, this does not appear to be straightforward. The closest method I've found is this one, which stores the user's choices in an internal parameter in xml format, then queries this parameter to produce a dataset from the xml.
So, I created the xml-producing internal parameter like this:
...and I've created a dataset based on this data, with the following query:
But now when I put these values into a tablix, the labels and values are now on separate rows, like this:
Does anyone have a straightforward way of sorting this out?
Best Wishes
C J
OK - I've solved it (it's not pretty, but it works!)
Basically, the problem comes from having to use the join statement when creating the xml - you can't really put both the label information and the value information on the same row in the xml.
To get round this, you have to use two internal parameters to create two datasets - one for the labels column, one for the values column. The method for creating these is essentially the same as that shown above, except for the values parameter, the expression for the default value is:
...and for the labels parameter, it's this:
(by the way, make sure you set the available values to "None" for internal / hidden parameters - the expressions here are for the default values)
Then, when you create the values dataset, you use the following syntax in its query:
...and similarly for the labels dataset.
Finally, in the report, you create one matrix from each of the two datasets, then put them next to each other, using fixed row heights and setting "can shrink" / "can grow" to false so that they look like they're in the same table:
I hope this helps somebody!

Now you're thinking with parameters; Having a NOT list as a parameter option (report builder 3.0)

I have been super impressed with the way you guys think about parameters in SSRS. You can make them do all sorts of interesting things. I have a report where I want two parameter options reflected in my SQL query, option one is as list of numbers for a certain field. This is easily done via
WHERE [DDI] in #DDI
Setting up the parameter DDI to include the full list of numbers. The point I'm struggling is with the counter case. In essence I need the query to become
WHERE [DDI] not in #DDI
If the second option is picked. I think the best way to solve this is with nested parameters but I can't think with portals sorry parameters in the correct way to figure this out.
TLDR : I want a parameter solution where user can pick between 2 options the first gives them a curated list in a particular field and the second gives everything except that curated list.
You could create a drop down parameter with Include (YES), Exclude (NO) wording. Then in your stored procedure it could filter based on the value passed
WHERE (( #Include = 'YES' AND [DDI] in #DDI) OR (#Include = 'NO' AND [DDI] NOT IN #DDI))
``

Populating listbox from another listbox selection

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Access: Fill a combobox with selections from another field

I'm just learning how to use Access and while I've managed to muddle my way through most of what I'm trying to do, there's something I haven't been able to figure out how to do yet.
I have two forms and corresponding tables. In frmProducts is ColorOptions, a multi-select combobox containing a list of possible color options for a product, and Design, a text control for the name. In frmCustomers is OrderDesign, a combobox with a list of items from tblProducts, and OrderColours, a combobox.
Now, this is the problem: I want OrderColours to display list of the color options in tblProducts, but I can't figure out how. I can get it to display the value, but it's not a list of items, just one entry with the 'list' (e.g. a single entry reading "Brown,Red,Green"). I want the user to be able to select a single item from that subset.
Ideally I'd like to do this without messing with VBA or any advanced SQL, but if that's not possible then that's fine as well. I think the issue may be that the Colours field which contains the colours for that product is stored as text, but I'm not sure how else to store it as there's no 'array' or 'list' option for datatypes.
Sorry if I haven't been clear enough, or if this is posted in the wrong sub. I'm a beginner in Access, so I may have not been clear enough or used the wrong terminology. Any help would be much appreciated.
I'm not quite sure I understand exactly how you want this set up, so I'm assuming the following. Please correct me if this is not right:
tblProducts contains (at least) the two fields productDesign and productColour
It is possible for there to be multiple records in tblProducts with the same productDesign but different productColour (different colours of the same design)
There is another table, tblCustomers, in which each record contains a productDesign and one of the corresponding productColours.
So you need the combobox OrderColours to display a list of the possible productColours for the selected value of productDesign in OrderProducts.
Now, set up combobox OrderDesign as follows:
Row Source Type: Table/Query
Row Source: SELECT DISTINCT tblProducts.productDesign FROM tblProducts;
and OrderColour:
Row Source Type: Table/Query
Row Source: SELECT tblProducts.productColour, tblProducts.productDesign FROM tblProducts WHERE (((tblProducts.productDesign)=[Forms]![frmCustomers]![OrderDesign]));
Column Count: 1
and give OrderDesign the following event AfterUpdate:
Private Sub OrderDesign_AfterUpdate()
Me.OrderColour = Null
Me.OrderColour.Requery
Me.OrderColour = Me.OrderColour.ItemData(0)
End Sub
You may well also need to consider what happens when moving between records, if your comboboxes are bound:
Private Sub Form_Current()
Me.OrderColour.Requery
End Sub
in the Form_Current event should do the trick.
Read this for details.

Magento - change multiselect semantics from "or" to "and"

When more than one value of a multiselect is used as a filter on a catalog or catalogsearch page in Magento, the multiple values are or'd together - i.e. the result is the union of the products that have any of these attributes.
How would I get the intersection of the sets of products with the selected attributes - i.e. only those products that have all of the selected attributes?
In the standard behavior of Magento you can only filter for one value like Lucasmus already indicated.
So you seem to be using some customization or module which edits the behavior of the core/Mage/Catalog/Model/Layer/Filter/Attribute.php model.
Also the class Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute has to be rewritten, this is exactly the class where you could change the behavior you asked for.
In that class, the method
applyFilterToCollection($filter,$value)
creates the corresponding SQL code to do the filtering.
Currently your module or extension probably uses an OR notation, or uses something like
$connection->quoteInto("{$tableAlias}.value IN (?)", $value),
so it searches for any of your to be filtered values.
To achieve an AND, you will have to rewrite this method by splitting the $value into its distinct parts and using a where clause for each of the individual values.
Hope that makes sense.