{Header}
This example shows how to implement a sample record form that allows users to assign multiple employees to a project using a multi-select ListBox. This is an example of a many-to-many relationship whereby one project can have many employees and one employee can have many projects.
To assign employees to an existing project, select a project from the list to the left then select multiple employees from the multi-select ListBox. You can select multiple employees by holding the CTRL key then clicking on each employee you want to add to the selection. When done, click “Submit” to save the selection.
To add a new project and assign employees to it, click the “Add New” link then in the 'Add/Edit Project' form, type in a name for the new project then select the employees you want to assign to the project. When done, click "Submit" to save the new record.
Database: Intranet
Tables: projects, employees, projects_employees
The record form’s Before Show event is used to retrieve the existing database values and select the appropriate values for the multi-select ListBox before the page is displayed.
The ProjectEmployeesModify function is used to simplify the updates performed by the events below.
The record form’s After Update event is used to retrieve the ListBox values and update the many-to-many relationship table, in this case projects_employees.
The record form’s After Insert event is used to obtain the last inserted key and update the many-to-many relationship table when a new record is added by a user.
Note: you can use several methods to retrieve the value of the last key inserted into the database, which is the current project_id. Although many databases have appropriate functions for retrieving the last inserted key within the current session, some databases do not support this. As such, you can simply retrieve the highest key value in the table and assume that it is the key last inserted by the current user. This method can be improved to be more reliable, though this is not the purpose of this example.
The record form’s Before Delete event is used to delete records from the many-to-many relationship table whenever a project is deleted.