473,395 Members | 1,368 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,395 software developers and data experts.

Filtered Combo Box on the detailed section of a subform

kcdoell
230 100+
Hello:

I have a form (Default view =single form) with a subform (Default view =continuous forms) embedded into it. In the form I have three controls that display the Division, Working Region & Credit Region. The subform displays the data/records. The record source for both my form and subform is driven by a query, which is the way I filter the records that the end user will see via another form I created.

Everything is working fine but I can not seem to figure something out that I have never done before.

The issue is that the end user can add a new record via my subform. My challenge is that one of the fields that is displayed is called “Product” There are a lot of products to choose from on this drop down list but not as many if the list was filtered by Working Region. The layout of my product table is as follows:

Product:
tblProduct
ProductID = Autonumber
ProductsName = Text
WrkRegID = Number (This is for my one to many relationship; a working region can have many different Products)

Is there a way to filter the list of my product field on my subform for new records?

I was trying to experiment with doing this and can not figure it out. What I did get to work was on the header of my subform I dropped in a combo box and then on the load event of that form wrote the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. 'When the Working Region has been displayed on the form, the appropriate Product list
  3. 'will display in the drop down list of CboProduct
  4.  
  5.   With Me![cboProduct]
  6.     If IsNull(Me!txtWrkReg) Then
  7.       .RowSource = ""
  8.     Else
  9.       .RowSource = "SELECT [ProductsName] FROM tblProduct WHERE [WrkRegID]=" & _
  10.                     DLookup("[WrkRegID]", "tblWrkRegion", "[WrkRegionName] = '" & Me![txtWrkReg] & "'")
  11.     End If
  12.     Call .Requery
  13.   End With
  14.  
  15. End Sub
  16.  
As mentioned this worked. The problem is that the minute I embedded that control “cboProduct” in the detail portion of my subform I do not get any errors, the list will be appropriately populated but if let’s say three records are displaying, the minute I make a selection from cboProduct all of the records will be change to that selection. I only want it to change new records not the old ones.

Does anybody have any idea on how this can be done??

Thanks,

Keith.
Mar 13 '08 #1
14 2306
Stewart Ross
2,545 Expert Mod 2GB
Hi Keith. From what you say it would appear that the combo is not bound to one of the fields in your subform, as unless you are calling an after-update event to update records when you make a selection from the combo it cannot of itself change multiple records - but it can give the appearance that it is.

A subform actually repeats the same set of controls on each row shown on the form, and if one of those is unbound you see the same value repeated on every row. This makes it look like you have changed all records - but as mentioned unless there is an After Update event in use it has not actually done anything at all to your records.

An unbound control is one whose controlsource is blank - one that is not bound to a specific field in your underlying table. If you are embedding an unbound combo box control in the detail section of your subform it's in the wrong place.

Ask yourself what you want the combo to do on your subform, and if that is to provide a list for one of the table values in your subform you will need to bind it to the appropriate field.

By the way, the form load event is fired only when you first open the form - is this the right timing for the conditional recordsource loading in your event code?

-Stewart
Mar 13 '08 #2
kcdoell
230 100+
Hello:

Let me answer the last question first:

"By the way, the form load event is fired only when you first open the form - is this the right timing for the conditional recordsource loading in your event code?"

Once the end user in on the form the "working region" parameter can not be changed within it, so there is no problem with the load event only firing once.

On another note, I am not yet an advance Access person so please be patient if I don't entirely understand your response.

Let me see if I can better explain the scenario:

Let's say my subform is displaying 3 records that only have 3 fields:
Expand|Select|Wrap|Line Numbers
  1. RecID      Product         ImageDate         Company
  2. 1            ASL           12/12/2005      Richardson Inc.
  3. 2            BAA           11/10/2005      Peterson Corp.
  4. 3            DWW           08/21/2005      Harolds Inc.
Currently in my “Product” field it is displaying the full gamut of Products (when you click on the combo box) because its row source is pointing to my Product table (tblProduct). So if I add a new record, once I key into the product field my choices will be the full list. What I am trying to do is have it so that the choices is limited by the working region. In essence, I am trying to filter the list by working region so the end user can only select those now limited choices.

How would one do that? Do you have an idea I could try?

Thanks,

Keith.
Mar 13 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi Keith. You can use a combo for selection of the product field, a combo which is bound to the value of the product field itself. To do this you delete your existing product field on the subform and replace it with a combo, using the combowizard to build the basic query for the recordsource from your product table. This can be tweaked with a WHERE clause similar to the one in your form load event to filter for the region. The DLookup should not be necessary.

The SQL for this will look something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT [Product], [ProductsName] FROM tblProduct WHERE [WrkRegionName] = '" & Me![txtWrkReg] & "'"
assuming I am interpreting your fields correctly.

The main difference between this and what you had before is that the rowsource is returning the product ID and the product name. It is the product ID which is the value that you store in the subform table, not the name of the product. The combo wizard will itself recommend that you hide the key value - showing the product name, but recording the product ID instead. This is done by setting the ID column width of the combo to 0 to hide the column.

-Stewart
Mar 13 '08 #4
kcdoell
230 100+
I have been trying to get this to work all day......... My problem is that it is looking for the ID number but is only finding the name of the Working Region...
Mar 14 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
I have been trying to get this to work all day......... My problem is that it is looking for the ID number but is only finding the name of the Working Region...
Hi Keith. Is it the case that you do not have a product ID field in your table? If you don't it would explain why you are only returning the product name. You need to bind the combo to whatever is the key field of your product table; if this is the product name, then you wll only need the one column in your combo and you should just return the name and nothing else.

In relational table design there is always a primary key which uniquely identifies rows in a table. There is a good article on table design and normalisation in the HowTo section of the forum, at http://www.thescripts.com/forum/thread585228.html

-Stewart
Mar 14 '08 #6
kcdoell
230 100+
Okay:

So what I did was go into the combo box of "Product" and put the row source as follows:

SELECT [ProductName] FROM tblProduct WHERE [WrkRegID] = '" & Me![txtWrkReg] & "'"

What I am getting is a syntax error message when I click on the drop down box. I believe my other problem is that txtWrkReg is displaying the name where my tblProduct is looking for the Working Region Name.

Could that be the reason I am having the syntax errors or is the code missing something?

Keith.
Mar 14 '08 #7
kcdoell
230 100+
Hi Keith. Is it the case that you do not have a product ID field in your table? If you don't it would explain why you are only returning the product name. You need to bind the combo to whatever is the key field of your product table; if this is the product name, then you wll only need the one column in your combo and you should just return the name and nothing else.

In relational table design there is always a primary key which uniquely identifies rows in a table. There is a good article on table design and normalisation in the HowTo section of the forum, at http://www.thescripts.com/forum/thread585228.html

-Stewart

Below is my table layout:

Product:
tblProduct
ProductID = Autonumber
ProductsName = Text
WrkRegID = Number (This is for my one to many relationship; a working region can have many different Products)

As noted earlier it does have a unique ID number and it has a relationship with the working region ID (One to many).

Does that answer your question????
Mar 14 '08 #8
kcdoell
230 100+
I just read the article. I am not an expert but I believe that my structure is normalized with my 1to1 and 1toMany relationships.
Mar 14 '08 #9
Stewart Ross
2,545 Expert Mod 2GB
Below is my table layout:

Product:
tblProduct
ProductID = Autonumber
ProductsName = Text
WrkRegID = Number (This is for my one to many relationship; a working region can have many different Products)

As noted earlier it does have a unique ID number and it has a relationship with the working region ID (One to many).

Does that answer your question????
Hi Keith. Thank you for the clarification and your previous post. You do indeed have an ID field which is unique; and you are right about the region name/ number mismatch causing the drop-down error. We will need to restore the use of the Dlookup to get back the region number from the region name, although this should not have been necessary.

I'll post back shortly with revised SQL for your combo.

-Stewart
Mar 14 '08 #10
kcdoell
230 100+
Below is my table layout:

Product:
tblProduct
ProductID = Autonumber
ProductsName = Text
WrkRegID = Number (This is for my one to many relationship; a working region can have many different Products)

As noted earlier it does have a unique ID number and it has a relationship with the working region ID (One to many).

Does that answer your question????

I have been doing some reading and the above WrkRegID is the foreign key of which I am trying to filter on. Since it is a number and my txtWrkReg on my form is the name, I believe that is where the problem is.
Mar 14 '08 #11
Stewart Ross
2,545 Expert Mod 2GB
OK, the following SQL does return the numeric product ID and the product name for display in your combo. It does not use a Dlookup - it simply joins the wrkRegion table to the query instead.

The where clause refers to the combo box on your form. As I do not know the name of your form there is a placeholder name there which you will need to replace with the real name. Please note that because it depends on the actual value of the control you will not see anything returned by the combo query when the form is in design view - only when it is active.

You will then be able to bind the combo to the product ID field on your form, as the combo will be filtered to return only the values matching that region.

The SQL is
Expand|Select|Wrap|Line Numbers
  1. SELECT [ProductID], [ProductName] FROM tblProduct 
  2. INNER JOIN tblWrkRegion ON 
  3. tblwrkRegion.[wrkRegID] = tblProduct.[wrkRegID]
  4. WHERE tblWrkRegion.[wrkRegionName] = forms![your form name]![txtWrkReg]
  5. ORDER BY tblProduct.[ProductName];
I tested this by preparing two test tables with the same fields and names as your Product and Region tables, pupulating them with some dummy data, and trying out the query from the query editor after entering the raw SQL.

-Stewart
Mar 14 '08 #12
kcdoell
230 100+
Stewart:

First thanks for hanging in there with me. I inputted the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT [ProductID], [ProductName] FROM tblProduct 
  2. INNER JOIN tblWrkRegion ON 
  3. tblwrkRegion.[wrkRegID] = tblProduct.[wrkRegID]
  4. WHERE tblWrkRegion.[wrkRegionName] = forms![Forecastform]![cboWrkReg]
  5. ORDER BY tblProduct.[ProductName];
  6.  
into my product combo box field, no errors but it is displaying the Product ID number and not the Product Name on the drop down list.

Any ideas?
Mar 14 '08 #13
Stewart Ross
2,545 Expert Mod 2GB
Stewart:

First thanks for hanging in there with me. I inputted the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT [ProductID], [ProductName] FROM tblProduct 
  2. INNER JOIN tblWrkRegion ON 
  3. tblwrkRegion.[wrkRegID] = tblProduct.[wrkRegID]
  4. WHERE tblWrkRegion.[wrkRegionName] = forms![Forecastform]![cboWrkReg]
  5. ORDER BY tblProduct.[ProductName];
  6.  
into my product combo box field, no errors but it is displaying the Product ID number and not the Product Name on the drop down list.

Any ideas?
Hi Keith. I'm really glad you got the query in and it is working so far...

For the combo width issue check that the Column Count property in the combo is set to 2, and the Column Width property has two values in it like this; 1cm;5cm (these are just examples - the widths could also be in inches).

The other property that needs to match these is the List Width, which should be the sum of all the widths of the columns. for the example given, the list width would be 6cm.

To hide the ID column after you are showing both columns, you can set its width to 0, which would leave the width property showing something like 0cm; 5cm

-Stewart
Mar 14 '08 #14
kcdoell
230 100+
Stewart:

I just want to thank you for helping me with this problem. As you know, I am still in learning mode with applying sql and vba amongst other things. Your thoughtful responses where over the top and I thank you for that. In the end, I got it to work.

Thanks to you!

Take care,

Keith.
Mar 14 '08 #15

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

Similar topics

2
by: Kelly | last post by:
I have a subform that display requisition information. One of the fields in the subform is a combo box that shows who requested the requisition. The users can change who requested the requisition...
3
by: Mike Jakes | last post by:
I hope that someone can offer a little advice on this one - I've searched the group but can't find an answer. I think that I'm doing something really stupid or missing something trivial, but see...
6
by: Rick | last post by:
I have a main form called frmDemo with a subform frmStageTrack_Sub. On the subform is a Combo Box CboTermID. The frmDemo is tied to tblDemo and the frmStageTrack_Sub is tied to tblStageTrack. ...
2
by: docsix | last post by:
I am having trouble populating a combo box in a subform. Currently I have two combo boxes on a single form that works. This is my current setup: Table:Facilities - FacilityID ... FacilityType 1...
4
by: virtualgreek | last post by:
Dear All, First of all I would like to take the time to thank you all for your efforts and time spent at this wonderful forum. I have found it very helpful with numerous examples available for...
0
by: Oerf | last post by:
I have a form were I select the deal name and therein a subform in which a have a combo box to specify the type of input for the selected deal. This combo box should show my the types that are...
5
by: Penstar | last post by:
I have a Combo Box on a Subform which works perfectly when the suborm is opened on its own. However, when I open the Main form Containing the Subform, and try to use the Combo Box it comes up with...
12
by: Supermansteel | last post by:
I have a combo box called cmb_category. I then have the SQL statement as such: SELECT Originations_Categories.Category, Originations_Categories.Category_ID FROM Originations_Categories WHERE...
1
by: Yousaf Shah | last post by:
Hello everybody i am building my dataabse for patient record. my main record entry form "patients Record" is a tabbed form with tabsas follows tab "NOK" having subform NOK, tab "History/Exam"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.