Results 1 to 14 of 14
  1. #1
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31

    Restricting Lookup Values in a Subdatasheet

    I am new to Access and am currently learning it so that I can construct a database of trivia questions.



    I have a "Questions" table with question, answer, type, etc. This table is linked to a "Categories" table with a many-to-many relationship using a junction table called "Questions_Categories". The "Categories" table is also linked to a "SubCategories" table with a Categories as the parent and SubCategories as the child. I have attached the database for ease of understanding.

    When I enter data into the "Questions" table using datasheet view, I open up a subsheet that shows me the Category and SubCategory fields which have their own lookups.

    My problem is the following: I want each category to have its own set of SubCategories. For example, the "Region-Specific" category has "Canada", "London", and "Ontario" as sub categories. However, the movies category should not have any sub categories. The problem is that when I choose Movies in the sub datasheet, the subcategory lookup still shows Canada London and Ontario when I don't want it to show anything. It doesn't make sense to have a Movies category with a London subcategory.

    I have a feeling that maybe the layout of my database is not set up for this. Any advice would be appreciated.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Quote Originally Posted by June7 View Post
    I set up the combo boxes the way it outlined, using cmb_CategoryID and cmb_SubCategoryID in the Questions_Categories form. I put Questions_Categories as a subform in the form Questions. Now when I load the form Questions, it asks me for the parameter Lookup_CategoryID.Category... I have no idea why.

    cmb_SubCategoryID Row Source:
    SELECT Questions_Categories.SubCategoryID FROM Questions_Categories WHERE (((Questions_Categories.CategoryID)=[forms]![Questions_Categories].[cmb_CategoryID])) ORDER BY Questions_Categories.SubCategoryID;

    I am pulling my hair out at this point

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I finally had a chance to look at your db.

    First comment, you are using lookups with alias in the table. Advise not to. Review http://access.mvps.org/access/lookupfields.htm

    Second, Type field in Questions table is multi-value. I NEVER use multi-value field. Be sure you fully understand multi-value before commit to this.

    Third, the subform container SourceObject is table. Cannot have code behind the table to do what you want. The SourceObject must be a form.

    Fourth, I don't understand the Region-Specific category value. Is this the only category that will have sub-category? I am inclined to think this should be a field in Questions table. Maybe need another value - All. Can a question be associated with more than one Region? Can a question be associated with more than one category? Is that what you are trying to do with Questions_Categories table, create these associations?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Quote Originally Posted by June7 View Post
    I finally had a chance to look at your db.

    First comment, you are using lookups with alias in the table. Advise not to. Review http://access.mvps.org/access/lookupfields.htm

    Second, Type field in Questions table is multi-value. I NEVER use multi-value field. Be sure you fully understand multi-value before commit to this.

    Third, the subform container SourceObject is table. Cannot have code behind the table to do what you want. The SourceObject must be a form.

    Fourth, I don't understand the Region-Specific category value. Is this the only category that will have sub-category? I am inclined to think this should be a field in Questions table. Maybe need another value - All. Can a question be associated with more than one Region? Can a question be associated with more than one category? Is that what you are trying to do with Questions_Categories table, create these associations?
    Thanks for taking the time to look at this. Is there a way to quickly change the lookup with alias to a Number field that displays foreign keys?

    Each category may or may not have a sub-category. For example, the category Region-Specific currently has subcategories London and Canada, and in the future, Science will have Physics, Chemistry, Biology, etc. I just want to have flexibility in the future when I search for questions. Each question may have multiple categories or sub-categories, and each category and subcategory will have multiple questions. This is what I was trying to do with the junction table Questions_Categories.

    I am not sure what is best for the subcategories. Should I have a "None" value to avoid blank values? Would appreciate any advice you have.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I presume each subcategory will be associated with only one category. For instance Physics is in the Science category but could not be in Region-Specific. I suggest a junction table that associates the categories and subcategories. Then the Questions_Categories table would save the Question ID and the Cat_SubCat ID as foreign keys.

    Then you might want to make use of cascading (dependent) comboboxes for selecting Cat_SubCat ID. Review: http://www.datapigtechnologies.com/f...combobox2.html

    The fields with lookups in table don't need to be changed to anything, they are already Number type fields, just change the Lookup properties from combobox to textbox.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I made a Categories_SubCategories junction table as you suggested and created a form based on this table. I added the comboboxes and set it up like that video said. When I run the query it correctly shows the SubCategory values that it should, but when I save the query and go to the form, the SubCategory combobox stays blank, even with the Requery added to the Category combobox. What could be wrong?

    I have attached it so you can see what I did.
    Attached Files Attached Files

  8. #8
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I have fixed the issue using some code you used in a previous thread. Thank you!!

    https://www.accessforums.net/forms/c...ble-16462.html

  9. #9
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31

    Cascading Combo Boxes in Subform - Can't assign value

    I set up cascading Combo Boxes in a form called Categories_SubCategories and they work correctly and are able to update the table Categories_SubCategories.

    When using this form as a subform in the form Questions, there are 2 problems.

    First:
    When I choose a value in the master combo box of the subform, I get the message "You can't assign a value to this object." I click OK, yet the table is still updated with the correct value. This occurs when trying to add a new record with the combo box.

    Second:
    The next problem is that when there is a value in the master combo box of the subform, I am prompted for the value of Forms!Categories_SubCategories.cmb_CategoryID, which is the master combo box. This is the criteria that makes the slave combo box only display the values associated with the value displayed in the master. It seems that when inside a subform, Access doesn't know what the master combo box value is anymore.

    Again, when being used in the primary form, it works fine. These problems only exist when being used as a subform in the form Questions.

    It seems the learning curve for Access is much steeper than I had anticipated...

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Categories_SubCategories form should not be used as a subform in Questions. Only purpose of this form is to create new records that associate categories and subcategories. Cascading comboboxes are not useful in this form.

    Now you need a junction table (Questions_Categories) that will associate questions with Category_SubCategories records. This assumes each question can be associated with more than one category/subcategory. This is where the cascading comboboxes can be useful.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I am trying to visualize how to use the cascading Combo boxes but I am at a loss. The Questions_Categories table has a QuestionID field and a Cat_SubCatID field that links to the Categories_SubCategories table. If I use cascading combo boxes, say the master box for Category and slave for SubCategory, how do I make the Cat_SubCatID field update according to the CategoryID and SubCategoryID chosen by the cascading boxes? The latter two ID's appear in Categories_SubCategories table. Is there a way to search for the Cat_SubCatID that corresponds to the CategoryID and SubCategoryID?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Options for data entry:

    1. single form bound to Questions_Categories with comboboxes to select question and category_subcategory (use cascading comboboxes for the category/subcategory)

    2. main form bound to Questions and subform bound to Questions_Categories with the cascading comboboxes

    3. main form bound to Categories_SubCategories and UNBOUND cascading comboboxes to find the desired Category_Subcategory record and subform bound to Questions_Categories and combobox to select question

    For options 1 and 2, purpose of the cascading comboboxes is to aid user in selecting the Category_Subcategory ID. User selects category from UNBOUND combobox which will restrict the choices of subcategories in second combobox. User selects subcategory and the ID is saved.

    For option 3, the cascading comboboxes serve as filter/search tools to locate desired Category_Subcategory record on the main form.

    Options 2 and 3 are form/subform arrangements and subform container control Master/Child Links properties will synchronize related records and automatically save the FK ID.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Thank you. I have opted for Option 2. I am still having a problem with the slave combo box in the subform. When running the query for the row source, I am always prompted for the value of the expression in Criteria. If I put the value in manually it works. It seems that I have the wrong expression for Criteria... Access can't seem to find the CategoryID from the master combo box.

    Click image for larger version. 

Name:	accessproblem.jpg 
Views:	7 
Size:	57.3 KB 
ID:	16514

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Both comboboxes are on the subform? You are saving CategoryID into Categories_Subcategories?

    RowSource for Categories combobox, named cbxCat:

    SELECT ID, Categories FROM Categories ORDER BY Categories;

    RowSource for SubCategories combobox:

    SELECT ID, SubCategory FROM Categories_Subcategories WHERE Category = [cbxCat] ORDER BY SubCategory;

    You need to be aware that cascading comboboxes with alias don't work nicely in Continuous or Datasheet View forms. I have used cascading comboboxes only once for data entry and the form was set up in Single View with code to refresh the second combobox in the form OnCurrent event as well as the first combobox AfterUpdate event.

    I am beginning to think you might be better off using just a single combobox that lists the subcategories and the ID from Categories_Subcategories table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Lookup values instead of ID numbers
    By JustAnElf in forum Queries
    Replies: 3
    Last Post: 11-04-2013, 01:00 PM
  2. duplicate lookup values
    By andy33 in forum Database Design
    Replies: 2
    Last Post: 11-18-2011, 03:07 PM
  3. Restricting values in combo boxes
    By Remster in forum Access
    Replies: 4
    Last Post: 09-08-2010, 12:24 PM
  4. Lookup Values
    By jbarrum in forum Access
    Replies: 6
    Last Post: 12-31-2009, 08:58 AM
  5. Lookup Values
    By gjw1012 in forum Access
    Replies: 5
    Last Post: 07-22-2009, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums