I am trying to get a recordset containing a distinct list of all User groups in the userGroups table. If the user_id making the query belongs to any of the groups, flag the 'checked' field as '1', if not flag the 'checked' field as '0'. User_id may belong to none or multiple userGroups.
The query result is to show a list of userGroups with each having a check box showing whether or not the user_id is in the userGroup. This user would be in both the Manager and Admin userGroups.
#user_id# is the user making the query and passed into the query.
Example 1:
The example above is not correct and returns a non-distinct userGroups list with checked values of 0 and 1.
Example 2:
This attempt results in a distinct list of the four userGroups, but with all checked = 0.
Any suggestions on where I am going wrong?
Thanks!
The query result is to show a list of userGroups with each having a check box showing whether or not the user_id is in the userGroup. This user would be in both the Manager and Admin userGroups.
Code:
UserGroup_id | UserGroup | Checked
1 | Staff | 0
2 | Manager | 1
3 | Admin | 1
4 | Guest | 0
Code:
ACCESS 2000
Tables:
users
----------
user_id | user_name
userGroups
-------------------------------
userGroup_id | userGroup
user_userGroups
--------------------------
user_id | userGroup_id
#user_id# is the user making the query and passed into the query.
Example 1:
Code:
SELECT DISTINCT UserGroups.UserGroup, UserGroups.UserGroup_ID, IIf([users_UserGroups.user_id]=#user_id#,1,0) AS checked
FROM UserGroups LEFT JOIN Users_UserGroups ON UserGroups.UserGroup_ID = Users_UserGroups.UserGroup_ID
ORDER BY UserGroups.UserGroup;
Code:
UserGroup_id | UserGroup | Checked
1 | Staff | 0
2 | Manager | 0
2 | Manager | 1
3 | Admin | 0
3 | Admin | 1
4 | Guest | 0
Example 2:
Code:
SELECT DISTINCT UserGroups.UserGroup, UserGroups.UserGroup_ID, IIf(Exists (SELECT Users_UserGroups.User_ID, Users_UserGroups.UserGroup_ID FROM Users_UserGroups
WHERE Users_UserGroups.User_ID=#user_id# AND Users_UserGroups.UserGroup_ID= UserGroups.User_ID),"1","0") AS checked
FROM UserGroups LEFT JOIN Users_UserGroups ON UserGroups.UserGroup_ID = Users_UserGroups.UserGroup_ID
ORDER BY UserGroups.UserGroup;
Code:
UserGroup_id | UserGroup | Checked
1 | Staff | 0
2 | Manager | 0
3 | Admin | 0
4 | Guest | 0
Any suggestions on where I am going wrong?
Thanks!