473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

return a value in combo box based on another combo box's selection

Hello amazing vba writers,
I am trying to make a combo box return a value based on a combo box selection. I have tried a number of the codes posted here and am still having problems. I think it is because I am trying to return a value from a table that only links with the form through an intermediate table. I am hoping to avoid a subform for this one box if at all possible, because of space limitations. Any guidance would be greatly appreciated!

I have a form called frmBldgs based on a table called tblBldgs with the following fields:
Field1: BldgID – autonumber primary key
Field2: BldgNm – name of building
Field3: Addr1IDFK – foreign key that links to a table called tblAddr1
Field4: DistrictIDFK – foreign key that links to a table called tblDistricts
etc….

The table called tblDistricts has the following fields:
Field1: DistrictID – autonumber primary key
Field2: DistrictNm – name of sanitation district
Field3: SanBoroIDFK – foreign key that links to a table called tblSanBoros

(Each SanBoro has a number of districts nested beneath it.)

The table called tblSanBoros has the following fields:
Field1: SanBoroID – autonumber primary key
Field2: SanBoroNm
Field3: BoroIDFK – foreign key that links to a table called tblBoroughs

(Each Borough has one or two SanBoros nested beneath it.)

I also have the following simple query, called qrySanDistBoro:
SELECT tblBldgs.DistrictIDFK, tblSanBoros.SanBoroID, tblDistricts.DistrictNm, tblSanBoros.SanBoroNm
FROM tblSanBoros INNER JOIN (tblDistricts INNER JOIN tblBldgs ON tblDistricts.DistrictID = tblBldgs.DistrictIDFK) ON tblSanBoros.SanBoroID = tblDistricts.SanBoroIDFK;

The first combo box I am working with in frmBldgs is called cboSanDistrict.
Its ControlSource is DistrictIDFK.
I made the Row Source the following so that it returns the DistrictNm in the form from tblDistricts:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, tblDistricts.DistrictNm
  2. FROM tblDistricts
  3. ORDER BY tblDistricts.DistrictID;
  4.  
I have added a combo box called cboSanBoro that I hope to fill with SanBoroNm from the tblSanBoros that matches the DistrictNm chosen in cboSanDistrict.
The Control Source for cboSanBoro is DistrictIDFK.

For the Row Source, I have tried using the following query
SELECT qrySanDistBoro.SanBoroNm
FROM qrySanDistBoro;

This did not work. The combo box just returns the DistrictIDFK associated with the DistrictNm in cboSanDistrict.

I’m guessing the simple query was too simple because tblSanBoros is not directly linkable to tblBldgs except through tblDistricts. Maybe I need to change the control source?? (but to what?)

Thanks in advance, any help would be greatly appreciated!
Bridget
Oct 2 '07 #1
4 3164
Scott Price
1,384 Expert 1GB
Hi Bridget,

In your query:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblBldgs.DistrictIDFK, tblSanBoros.SanBoroID, tblDistricts.DistrictNm, tblSanBoros.SanBoroNm
  2. FROM tblSanBoros INNER JOIN (tblDistricts INNER JOIN tblBldgs ON tblDistricts.DistrictID = tblBldgs.DistrictIDFK) ON tblSanBoros.SanBoroID = tblDistricts.SanBoroIDFK;
You need to add a WHERE criteria pointing to the DistrictID chosen in the cboSanDistrict. This should look something like: WHERE tblDistricts.DistrictID = Forms![FormName]!cboSanDistrict

Regards,
Scott
Oct 3 '07 #2
Hello Scott,
Thank you for this suggestion!
I have tried to add a WHERE statement without luck. :(
It makes logical sense that a WHERE statement would be needed, but when I tried to add what you suggested in the row source for cboSanBoro, it first prompts me with a dialogue box to enter a parameter value for cboSanDistrict (even when a value already exists in the cboSanDistrict combo box already!). Once I enter a value, nothing is returned in cboSanBoro. When I’ve made a choice in the dialogue box once, I can then choose straight from the cboSanDistrict box, but still no values are returned in cboSanBoro.

Here is what I have tried…
1. With the original code I sent, the DistrictIDFK is returned in the cboSanBoro box. This is the right idea, except the wrong value is returned (rather than DistrictIDFK, I'd like SanBoroNm returned.)

2. When I put the following in the row source for cboSanBoro, it first prompts me for a parameter value for cboSanDistrict (even if a value is already chosen in the cboSanDistrict box), and when I choose a cboSanDistrict value, nothing is returned in cboSanBoro.
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, qrySanDistBoro.SanBoroNm
  2. FROM qrySanDistBoro INNER JOIN tblDistricts ON qrySanDistBoro.DistrictIDFK = tblDistricts.DistrictID
  3. WHERE (((tblDistricts.DistrictID)=[Form]![frmBldgsAdd]![cboSanDistrict]));
  4.  
(The qrySanDistBoro is the same as the original one I posted)

3. Perhaps the closest I have gotten so far is the following code in which I get the opposite effect of what I want: when I make a selection in cboSanBoro, the correct SanDistrict is returned in the cboSanDistrict box. If I make a selection in cboSanDistrict, nothing is returned in cboSanBoro.

Code in row source of SanBoro:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, qrySanDistBoro.SanBoroNm
  2. FROM qrySanDistBoro INNER JOIN tblDistricts ON qrySanDistBoro.DistrictIDFK = tblDistricts.DistrictID;
  3.  
This code does not limit the list in cboSanBoro (show’s all possible values), but when I choose one, it does return the correct value for cboSanDistrict. If I could combine the outcome of 1 and 3, I'd be golden...

Any insight is greatly appreciated!
Bridget
Oct 3 '07 #3
Hello Scott and anyone else following this string,
I found a work around for my problem. Its not as slick what I originally was trying to do, but works fine.

I used a query that looks up the entire list of SanBoros and nested Districts:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, tblSanBoros.SanBoroID, tblDistricts.DistrictNm, tblSanBoros.SanBoroNm
  2. FROM tblSanBoros INNER JOIN tblDistricts ON tblSanBoros.SanBoroID=tblDistricts.SanBoroIDFK;
  3.  
I made the row source for cboSanBoro the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, qrySanDistBoro2.SanBoroNm, tblDistricts.DistrictNm
  2. FROM tblDistricts INNER JOIN qrySanDistBoro2 ON tblDistricts.DistrictID = qrySanDistBoro2.DistrictID
  3. ORDER BY tblDistricts.DistrictID;
  4.  
Then I set up the properties of cboSanBoro so that both SasnBoroNm and DistrictNm show up on the drop down:
ColumnCount: 3
ColumnWidth: 0";1";1"
BoundColumn: 1

The row source for cboSanDistrict is the same as my original posting:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistricts.DistrictID, tblDistricts.DistrictNm
  2. FROM tblDistricts
  3. ORDER BY tblDistricts.DistrictID;
  4.  
Now, when I click on the drop down of cboSanBoro, I can see both the SanBoro and District name and choose the correct district. This returns the correct SanBoro in cboSanBoro and the correct SanDistrict in cboSanDistrict. If I choose another DistrictNm from cboSanDistrict, the correct SanBoro is returned in cboSanBoro.

I'd still love to know how to properly filter, but it'll have to wait until I have a bit more time to play around with things!

Bridget
Oct 3 '07 #4
Scott Price
1,384 Expert 1GB
Bridgit,

Glad you found a work-around, and thanks for posting back with what you've got working.

Regards,
Scott
Oct 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when...
1
by: OhFiddleSticks | last post by:
Does anyone know if there is a way to add a text value to a combo box (the text box part, not the rowsource) in VBA without triggering an update event? I've tried everything I can think of without...
2
by: Jeff Barry | last post by:
Hi, I wonder if any one can help, I'm pretty new to Access and I can't figure out how to change the contents of a combo box based on a selection I make in another. Let me explain I have a...
0
by: CSDunn | last post by:
Hello, In Access ADP's that connect to SQL Server databases, any time I have a situation where I have a combo box in a main form that looks up a record in a subform, the subform record source has...
8
by: Lyn | last post by:
Hi, Can anyone tell me how the initial value displayed in Combo Box is determined when a form is opened? I am loading the dropdown from one field ("CategoryName") of a table, with "ORDER BY ". ...
3
by: hmiller | last post by:
Hey everyone, I am having a hell of a time trying to set this menu system up. Here's what I'm trying to do. Combo Box One; is populated by names under properties "row source" "Phase 1"...
1
by: sara | last post by:
I am learning how to use simple functions to make my apps more powerful and efficient. On one screen, I want to populate field B: ItemDescription by looking up the ItemDescription in the Items...
1
by: peasedm | last post by:
Okay this one has me stumped. I have a table called Review_Statements with the following columns: statementid type statement1 statement2 statement3 I have a form called SR_Review with an...
1
by: ikuyasu | last post by:
Hi, I am trying to create a report that takes a value from the field on a table as a group category. But The value on the report takes an id number (auto increment, and the first column)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.