简体   繁体   English

根据组合框内容选择MS Access表单中的记录

[英]Selecting records in MS Access form based on combo box contents

I have an access database with two tables, one for users and the other for user permissions. 我有一个包含两个表的访问数据库,一个表用于用户,另一个表用于用户权限。

The users table has a list of user names with a unique ID: users表包含具有唯一ID的用户名列表:

ID | Name
1  | Joe Bloggs
2  | Steve Smith
3  | A Another

and the permissions table has a list of user IDs and the zones theyre allowed to access: 权限表中包含用户ID及其允许访问的区域的列表:

ID | Zone1 | Zone 2
1  | X     |
2  |       | X
3  | X     | X

I have created a form for updating the user's permissions based on the permissions table which all works fine stepping forward and back manually, and on this form I have a combo box that lists all the users from the first table. 我创建了一个用于基于权限表更新用户权限的表单,该表在手动前进和后退时都可以正常工作,在此表单上,我有一个组合框,其中列出了第一个表中的所有用户。

My question is, how do I make this navigate to the record in the permissions table? 我的问题是,如何使它导航到权限表中的记录? I know i need to put some code in the "after update" field for the combo box, and managed to get it to work when the combo box was displaying the User.ID field but not User.Name. 我知道我需要在组合框的“更新后”字段中放入一些代码,并在组合框显示User.ID字段而不显示User.Name时设法使其工作。 I have also set a relationship between the two user ID fields but this hasnt worked either. 我还设置了两个用户ID字段之间的关系,但这都没有起作用。

In Summary, I want to select "A Another" from the username drop down box, this has an ID of 3 in the users table, and then navigate to record ID 3 from the permissions table. 在摘要中,我想从用户名下拉框中选择“ A Another”,它在用户表中的ID为3,然后导航以从权限表中记录ID 3。

Thanks in advance 提前致谢

First off, your table structure is not well normalized or structured. 首先,您的表结构没有很好的规范化或结构化。 If you have multiple zones that users may have permissions for, it's inefficient to store each zone-permission as a column. 如果您拥有用户可能拥有权限的多个区域,则将每个区域权限存储为一列效率不高。 It's hard to get information about zones a user may have access to (zone metadata), and adding a new column for each additional zone can greatly increase your database size and make queries troublesome. 很难获得有关用户可能访问的区域的信息(区域元数据),并且为每个其他区域添加新列会极大地增加数据库大小并给查询带来麻烦。

A better approach would be to have 3 tables: 更好的方法是拥有3个表:

  • Users(ID, Name) <-- this would be the same as what you have now. Users(ID, Name) <-这与您现在拥有的相同。
  • Zones(zone_id, additional_zone_attributes)
  • user_zone_permissions(user_id, zone_id, additional_permission_attributes)

With this structure, you could still use @LeeMac's answer of using a sub-form: The parent form would list user details, and the sub-form would list zone permissions: 通过这种结构,您仍然可以使用@LeeMac的使用子表单的答案:父表单将列出用户详细信息,而子表单将列出区域权限: 在此处输入图片说明

With this structure, it would also be trivial to add additional privilege attributes (maybe down the line you want to add effective dates of the privilege). 使用这种结构,添加其他特权属性(可能要在要添加特权的有效日期的行下方)也很简单。

Just make sure to set your primary keys and foreign keys (using the Relationships) screen appropriately: 只需确保正确设置主键和外键(使用“关系”)屏幕即可: 在此处输入图片说明

Now , after all that is said, if you can't refactor your structure, you don't really need to use sub-forms at all. 现在 ,总而言之,如果您无法重构结构,则实际上根本不需要使用子表单。 Since your users and permissions table seem to have a one-to-one relationship, you can store everything on one form (either putting everything in one table, or tying the form to a query that combines the data). 由于您的userspermissions表似乎是一对一的关系,因此您可以将所有内容存储在一个表单中(将所有内容都放在一个表中,或将表单绑定到合并数据的查询中)。 But, like I said, you'll be better off changing your structure if you can. 但是,就像我说的那样,如果可以的话,最好改变一下结构。

Insert a subform in your form with the Master Field set to the ID in your Users table, and the Child Field set to the ID in your Permissions Table. 在您的窗体中插入一个子窗体,其中“ 主字段”设置为“ Users表中的ID,“ 子字段”设置为“ Permissions表中的ID。

This way, no event handlers are required and the displayed records will be changed automatically. 这样,不需要事件处理程序,并且显示的记录将自动更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM