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

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.

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.

Microsoft Access - Creating a single search function for multiple queries

I have a form where I would like to include a search function. Optimally this would include a combo box where the user can select their Search Field, i.e. Name, SSN, etc. and a text box to retrieve input.
There will be two tables I'd like the information to pulled from. So I assume I'll need two subforms, each with a query for that table. Is there a way to tie in the combo and text boxes to both of these queries at once?
My ultimate goal is to allow users to search for a record (an address), which may be in one of two tables, then select that record and have it appended to a third table which will be a separate subform.
This is basically my solution to relating addresses with people to reduce the amount of typing necessary.

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

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

Combo Box Column Count culls data?

Basically, I have a combo box that I'm using to select a record. That combo box then populates several textbox's that are found on my form based on the ID. See the two images:
.
I'd like the combo box to show the first two columns in my query, which consists of 16 fields. The Abbreviation and the Name, to be exact. However, I found that if I hid the fields in the query or if I limited the column count of the combo box so it only displays the two, it actually culled the data so that the textbox's would not find it (thus be blank).
My question is, how can I just show the first two columns of the combo box yet still use it's ID to reference all sixteen?
Query
SELECT Tradeshows.tradeShowAppreviation,
Tradeshows.tradeShowName, Tradeshows.tradeShowID,
Tradeshows.tradeShowWebsite, Tradeshows.tradeShowLocation,
Tradeshows.tradeShowDateStart, Tradeshows.tradeShowDateEnd,
Tradeshows.tradeShowBoothSize, Tradeshows.tradeShowShipDate,
Tradeshows.tradeShowAudience, Tradeshows.tradeShowFocus,
Tradeshows.tradeShowMailer, Tradeshows.tradeShowDoorDrop,
Tradeshows.tradeShowProductFocus, Tradeshows.tradeShowAttendees,
Tradeshows.tradeShowMembers, Tradeshows.tradeShowMemberships
FROM Tradeshows;
Do not change the column count, change the column widths.
Column width: 0cm;2cm;1cm;0cm
And so on.
it should not be difficult to only select the relevant columns. You could post the Row Source property of your combo.
It seems to me that you should be binding you table to the data source TradeShows and using the wizard to create a combobox that finds record.
You can do this by selecting the table Tradeshows and choosing either create form or form. You will now have a form with the RecordSource property set to TradeShows. Next, choose to add a combobox to the form, ensuring that you have selected the wizard (the magic wand button is highlighted). You will be given the option to "Find a record on my form based on the value I select in my combobox". You can choose this and step through the wizard. When you get to the step where you choose fields, make sure you choose the TradeShowID first, and then the name and / or abbreviation. If TradeShowID is the primary key, as it should be, the column will be automatically hidden in the next step, which will show the fields selected.
The result of these actions will be a combobox with the following properties:
Row Source : SELECT [Tradeshows].[tradeShowID], [Tradeshows].[tradeShowName],
[Tradeshows].[tradeShowAppreviation] FROM [Tradeshows];
Bound Column : 1
Column Count : 3
Column Widths : 0cm;2.54cm;2.54cm
You will also have [Embedded Macro] in the After Update event (2010 and possible 2007) or [Event Procedure] for earlier versions. The wizard code is pretty poor, but that is another days work.
If you still wish to do all the work of setting each control to the (possibly changing) column order, you can follow a similar lay out for the combo, setting counts and widths appropriately.

Access multi selection list box as a criteria to INSERT, UPDATE OR DELETE records from a table

For a database with this schema (a product may belong to one or more categories):
Item_category(product, category_name)
Category(category_name)
Will it be possible for me to build a multi-selection list box (List box data source from the Category relation) using an Access form and then highlighting the categories that the product belongs to (by querying the Item_category table), and at the same time letting users select new categories or deselect highlighted categories so that when an "Update" button is pressed, VBA code will automatically determine either INSERT, UPDATE or DELETE queries are needed to update the Item_category table?
How can I do that? Thanks!
I've generally done this type of interface using a form which looks very much like the "Which fields do you want on your form?" in the Form Wizard. Where you have the Table/Query list of Available Fields in the table in the listbox on the left hand side and the Selected Fields in the listbox on the right hand side. Along with the move left and right command buttons in the middle. The users quite like this interface.
Added: Note that the left hand listbox contains all the category records which aren't present in the products category table. So a category would be present in one or the other listbox but never both.
You very likely will want to use the Multi Select property of the listbox. If you hit the help on that field in the property sheet it should lead you to a page which mentions the ItemsSelected collection. Visit that page for sample code on how to read all the items selected by the user. Or visit http://msdn.microsoft.com/en-us/library/aa196172%28v=office.11%29.aspx.