Results 1 to 7 of 7
  1. #1
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    44

    How to set a form's Checkbox.Value to True based on a Junction Table

    Hi all. Simplified scenario here. I have 3 tables: UserT, ItemT and UserXItemJT. The latter being a junction table for the many-to-many relationships between Users & Items. I have an unbound form (ViewUserItemsF) that gets the user name & user_id from the calling form (EditUserF). This form also has 10 checkboxes for the items and all of them are named Item1, Item2, etc. The trailing number corresponds to the Item's PK in the ItemT. When I call the form I would like to read the junction table and enable the corresponding checkbox based on the user to show which items are associated with said user.

    What would be the best way to accomplish this?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,538
    Quote Originally Posted by easyrider View Post
    Hi all. Simplified scenario here. I have 3 tables: UserT, ItemT and UserXItemJT. The latter being a junction table for the many-to-many relationships between Users & Items. I have an unbound form (ViewUserItemsF) that gets the user name & user_id from the calling form (EditUserF). This form also has 10 checkboxes for the items and all of them are named Item1, Item2, etc. The trailing number corresponds to the Item's PK in the ItemT. When I call the form I would like to read the junction table and enable the corresponding checkbox based on the user to show which items are associated with said user.

    What would be the best way to accomplish this?
    If you have a table with fields named Item1, Item2, etc. then it sounds like the db needs to be "normalized".
    Can you post a copy of the db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Another approach would be to use a listbox instead of checkboxes. See

    Many-To-Many, two ways to update Junction table, includes Not In List handling (accessforums.net)

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    In the Load or Current event of the ViewUserItemsF from add something like this (adjust for your names). If you want to update the junction table if the user is checking or unchecking the corresponding checkbox you would need to run some delete\appends to remove or add the records.

    Code:
    Dim i as integer, y as integer
    
    For i=1 to 10
    	y=dCount("*","UserXItemJT","User='" & Forms!EditUserF!txtUser & "' And [PK]=" & i)
    	Me.Controls("Item" & i)=IIF(y>0,True,False)
    Next i
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    44
    Vlad, that worked perfectly, with one small change. It threw a runtime error, #3464 Data Type Mismatch, in the Dcount assignment so I removed the two single quotes in the expression and viola! I appreciate your clean, efficient code. Thank you!

  6. #6
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    44
    Bob, Item1, Item2, etc are not the field names in the junction table. They are the names of the check boxes on the form. Cheers!

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Glad to hear, I usually associate User with some sort of string (text) hence the single quotes but you got it anyway ��!
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 5
    Last Post: 11-10-2022, 10:42 AM
  2. Table True False Checkbox
    By Tuckejam in forum Access
    Replies: 2
    Last Post: 04-30-2020, 04:47 PM
  3. Replies: 3
    Last Post: 03-23-2018, 06:49 PM
  4. Replies: 1
    Last Post: 03-21-2016, 07:32 PM
  5. Replies: 3
    Last Post: 03-08-2013, 11:34 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