Relational Combo Boxes not Working (1 Viewer)

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
Good Morning,

I would really appreciate assistance as I have been trying for over 2 weeks to get the relational combo boxes to work.

I am trying to set up a Material Requisition DB. In the details section of the subform I tried to create a category combo box, which when a selection is made, the Product combo box would requery and show only those items under this category.

However, I have tried several methods and it is just not working. I have attached the database as I am at my wits-end and really need to get this DB set up.

You will see that I have copies of the tblMaterialRequisition and tblMaterialRequisitionDtls as I was trying several times to get the combos to work.

Thanks in advance for any assistance/advice provided.
 

moke123

AWF VIP
Local time
Yesterday, 19:48
Joined
Jan 11, 2013
Messages
3,920
your cboCategories should have

Code:
Private Sub cboCategories_AfterUpdate()
Me.cboProduct.Requery
End Sub

the rowsource of cboProducts should be
Code:
SELECT tblProducts.ProductName
FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID
WHERE (((tblCategories.CategoryID)=[Forms]![frmMaterialRequisitionDtls]![cboCategories]));
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:48
Joined
Feb 19, 2002
Messages
43,293
The simplest method requires only one line of code. The query for the child combo, uses a where clause that references the parent combo.

Select ... From ... Where SomeField = Forms!yourform!cboParent

Then in the click event of the parent combo, you requery the child combo.

Me.cboChild.Requery.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:48
Joined
Feb 19, 2013
Messages
16,616
that method works in a single form, but not in a continuous or datasheet form

for a continuous form the code is a bit more complicated - see this link

 

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
I don't understand why but it's just not working. This time the Enter Parameter Value keeps popping up everytime I open the form
 

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
I'm starting to think there is a problem with my Access program itself. Another database I have been using for over a year now, started giving trouble after an update in April.
 

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
If I open the subform frmMaterialRequisitionDtls alone, the combo boxes work. But when I open the frmMaterialRequisitions, the combo boxes do not work
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:48
Joined
May 21, 2018
Messages
8,529
You cannot reference a subform by its name. You have to reference it through its parent. The reason is a subform can be opened many times.
I would do it in code, but you could try
[Forms]![YourMainFormName]![TheNameOfTheSubformControl].[Form]![cboCategories]
However this is problematic because of you cannot test the subform by itself

I would change the sql in code. Way easier and you have to return it to unfiltered anyways once you exit.

Code:
Private Sub cboProduct_Enter()
  Dim strSql As String
  strSql = "SELECT tblProducts.ProductName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID WHERE "
  strSql = strSql & "tblCategories.CategoryID = " & Me.cboCategories
  cboProduct.RowSource = strSql
End Sub

Private Sub cboProduct_Exit(Cancel As Integer)
  Dim strSql As String
  strSql = "SELECT tblProducts.ProductName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID"
  cboProduct.RowSource = strSql
End Sub
 

tvanstiphout

Active member
Local time
Yesterday, 16:48
Joined
Jan 22, 2016
Messages
222
For a subform in Continuous Forms mode, I would refer you to the Northwind 2.0 Developer Edition (File > New). Cascading comboboxes is implemented in the Order Details subform, and the help page has documentation about the technique that was used.
 

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
For a subform in Continuous Forms mode, I would refer you to the Northwind 2.0 Developer Edition (File > New). Cascading comboboxes is implemented in the Order Details subform, and the help page has documentation about the technique that was used.
Hi. I did. Still didn't work.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:48
Joined
May 21, 2018
Messages
8,529
Did you try my code. Works for me.
 

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
You cannot reference a subform by its name. You have to reference it through its parent. The reason is a subform can be opened many times.
I would do it in code, but you could try
[Forms]![YourMainFormName]![TheNameOfTheSubformControl].[Form]![cboCategories]
However this is problematic because of you cannot test the subform by itself

I would change the sql in code. Way easier and you have to return it to unfiltered anyways once you exit.

Code:
Private Sub cboProduct_Enter()
  Dim strSql As String
  strSql = "SELECT tblProducts.ProductName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID WHERE "
  strSql = strSql & "tblCategories.CategoryID = " & Me.cboCategories
  cboProduct.RowSource = strSql
End Sub

Private Sub cboProduct_Exit(Cancel As Integer)
  Dim strSql As String
  strSql = "SELECT tblProducts.ProductName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID"
  cboProduct.RowSource = strSql
End Sub
This works...thank you so much
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:48
Joined
May 21, 2018
Messages
8,529
In a continuous form you want to apply the filter when you Enter the second combo and take the filter off when you Exit. If not what happens is in a continuous form all controls get the same rowsource, That means if you do not remove the filter when you exit all combos get repainted using the current filtered rowsource. Anything that is not included in the list will not display. So some combos will appear empty when in fact they are not.
 

bob fitz

AWF VIP
Local time
Today, 00:48
Joined
May 23, 2011
Messages
4,727
The Row Source property of the Product combo should be:
Code:
SELECT tblProducts.ProductName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryID = tblProducts.CategoryID WHERE (((tblCategories.CategoryID)=[Forms]![frmMaterialRequisition]![frmMaterialRequisitionDtls].[Form]![cboCategories]));

The following code should be in the GotFocus event of the Product combo:
Code:
Me.ActiveControl.Requery
 

June7

AWF VIP
Local time
Yesterday, 15:48
Joined
Mar 9, 2014
Messages
5,474
Why aren't you including ProductID in combobox RowSource? Why join to tblCategories?
When controls are on same form, don't need full form path reference for SQL statement in RowSource, instead of referencing a query object:
Code:
SELECT ProductID, ProductName FROM tblProducts WHERE CategoryID=[cboCategories];
 
Last edited:

Tophan

Registered User.
Local time
Yesterday, 19:48
Joined
Mar 27, 2011
Messages
367
Ok...so I'm still getting a couple problems.

I've tried both @MajP and @bob fitz methods and getting the same error message when I try to tab to a new record in the subform. A message box pops up which states You cannot add or change a record because a related record is required in table 'tblproducts'. Also, I noticed when i did select a product the same product shows in the line below.

I then tried @June7 method and I am getting the error message that I tried entering text in a numeric field.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:48
Joined
May 21, 2018
Messages
8,529
That is a problem with your form not saving a related record. Not related to the cmbo box issue.
 

June7

AWF VIP
Local time
Yesterday, 15:48
Joined
Mar 9, 2014
Messages
5,474
With my SQL, change combobox properties:

ColumnCount: 2
ColumnWidths: 0";1.0"

Your original RowSource was attempting to save product name to ProductID field. Need to save ProductID.
 

Users who are viewing this thread

Top Bottom