Trying to populate two comboboxes based upon selection of another combobox - ms-access

I've been searching this issue on Microsoft, Stackoverflow, techinthenet.com and several others, with no real solution that I can find.
I am trying to get two comboboxes (cboCourse and cboVols) to populate based on the selection of cboTrainee_Name. The code for the first combobox is:
SELECT DISTINCT [qryBooks].[PName] FROM qryBooks WHERE qryBooks.Complete=No ORDER BY [qryBooks].[PName];
Everywhere I've searched gives assistance and samples to populate the first combobox, then the second combobox based upon what is selected in the first. I need both cboCourse and cboVols to populate based on the result of the same query. What I have so far is:
Private Sub cboTrainee_Name_AfterUpdate()
cboCourse.RowSource = "SELECT DISTINCT [qryBooks].[Book] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Book];"
cboVol.RowSource = "SELECT DISTINCT [qryBooks].[Vols] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Vols];"
End Sub
This works for cboCourse, but cboVols will not populate. I know there has to be a simple explanation. Any suggestions?
ADDED! I used SELECT DISTINCT because each column in the query contains multiple instances of the same data.

I don't see any problems with your SQL.
You might need to call the Requery method on both combo boxes to get them to refresh after cboTrainee_Name is changed:
cboCourse.Requery
cboVol.Requery
Note that you don't have to set the RowSource property in code for this to work. You should be able to plug in your SQL statements as is in the RowSource property and just the Requery method on the dependent combo boxes in the AfterUpdate event of your cboTrainee_Name combo box.

Related

Can't select combobox entries after applying filter to column

I have a column full of comboboxes. The comboboxes work fine on their own.
But if I right click on the column and apply a filter, e.g. sort alphabetically, the comboboxes still show with the correct entries, but I can no longer select another entry in the box. E.g. the box says "supplier", but I want to select "customer". Clicking on "customer" simply does nothing.
When I apply the filter, apparently the entire formula "refreshes" itself. Therefore I am currently trying to add some code to
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer).
However, being rather new to MS Access, I have no idea whether this is a step in the right direction and if so, what code I should have in Form_ApplyFilter in order to make my comboboxes clickable/updateable again.
The properties of the comboboxes are:
Row source type = Table/Query
Row source = SELECT Tbl_Liability.ID, Tbl_Liability.Liability FROM Tbl_Liability;
Bound column = 1
Enabled = Yes
Locked = No
And the form the comboboxes are in has ALLOW FILTERS set to YES (but no code in Form_ApplyFilter).

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).

Multiple Values for a field (Link Master fields)

I am trying to use Link Master Fields to get a value from my form in order to filter some of my data and then show the remaining on my chart. Everything works perfectly fine but when I want to select more than one value (using a listbox to select multiple values for a field), my chart returns nothing.
I would like to know how can I address multiple values for a field in my chart so it filters the values on my chart as an "Or" function in SQL.
Imagine I want to have an SQL with: WHERE [MyTable].[Field1]= Selected values in listbox.
It seems that my SQL selects all the values in the listbox as One Value/And function.
You will need to do this via VBA.
Use the Change event on the list box to update the subforms recordsource.
Also make sure you execute the code in the forms Current event, so that it will display correctly when moving records, and on initial opening.
So with a bit of research and trying out different ways, an easy way is to use some hidden textboxes in the form and loop through the selected values and give each textbox one of the selected values in the listbox.
From there you can write a SQL for your chart specifying the condition so your field = Condition 1 or field = Condition 2.

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.

Access IIf statement retrieving a value using a Check Box

I am attempting to have a single List Box pull up two possible values based on whether or not a Check Box is filled. The Row Source I have for the List Box is:
IIf([Forms]![frmPDPRate]![chkTrue], [qryPDPRate]![Initial], [qryPDPRate]![Renewal])
The specifics are that I'm trying to pull a rate, either Initial or Renewal, based on if [chkTrue] is true or false.
Another note: The query that the list box should be pulling from has been paired down to one row based on cascading combo boxes. So there is only one possible choice per column in the query.
EDIT:
I think I'm going about this all wrong but I don't know how to get it to do what I want at this point.
Yawar, nothing is happening when I run the form but that makes sense as I can't force the rowsource to choose between two possibilities.
I think you need the following code in VBA:
Private Sub chkTrue_AfterUpdate()
If chkTrue =TRUE then
listbox.RowSource = "SELECT Initial FROM qryPDRRate"
Else
listbox.RowSource = "SELECT Renewal FROM qryPDRRate"
End If
End Sub
I was able to use the Query Builder in the RowSource Property using an IIf statement. The RowSource Data Looks as such.
SELECT IIf([Forms]![frmPDPRate]![chkTrue],[qryPDPRate]![Initial],[qryPDPRate]![Renewal]) AS Rate FROM qryPDPRate;