How to create multiple connected search combo boxes to filter a form? - vba

I have a form with 10 columns, and for 5 of them (Project_Phase, Contract, Design_DPM, AMM/UCC, 1_or_2 stat) I want to add connected drop list combo boxes to filter the records and display data as selected in the combo boxes.
I know how to make multiple drop list combo boxes that filter the whole form based on the selection of one value from one column. For example, "Contract" combo box has options: signed, not signed. If I select "signed" it will display all the records that have "signed". and If I filter another column it will cancel the previous filter and display records relevant only to that selection from that column.
But what I want is the ability to filter using any number of filtering options from the 5 columns I mentioned. For example, if I want to see the records that are ("signed" under "Contract") and in ("proposal" under "Project_Phase") and ("a certain DPM" under "Design_DPM"). And after filtering I want to be able to clear the filters and see all the records again, as I am using this form to display all my records for users. I do not want it to be cascaded, as I might want to filter using only one column or more or all. And I do not want it to be a query or use the basic filtering in datasheet view.
Sorry for the lengthy explanation, if something is not clear I will explain further. Thank you for your efforts.

Here is one way to implement a form filter using multiple comboboxes.
Create a table named 'tblWildcard'; one field named 'Wildcard', save the table.
Enter one record with an asterisk as the value
Create the queries for your comboboxes like:
SELECT DISTINCT Table1.flda
FROM Table1
ORDER BY Table1.flda
UNION Select wildcard from tblwildcard
Save a query like the following SQL to be the Row Source for your form (i.e. qryFormA):
SELECT Table1.Flda, Table1.Fldb, Table1.Fldb
FROM Table1
WHERE (((Table1.Flda) Like [Forms]![frmForm1]![cboFlda])
AND ((Table1.Fldb) Like [Forms]![frmForm1]![cboFldb])
AND ((Table1.Fldc) Like [Forms]![frmForm1]![cboFldc]))
In the After Update event for each combobox add the following code:
Me.Recordsource = "qryFormA"
Sometimes the 'Me.Rowsource.' may not work when changes are made (weird issue!). If so, do the following:
Application.Echo False
Me.RecordSource = vbNullString
Me.RecordSource = "rowVwFilter25"
Application.Echo True
Finally, if a multi-user environment, and people may be adding records that will need to be included in your comboboxes, use the combobox Before Update event and add the following code:
Me.cboFldA.Requery

My question is answered. I found exactly what I wanted here link
Thanks for the help everyone

Related

Show a name instead of an id

I currently have these options set in a combo box:
This box will show the projectIDs that a current company has which is set with the criteria. I just want to show the name of the project as people will not know just by the id. If I remove the check box for the projectID it will just remove that field completly and not show anything. I have debugged what companyBox.Value is and it is indeed a number for a company and if taken this id and replaced it with the following query which is built from the picture and it returns the results I want. I just cant get the values to show in the combo box.
SELECT projects.projectName
FROM companys INNER JOIN projects ON companys.companyID = projects.companyID
WHERE (((companys.companyID)=7));
Gives me all the projectNames where the companyID is 7
And as seen below that same query just gives me blank spaces instead of the names:
Ok, if you have say a query, then as noted, then you can use a "join" to pull in the other table (based on that "projectID").
So, for a general report etc., then your approach of using the query you have is correct.
HOWEVER!!!
For a combo box? They have this feature and ability built in. In other words you do NOT in general need a join.
The combo box has two parts:
A sql query that "drives" or "fills" the combo box. This can (and will be) of course based on the Projects table.
VERY important:
Our combo box is to save, store, put, use the project ID into a column in our CURRENT form. That form of course is based on a differnt table (the forms current table). So, keep in mind the two concpets:
Combo box can be driven by any table to display data.
Combo box will/can use a column from that "other" table to SAVE into a current column on the current form.
Now of course the Project "id" is the VALUE we want, but we sure as don't want to display that "project id" value, since as you note, humans want the nice looking text.
First Rule:
ALWAYS, but ALWAYS make sure the FIRST column of the combo box "sql query" is the column we WANT to save into the current form, but ALSO the column we are going to HIDE AND NOT show to the user.
So, a combo box can with great ease HIDE the first column. You can hide other columns, but as a general rule a VERY HIGH number of my combo boxs will have two columns. So, you might need company "id", but would want to display company name as nice text to the user, but store/save/use the "id" of company for this purpose.
So, in your case? Change the order of your columns.
You want:
ProjectID, ProjectName, and you ALSO can continue to have a filter based on company.
So, once you get above setup, and you WILL NOT need a sql join. Remember, the combo box has it own "whole sql statement" based on table projects.
So, the combo box will:
Save/store/use the ProjectID when you select a project.
And it will display the project name, but behind the scenes it will use + save ProjectID.
So, just make sure that you set the length of the first column in the combo box to 0 (to hide that projectID from display).
Next, make sure you set WHICH column value from the query the combo box is to save.
That column will be the FIRST column, so you want to set that to 1
Your combo settings will look something like this:
In above, the CONTROL source is your CURRENT form and table.
So, I want to get a Hotel "ID" from the table hotels, but I am going to save the results of the combo box selection INTO a column called Hotel_ID
And note VERY careful - I set the bound column = 1 (that is the FIRST value from the query that drives the combo box.
Next up:
We want to hide the first column value, so you need this setting in format tab of the property sheet for the combo box:
NOTE VERY close in above. I set the FIRST column width = 0. this is HOW you hide the "ID" of hotels - I only want the user to see the nice hotel name. (or in your case Project name).
But, the 2nd column, I have a width for the Hotel name (or in your case project name).
So, make your first column in the query for the combo box the "id" of project id. Set the width of the first column = 0.
So, the query that drives the combo box? It is based on the ONE table, but you need to ensure that the "id" that you going to use is the first column, and simple hide it from display.
So, a combo box has a sql query that drives the combo box, and that query in most cases will NOT be the current table.
So, you have to set BOTH settings (the column the combo sql query will use - but to avoid confusing, then just adopt the habit of making the 1st column the "id" or value you want from that sql.
But, after setting above, you STILL have to set what column to shove/put that column into on the current form. (that setting is the control source).
In effect, a combo box is a kind of look-up into the other table, and it can display nice user friendly text columns, but still use + store the ID.
So, your query should look more like this:
SELECT ID, projectName
FROM projects
WHERE (((companys.companyID)=7));
I don't know if your first column of Projects table is "ID" (or is it ProjectID), but as you can see, the combo box ONLY needs to be based on the one table, and with our new rule - we always use the first column of the query the "id" value we want.
Now, of course the above is "hard coded" for company, and your original query that drives the combo box was fine - just that the order of the columns display was incorrect.

ms-Access Not using combo box display values as the lookup value

NOTE
I significantly edited my original post to include more relevant information and removed insignificant information. Some of the comments are based on the information removed, but the integrity of the original question is still intact and the comments are still relevant and useful.
In my database I have a table tblStandards that has a list of compound names with their associated fields.
I created a form to generate a subform which only populates compound names and their data when you select a solution mixture (I called these "handles"). I am using this subform as a means of generating a "Location Finder" of all the respective compounds associated with that solution mixture "handle". The issue is some compounds are used in multiple different mixtures, thus having multiple "handles". I have included a cascading combo box as recommended by #June7, but I am running into an issue with the last combo box filter.
I don’t want to have a separate field attached to my tblStandards with the combo box display value ("handle") just to filter my table (which is what I have done in the past). Using combo box display value field in my table works but it is limited and I wish to make it more modular. I have a second table tblComboBox which has three fields; [Compound Name], [Standard_InternalStd], and [Cal_QC_Handle].
I have the cascading combo box working, except for last filter. I think I found a round-about way to filter based on my tables. I am using an After_Update event with the FilterOn function to filter a TempSubform which is filtering the tblComboBox based on matching the [Cal_QC_Handle] to the cboxSecondChoice, then using TempSubform![Compound Names] field as a second FilterOn function for my MainSubform.
My code is working, but the second FilterOn is only using the first Compound Name from my TempSubform to filter MainSubform. I want to filter based on all the rows in my Tempsubform.[Compound Name]. I think my issue is in my " & Me.Temsubform![Compound Name] &" block of code.
Private Sub cboxSecondChoice_AfterUpdate()
Dim Filter_Tempsubform As String
Filter_Tempsubform = "[Cal_QC_Handle] ='" & Me.cboxSecondChoice & "'"
Forms![Standards_Form]![TempSubform].Form.FilterOn = False
Forms![Standards_Form]![TempSubform].Form.Filter = Filter_Tempsubform
Forms![Standards_Form]![TempSubform].Form.FilterOn = True
Dim Filter_MainSubform As String
Filter_MainSubform = "[Compound Name] = '" & Me.TempSubform![Compound Name] & "'"
Forms![Standards_Form]![MainSubform].Form.FilterOn = False
Forms![Standards_Form]![MainSubform].Form.Filter = Filter_MainSubform
Forms![Standards_Form]![MainSubform].Form.FilterOn = True
End Sub
The issue is the filter is only using one Compound Name (the first on the list) to filter. I need the filter to use all the Compound Names from the newly filtered TempSubform. Apologies if this is a very convoluted approach. Every example that I have seen so far always has the combo box value embedded in the table that they are filtering, which is something I wish to avoid.
UPDATE
I have abandoned my 1st attempt (the double FilterOn function with a temporary Subform) and I have a working form! I had to modify my tblStandards to include a new field which contained values from my final combobox selection (which I wanted to circumvent initially due to my perceived limitations of this design). The new Form-Handle field on my table is a string with multiple single "handles" which I use if the final combobox selection (only a single "handle" can be selected) is in the subform using the wildcard "Like *". Each one of these "handles" corresponds to a solution mixture which is a specific list of compounds.
This solution allows me to have a single compound used to make multiple different solutions, and if you select any of these solutions to make from the dropdown combo box the compound will be returned. I think this is the simplest solution to my issue and I might have just been too stubborn looking for a over-complicated solution.
I modified code from Allen Browne's ms-Access "Filter a Form on a Field in a Subform"
Relationships (All)
Form with cascading comboboxes and filter button
Working code
As #June7 said, cascading ComboBoxes (or ListBoxes) are the standard UI design for multistage filtering of data tables. If I have the available form real estate, I prefer cascading ListBoxes over ComboBoxes, because of the visual cues they provide. For example, look at how Digi-Key does it. Problems with cascading ComboBoxes have been questioned to death here. Boxes which filter data, list or combo, are usually unbound, and should not be simultaneously used to update data. Their sole purpose is to display item choices, select an item, and pass it's value to the next filter.
I'm linking below to a few tutorials, which require little to no VBA code. The 3rd tutorial filters "plants and animals, categorized by their taxonomic rank", which is a similar case to filtering chemical compounds. The 4th is one of my previous answers to a similar question.
Microsoft Access: How to Create Cascading Combo Boxes
Access 2016 - How to Make a City/State Selector (Cascading Combo
Boxes)
Creating Cascading Combo Boxes and List Boxes on Microsoft Access
Forms
Creating Cascading Comboxes in VBA code using RowSource

Filtering columns presented as comboboxes in a split form

I have a split form based on a table (tblMain). The form shows many columns some of which are textboxes and others are comboboxes. I can easily sort and filter every column in a datasheet part of the form except of two columns.
These two columns are comboboxes which have the same control source and same row source table however with the following difference:
cboPUNUM:
control source tblMain.[PU_ID];
row source SELECT tblPU.[PU_ID], tblPU.PUNUM FROM tblPU;
cboPUTYPE:
control source tblMain.[PU_ID];
row source SELECT tblPU.[PU_ID], tblPU.PUTYPE FROM tblPU;
Table tblPU has the following structure:
PU_ID PUNUM PUTYPE
1 11234 SMC
2 21234 DPL
3 11234 NEC
4 21123 SMC
and so on...
The idea is to have two separate columns for PUNUM and PUTYPE in the split form and be able to filter them.
Both columns in the split form show all available values in checkboxes for filtering in the drop-down list on top of columns until the filter is applied:
The problem is that I can filter only one of these columns (see the picture below). For example if I select 84174 in the PUNUM field then I lose an ability to filter PUTYPE field. Ideally PUTYPE should give me an option to check FHP or ADH but checkboxes don't appear in a drop-down list.
And vise versa, if I start from filtering the PUTYPE, i.e. I select FHP and expect the PUNUM to have many options but it doesn't show checkboxes for filtering (see below).
Please advise what I can do with this as I have no ideas...?
P.S. DLookup function instead of comboboxes doesn't allow to filter columns at all.
I know that I can base the form on a query and save DLookup expressions in that query, but it significantly slows down the performance.
Ok, Looks like I was over thinking the issue...
I just changed the query for the split form and included tblPU with those two fields. And then converted form controls to text boxes and sourced them to that fields directly. Works as I wanted.

MS Access How to make a field in form invisible if it is empty

I am trying to make a filed in my form become invisible if it's value is empty.
What I have is a form with individual records from a table, this has a sub-form which is linked by an ID, and it displays the results of a query.
This has three fields ones is the ID, a number and third is a date, Not all records have a date and therefore for the ones that do not have one I want to completely hide the date label and the empty text box; and make it appear only when a date is specified.
I'm guessing I might have to use some kind of WHILE statement in my query but I'm not sure whether that's right and how to do it.
I would appreciate your advice on this.
Thanks
You can add this code to the subform current event:
If (datafieldname & "") = "" then
datafieldname.visible=false
else
datafieldname.visible=true
End if
Where datafieldname is the name of the data field on the subform. This will only work if your subform layout is not tableview

Populate combo box with distinct values from one table based on values existing in another?

I am trying to populate a combo box with distinct values pulled from a clients table, but only if those clients exist in the mfgOrders table. I have set the Row Source to the following query:
SELECT DISTINCT Client.ClientName FROM Client
INNER JOIN mfgOrders ON Client.id=mfgOrders.client;
When I switch to Datasheet view, I can see all the distinct clients that are also in the mfgOrders table. The problem is that this does not populate in the combo box.
I originally thought that this may be due to the data size, but oddly enough when I select ClientName from Client, it will populate the box successfully but with ALL clients.
Unfortunately, I don't want all clients in the combo box. Just the clients which also appear in the mfgOrders table.
I'm stumped on this one. Is my logic incorrect?
It appears that when linking a drop-down list to a query, the list will be populated based on how many fields are queried.
In this case, despite the first field being used simply to create the union, Access created a combo box with 2 columns, the first of which had a width of 0".
This appears to have been done because the "show" box for the first field in the query was unchecked. The result was a blank list.
To fix the error I simply re-arranged the query.