Insert New Record Action

image\Action_Database_Insert_Record.jpg

The Insert New Record action is used to add a new record to a table in a database. This option is particularly useful if you want to store information about a user (such as: quiz results; pages visited; time spent in the publication, etc.) or information entered by the user (such as: name; address; email address; comments, etc.) to a database. Furthermore, this means you can use Opus to create attractive interfaces for data-entry personnel to populate your existing databases.

Note:
This action is used to insert a new record in a table of a database. If you want to update an existing record in a table, use Update Current Record action.

Important Note!

This action requires you to enter a Data Source Name (DSN) and table name – see Connecting to a Database for more information. This action does NOT use a query created with the Database Query action.

Setting up the Insert New Record action:

1. Add the Insert New Record action to your trigger, as described in adding actions. The Insert New Record tab will automatically appear when you add the action.

2. Click on the Select… button to create a new DSN – this will open the Select Data Source dialog box. Alternatively, click on the down arrow to the right of the DSN box to list all of the DSN’s that have previously been used in this publication.

Note:
Once you have created a DSN on your machine, you can select it from the Select Data Source dialog box. However, if you have not created a DSN for your database already, you will have to create it before you can proceed with this action – see Connecting to a Database for more information.

3. Use the Table option to select the table from the database you have just connected to in point 2 above in which you want to insert a new record. Click on the down arrow to the right of the Table box to list all of the tables in the database. Once you select a table name the Field / Set To panel below this option is activated – this will show the fields of the table in the database in the Field column and the Set To column will be blank.

Note:
If the drop down box is empty, there are either no tables in the database you have connected to or there has been a problem with the connection (e.g. the DSN may be invalid). You must have created at least one table in your database – the table does not have to contain any existing records.

4. Use the Field / Set To panel to insert a value for each field of the new record. To set the contents of a field, select the field name in the Field column and click on one of the following radio buttons: String, Variable, Null and Don’t Set (Use Database Default). Repeat this exercise for each field listed in the Field column. By default, each field has no initial value in the Set To column.

5. Use the String option to type a new value for the selected field. Any letter, number, word or phrase can be typed into this field.

Note:
The name tag String is context sensitive. This means the name tag will change depending on what the field in the table of the database is allowed to contain (e.g. if the field can only contain numbers then this name tag will appear as Numbers).

6. Use the Variable option to copy the contents of an Opus variable into the field of the current record. Click on the down arrow to the right of the Variable box to display a list of all the variables in the current publication.

Note:
This option is useful when you want the value a user has entered to be stored in the field. For example, you could create an Input Text object on the page – this automatically saves whatever the user types into a specified variable – this variable could be used here in this action.

7. Use the Null option when you want the entry for the field to contain nothing (i.e. a blank entry).

8. Use the Don't Set (Use Database Default) option when you want the database to determine the entry for this field.

Note:
This option is particularly useful for the Primary Key field. A primary key is a unique way of identifying a record in a table (i.e. two records cannot have the same primary key). We recommend that you allow the database to determine the primary key for each record by using this option for the field you have set as the primary key.

This option is also useful if you have set other fields in your database to have default entries because it is a quick method of ensuring consistent and accurate entry.

9. Remember, for each field that is listed in the Field column, you must select one of the radio buttons described in points 5 to 8 above.

10. Click on the Apply button to save your changes.

Note:
This action will NOT ask for confirmation before it inserts a new record. To ensure a record is not accidentally inserted, we recommend that you create an If or Loop action that requires a user response of Yes or No before using the Insert New Record Action.

Related Topics:

Working with Databases - Overview

Introducing the Database Actions

Showing Records from a Database in Opus

Choosing an Action