Sacha Walter
asked on
Update the rowsource property of a second control
I have a form with tabs. The first tab contains multiple bound ComboBoxes, I want to make the selection in one ComboBox limit the choices in another ComboBox. I am trying to update the RowSource property of the second ComboBox in the AfterUpdate event of the first ComboBox, but nothing is showing in the second ComboBox dropdown when I make a selection in the first ComboBox.
I'm not sure what I'm doing wrong with the following code (I don't get any errors, just nothing to select in the second combobox):
Private Sub cboStatus_Phase_1_AfterUpd ate()
' Set the second combo box to be limited by the first
Me.cboConfirmation_Phase_1 .RowSource = "SELECT t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1 " & _
"FROM t_0_MAIN_Downgrade_Trackin g, t_2_JUNCTION_WORKFLOW " & _
"WHERE Status_Phase_1 = me.cboStatus_Phase_1 " & _
"GROUP BY t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1;"
Thanks!
I'm not sure what I'm doing wrong with the following code (I don't get any errors, just nothing to select in the second combobox):
Private Sub cboStatus_Phase_1_AfterUpd
' Set the second combo box to be limited by the first
Me.cboConfirmation_Phase_1
"FROM t_0_MAIN_Downgrade_Trackin
"WHERE Status_Phase_1 = me.cboStatus_Phase_1 " & _
"GROUP BY t_2_JUNCTION_WORKFLOW.Conf
Thanks!
shouldn't need the requery. The act of changing the rowsource forces a requiry. Try:
strSQL = "SELECT t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1 " _
& "FROM t_0_MAIN_Downgrade_Trackin g, t_2_JUNCTION_WORKFLOW " _
& "WHERE Status_Phase_1 = " & me.cboStatus_Phase_1 & " " _
& "GROUP BY t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1;"
strSQL = "SELECT t_2_JUNCTION_WORKFLOW.Conf
& "FROM t_0_MAIN_Downgrade_Trackin
& "WHERE Status_Phase_1 = " & me.cboStatus_Phase_1 & " " _
& "GROUP BY t_2_JUNCTION_WORKFLOW.Conf
You need to concatenate the SQL with the value from the first combo box.
Try this if me.cboStatus_Phase_1 is numeric:
If me.cboStatus_Phase_1 is text then
Try this if me.cboStatus_Phase_1 is numeric:
Me.cboConfirmation_Phase_1.RowSource = "SELECT t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1 " & _
"FROM t_0_MAIN_Downgrade_Tracking, t_2_JUNCTION_WORKFLOW " & _
"WHERE Status_Phase_1 = " & me.cboStatus_Phase_1 & " & _
"GROUP BY t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1;"
Me.cboConfirmation_Phase_1.Requery
If me.cboStatus_Phase_1 is text then
Me.cboConfirmation_Phase_1.RowSource = "SELECT t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1 " & _
"FROM t_0_MAIN_Downgrade_Tracking, t_2_JUNCTION_WORKFLOW " & _
"WHERE Status_Phase_1 = """ & me.cboStatus_Phase_1 & """ & _
"GROUP BY t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1;"
Me.cboConfirmation_Phase_1.Requery
You had the reference to the first combo inside the quotes, so it was never actually seeing the value of that control.
Also, this query uses a Cartesian Join, which can involve a lot of results. Is there a field in common between your tables [t_0_MAIN_Downgrade_Tracki ng] and [t_2_Junction_Workflow] that you could use to join the two tables.
BTW, I usually build the SQL then apply it to the rowsource so I can use:
debug.print strSQL
to test the value that is actually being assigned to the rowsource.
Also, this query uses a Cartesian Join, which can involve a lot of results. Is there a field in common between your tables [t_0_MAIN_Downgrade_Tracki
BTW, I usually build the SQL then apply it to the rowsource so I can use:
debug.print strSQL
to test the value that is actually being assigned to the rowsource.
ASKER
Hi Experts,
I get a compile error if I try TheHighTechCoach solution.
If I try fyed's solution, I do get dropdown options back, however, they are still unfiltered (i.e. they are from the ComboBox's original control row source: SELECT t_1_COMBO_Confirmation.Con firmation_ ID, t_1_COMBO_Confirmation.Con firmation FROM t_1_COMBO_Confirmation ORDER BY t_1_COMBO_Confirmation.Con firmation; )
This is my attempt at fyed's code:
Private Sub cboStatus_Phase_1_AfterUpd ate()
Dim strsql As String
strsql = "SELECT t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1 " _
& "FROM t_0_MAIN_Downgrade_Trackin g, t_2_JUNCTION_WORKFLOW " _
& "WHERE Status_Phase_1 = " & Me.cboStatus_Phase_1 & " AND Action_Type_Phase_1_ID = " & Me.cboAction_Type_Phase_1_ ID & " " _
& "GROUP BY t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1;"
Debug.Print strsql
End Sub
I get a compile error if I try TheHighTechCoach solution.
If I try fyed's solution, I do get dropdown options back, however, they are still unfiltered (i.e. they are from the ComboBox's original control row source: SELECT t_1_COMBO_Confirmation.Con
This is my attempt at fyed's code:
Private Sub cboStatus_Phase_1_AfterUpd
Dim strsql As String
strsql = "SELECT t_2_JUNCTION_WORKFLOW.Conf
& "FROM t_0_MAIN_Downgrade_Trackin
& "WHERE Status_Phase_1 = " & Me.cboStatus_Phase_1 & " AND Action_Type_Phase_1_ID = " & Me.cboAction_Type_Phase_1_
& "GROUP BY t_2_JUNCTION_WORKFLOW.Conf
Debug.Print strsql
End Sub
so, whey you tried it using my code, what got printed out in the debug window?
That looks like it should be correct (assuming that [Status_Phase_1] is numeric and the bound column in me.cboStatus_Phase_1 is also numeric, and that the same applies for [Action_type_Phase_1_ID]
But you forgot to set the rowsource.
me.cbo_ThirdField.Rowsourc e = strSQL
The other point is that if you are doing this for the cbo_Status_Phase_1_AfterUP date event, does the other combo even have a value at that point?
Can you take a screen shot of your form in design view, so that we can see the controls and the order they are in?
That looks like it should be correct (assuming that [Status_Phase_1] is numeric and the bound column in me.cboStatus_Phase_1 is also numeric, and that the same applies for [Action_type_Phase_1_ID]
But you forgot to set the rowsource.
me.cbo_ThirdField.Rowsourc
The other point is that if you are doing this for the cbo_Status_Phase_1_AfterUP
Can you take a screen shot of your form in design view, so that we can see the controls and the order they are in?
ASKER
Hi fyed,
Yes, all numeric and the other combo has a value. I have stripped out confidential information and attached my database....starting to get confused by the SQL bit...it's looking like I bit off more than I can chew! Maybe the way the rest of the database works interferes with filtering? Is the after update event the right one to choose?
(The combo boxes I am working on are on the first tab of 1_Main_Form)
Thanks!
Expert-Exchange.accdb
Yes, all numeric and the other combo has a value. I have stripped out confidential information and attached my database....starting to get confused by the SQL bit...it's looking like I bit off more than I can chew! Maybe the way the rest of the database works interferes with filtering? Is the after update event the right one to choose?
(The combo boxes I am working on are on the first tab of 1_Main_Form)
Thanks!
Expert-Exchange.accdb
OK,
Now that I've had a chance to take a look at your data, can you explain to me what your t_2_JUNCTION_Workflow table is supposed to do? It has a compound primary key containing six fields, I assume the point of that is to ensure uniqueness of each record in that table. But what is it supposed to do?
I think what you probably want as the row source of cboConfirmation_Phase_1 is:
Private Sub cboStatus_Phase_1_AfterUpd ate()
Dim strsql As String
strsql = "SELECT t_2_JUNCTION_WORKFLOW.Conf irmation_P hase_1" _
& " FROM t_2_JUNCTION_WORKFLOW" _
& " WHERE Status_Phase_1 = " & Me.cboStatus_Phase_1 _
& " AND Action_Type_Phase_1_ID = " & Me.cboAction_Type_Phase_1_ ID
Debug.Print strsql
Me.cboConfirmation_Phase_1 .RowSource = strsql
End Sub
Now that I've had a chance to take a look at your data, can you explain to me what your t_2_JUNCTION_Workflow table is supposed to do? It has a compound primary key containing six fields, I assume the point of that is to ensure uniqueness of each record in that table. But what is it supposed to do?
I think what you probably want as the row source of cboConfirmation_Phase_1 is:
Private Sub cboStatus_Phase_1_AfterUpd
Dim strsql As String
strsql = "SELECT t_2_JUNCTION_WORKFLOW.Conf
& " FROM t_2_JUNCTION_WORKFLOW" _
& " WHERE Status_Phase_1 = " & Me.cboStatus_Phase_1 _
& " AND Action_Type_Phase_1_ID = " & Me.cboAction_Type_Phase_1_
Debug.Print strsql
Me.cboConfirmation_Phase_1
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
First, verify that the RowSourceType property is set to Table/Query
---
In this situation, I would expand on vadimrapp1's suggestion --- keep the RowSource as a 'fixed' SQL statement that references the form controls ...
Then, since the RowSource is dependent upon TWO controls, you need to trigger a requery of the RowSource after an update of either of the two dependancies.
---
If you don't like the 'Fixed' SQL concept, then I suggest that you create a Private Function in the code behind your Form object that looks what Fyed modeled ...
Then set the AfterUpdate PROPERTY for cboStatus_Phase_1 and Me.cboAction_Type_Phase_1_ ID to ...
=pfSetConfirmationRowSourc e()
---
{Note: that the SQL statements I proposed handle the possibility of Null values in those controls. I coerced the Null values to -1 with the assumption that a -1 would NOT exist is the domain of the values that populate the respective fields. I did that with the assumption you would NOT want records returned if either dependent control was Null/Blank. If that is not the preferred behavior, then let us know and we can guide you as to how to change that behavior}
---
In this situation, I would expand on vadimrapp1's suggestion --- keep the RowSource as a 'fixed' SQL statement that references the form controls ...
SELECT t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1
FROM t_2_JUNCTION_WORKFLOW
WHERE Status_Phase_1 = CLng(Nz([Forms]![nameOfYourFormObject]![cboStatus_Phase_1],-1))
AND Action_Type_Phase_1_ID = CLng(Nz([Forms]![nameOfYourFormObject]![Me.cboAction_Type_Phase_1_ID],-1))
Then, since the RowSource is dependent upon TWO controls, you need to trigger a requery of the RowSource after an update of either of the two dependancies.
Private Sub cboStatus_Phase_1_AfterUpdate()
Me.cboConfirmation_Phase_1.Requery
End Sub
Private Sub Action_Type_Phase_1_ID_AfterUpdate()
Me.cboConfirmation_Phase_1.Requery
End Sub
---
If you don't like the 'Fixed' SQL concept, then I suggest that you create a Private Function in the code behind your Form object that looks what Fyed modeled ...
Private Function pfSetConfirmationRowSource() As Byte
Dim strsql As String
strsql = "SELECT t_2_JUNCTION_WORKFLOW.Confirmation_Phase_1" _
& " FROM t_2_JUNCTION_WORKFLOW" _
& " WHERE Status_Phase_1 = " & Nz(Me.cboStatus_Phase_1,-1) _
& " AND Action_Type_Phase_1_ID = " & Nz(Me.cboAction_Type_Phase_1_ID,-1)
Debug.Print strsql
Me.cboConfirmation_Phase_1.RowSource = strsql
End Sub
Then set the AfterUpdate PROPERTY for cboStatus_Phase_1 and Me.cboAction_Type_Phase_1_
=pfSetConfirmationRowSourc
---
{Note: that the SQL statements I proposed handle the possibility of Null values in those controls. I coerced the Null values to -1 with the assumption that a -1 would NOT exist is the domain of the values that populate the respective fields. I did that with the assumption you would NOT want records returned if either dependent control was Null/Blank. If that is not the preferred behavior, then let us know and we can guide you as to how to change that behavior}
ASKER
As pathetic as this sounds, I'm super excited about this solution, it solves alot of my problems!
ASKER
Shoot, I didn't see datAdrenaline's expansion until after I accepted the solution!
No problem ... I will just continue to sit here trying desperately to get 200 points to maintain my status for this month ... woes me ... alms for the poor ... <moan and groan> ... LOL!!!
---
<note: sarcasm, humor, etc. heavily applied above!>
----
Now the sincere part:
Good luck on your project! Glad you got the help you needed!
---
<note: sarcasm, humor, etc. heavily applied above!>
----
Now the sincere part:
Good luck on your project! Glad you got the help you needed!
ASKER
Thanks so much datAdrenaline, you have just saved me from having to struggle with the next part of my problem or having to write a second question. I learn so much from the Experts and appreciate all responses.....I would go back and give you 1000 points if I could!
@datAdrenaline,
https://www.experts-exchange.com/questions/28225205/Rowsource-lookup-by-other-than-bound-column.html
btw, you may want to update the link in your profile/about me
https://www.experts-exchange.com/questions/28225205/Rowsource-lookup-by-other-than-bound-column.html
btw, you may want to update the link in your profile/about me
Thanks! ... Updated the you referenced. :)
Me.cboConfirmation_Phase_1
In there after you set the RowSource.
Try running that RowSource query on its own as a stand-alone query. Does it return any rows?