{Header}

Updating Many-to-Many Relations via CheckBox List

Description

This example shows how to implement a sample record form that allows users to assign multiple employees to a project using a CheckBox List control. This is an example of a many-to-many relationship whereby one project can have many employees and one employee can have many projects.

Usage

To assign projects to an existing employee, select the employee from the list to the left then select multiple projects by checking the corresponding checkboxes. When done, click “Submit” to save the selection.

To add a new employee and assign projects to him\her, click the “Add New” link then in the 'Add/Edit Employees' form, type in a name for the new employee then select the projects you want to assign the employee to. When done, click "Submit" to save the new record.

Steps to recreate

  1. Create a HTML table with two columns.
  2. In the left cell of the HTML table, use the Grid Builder to create the grid based on the "Employees" table and its two columns: emp_id and emp_name.
  3. Convert the emp_name field to a Link field that points to the current page and add a Link Parameter with the following values: Data Source Type = DataSource Column, Parameter Source = emp_id and Parameter name = emp_id. (Use the Href Source property).
  4. In the bottom row of the Grid, add a Add New Link control and set it's Href Source property to the current page and add the text emp_id into the Remove Parameters property.
  5. In the right cell of the HTML table, use the Record Builder to create the 'Add/Edit Employee' record form with the emp_name field.
  6. Enter emp_id in the Remove Parameters property of the record form.
  7. Insert a new row into the record form and move it below the row with the emp_name control (ALT + Arrow Down ). In the left cell of the new row, enter the text 'Projects' then in the right cell, add a CheckBox List control called ProjectList.
  8. For the CheckBox List, set the Connection property to IntranetDB  and select projects in the Data Source property. Select project_id in the Bound Column property, project_name in the Text Column Property and Integer in the Data Type property. Also set the Source Type property to Code Expression.
  9. Add the appropriate programming code into the Before Show, After Insert, After Update and Before Delete events of the record form as shown in the example.
    Note that you also need to copy the function ProjectEmployeesModify, which should to be added anywhere in the event file, preferably between Events.
  10. Make cosmetic changes as needed, by modifying the text and captions within the HTML.

Database Tables used

Database: Intranet

Tables: projects, employees, projects_employees

Events used

The record form’s Before Show event is used to retrieve the existing database values and select the appropriate values for the CheckBox List 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 CheckBox List 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.