List box based on combo choice

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
John SheehySystem Security ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John SheehySystem Security ManagerAuthor Commented:
I am going to try this right now.
John SheehySystem Security ManagerAuthor 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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

John SheehySystem Security ManagerAuthor 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, Developing Solutions LLCCommented:
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 ManagerAuthor Commented:
This is awesome.  Worked like a charm and way more efficient than I expected.  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Coding

From novice to tech pro — start learning today.