List box based on combo choice

John Sheehy
John Sheehy used Ask the Experts™
on
I have a form with a combo box for listing all the assets we have.  Since we now have over 10 different networks and thousands of different assets I want to do something different.

I want the user to be able to choose the network from the combo box and the list box display all the assets.

I have never user list boxes before so I have no idea how they work.  

So this what I have:
Combo box is cbo_net; List box is HWB_List
cbo_net is based on the query, qry_Inventory

Thanks,
John
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
john,

What you are dealing with is cascading controls, usually you see the discussion as cascading combo boxes, but it applies equally to single select listboxes.

The way to do this is you set the RowSource of the listbox to refer to the value of your combo box, something like:
Select yourTable.Field1, yourTable.Field2
FROM yourTable
WHERE yourTable.Network = forms!yourFormName.cbo_Net

Open in new window

Then, in the AfterUpdate event of cbo_Net, you simply requery the listbox:
Private sub cbo_Net_AfterUpdate

    me.HWB_List.Requery

End Sub

Open in new window

John SheehySystem Security Manager

Author

Commented:
I am going to try this right now.
John SheehySystem Security Manager

Author

Commented:
Won't let me type in the SQL syntax into the row source nor will it allow me to define it when the from loads, gives me a compile error: Expected: expression

This is how I have it entered:
Me.HWB_LIST.RowSource = Select qry_Inventory.Asset_Name FROM qry_Inventory WHERE qry_Inventory.Net = forms![Inventory Title Page].cbo_Net
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

John SheehySystem Security Manager

Author

Commented:
Ok, so I see what I did.  I did not set up the list box correctly.  I am doing that now and will update in a minute.

John
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
Well, you wouldn't need to define it when the form loads, do it in design view.  I find that the best way to do this is to remove the rowsource from the listbox, and then open the form so that you can make a selection in cbo_Net.

Then, with the form open, create a new query and enter your SQL in the query designer.  This will allow you to test your SQL string against the value selected in cbo_Net.  Change your SQL to include the [Net] column from qry_Inventory so that you can confirm that it is displaying the value in that column that exists in cbo_Net.  I would use a SQL statement like the following:

Select Asset_Name, [Net] 
FROM qry_Inventory 
WHERE Net = NZ(forms![Inventory Title Page].cbo_Net, "")   

Open in new window

This assumes that the [NET] column is a text value.

Run the query, change the value in cbo_Net of your form and rerun the query.  If it works, remove the [NET] column from the results and save that query (I give mine names like qry_frm_MyFormName_lst_AssetName).

Then change the form view to design and select that saved query as the RowSource for the listbox.

Just out of interest, what are you doing with the value from the listbox?  Are you binding it to a field, or using it for some other purpose?  If you are binding it to a field, I find that I prefer to use the AssetID rather than the AssetName as the bound column, as this saves space in my BE data file.
John SheehySystem Security Manager

Author

Commented:
This is awesome.  Worked like a charm and way more efficient than I expected.  Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial