geekypenguin
geekypenguin

Reputation: 63

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:

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 | 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. I have also set a relationship between the two user ID fields but this hasnt worked either.

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.

Thanks in advance

Upvotes: 0

Views: 1509

Answers (2)

Zack
Zack

Reputation: 2341

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:

  • Users(ID, Name) <-- this would be the same as what you have now.
  • 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: enter image description here

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: enter image description here

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). But, like I said, you'll be better off changing your structure if you can.

Upvotes: 3

Lee Mac
Lee Mac

Reputation: 16025

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.

This way, no event handlers are required and the displayed records will be changed automatically.

Upvotes: 0

Related Questions