Avatar of RLHunter
RLHunter
Flag for United States of America asked on

Filter Combo Box Based on Selection in Different Combo Box

I have a single form that has two combo boxes.  One of these is [Current Facility] and one is [Current Room].  I have been trying for many unsuccessful hours to come up with a way to fillter the [Current Room] based on the selection that is made for [Current Facility].  The values for [Current Facility] are determined based on a table Facilities and the values for [Current Room] are based on a table Rooms that link the facility to the room.
Can someone please help me with a solution to this?  Right now, the [Current Room] combo box displays all 150 rooms.  This is a problem because it could mess up reporting if people select one facility and a room that is not in that facility.
I thought it would be simple to have the [Current Room] combo box be filtered by what the user selects in the [Current Facility] box.
Microsoft Access

Avatar of undefined
Last Comment
RLHunter

8/22/2022 - Mon
Rey Obrero (Capricorn1)

<I thought it would be simple to have the [Current Room] combo box be filtered by what the user selects in the [Current Facility] box. >

yes. This is how you should do it. but, you have to properly identify the rowsource of the second combo by linking the two tables.

answer  this q
what is the field that link the tables Facility and Rooms?

what is the rowSource of Combo1?
what is the rowSource of combo2?

RLHunter

ASKER
answer  this q
what is the field that link the tables Facility and Rooms?
The field is Facility
what is the rowSource of Combo1?
SELECT Facilities.Facility FROM Facilities;
what is the rowSource of combo2?
SELECT Rooms.Room FROM Rooms;
Rey Obrero (Capricorn1)

in the after update event of combo1

private sub combo1_afterupdate()

me.combo2.rowsource="select room from rooms where facility='" & me.combo1 &"'"

end sub

this is assuming that the facility field is Text Data Type

if it is Number use this

private sub combo1_afterupdate()

me.combo2.rowsource="select room from rooms where facility=" & me.combo1

end sub
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
RLHunter

ASKER
Ok...so we seem to be on the right track, but still have  a slight problem.  I have entered in the code as follows, but I get "Syntax error (missing operator) in query expression 'facility=MCN III (Barrier)'. -- which is replaced by any of the facilities that I select, and nothing is available in the dropdown list.
Private Sub Current_Facility_AfterUpdate()
Me.Current_Room.RowSource = "Select room from rooms where facility=" & Me.Current_Facility & ""
 
End Sub

Open in new window

Rey Obrero (Capricorn1)


use this

Private Sub Current_Facility_AfterUpdate()
Me.Current_Room.RowSource = "Select room from rooms where facility=" & chr(34) & Me.Current_Facility & chr(34)
 
End Sub
RLHunter

ASKER
That won't work.  Now I get the "data type mismatch in criteria expression"  
I tried removing the  & Chr(34), but got the Syntax error again.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sheils

Have a look at the attached sample in which the combo box in the subform is filtered based on the selection made  from the listbox on form1. Have a look at the where condition of the query that is used as the control source of the combo box.

You can use a similar method for your case

sample1.mdb
RLHunter

ASKER
sb9,
Tried your solution, with no results.  This turns up a blank dropdown list.

capricorn1,
I'm not sure how to upload my database. Can you give instructions? I know I saw some on here at one time, but when I actually need them I cannot find them.
Rey Obrero (Capricorn1)

if your db is .accdb add an  .txt extension i.e.,
  if the name is  myDB.accdb, create a copy and rename it  copymyDB.accdb.txt

check the Attach File below to initiate upload
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Sheils

did you open form1,select one of the items in the listbox then click on the dropdown box in the subform
RLHunter

ASKER
Capricorn1,
I figured out how to get it uploaded, thank you.  It is attached to this post.

sb9,
There is no subform, so maybe that is why your solution did not work.  I did select an item in the listbox, but the dropdown box would be blank.
Copy-of-Transfer-Database.accdb.txt
Rey Obrero (Capricorn1)

RLHunter,
upload an accdb not an accde.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RLHunter

ASKER
That is an accdb.
Rey Obrero (Capricorn1)

it is NOT an accdb, believe me when i said it is an accde.

nevermind.. see this sample db


Database1.accdb.txt
RLHunter

ASKER
None of these have worked.  Does anyone have  any suggestions on how I can accomplish this?  I simply want to filter a combo box based on what was selected in a different combo box.  In my database, there are two tables (facility and room) and one form.  The combo box for facility pulls information from the facility table.  I want the room combo box to pull the rooms from the room table but only show the rooms that are in a certain facility.  I have it currently where all of the rooms populate, but I only want the ones that belong in the facility that was selected.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Sheils

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
RLHunter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.