Defining comboboxes

In this chapter we will take a closer look at the combobox object. For this exercise we create a new form in which we can write reviews for the plays, books etc. held in our database. Create the form using the field definitions as shown in the following table.

First we need to define a new table called "review", which will be the basis for our new form.

Table 6.1. table structure of the table "review"

FieldnameTypeDescription
review_idauto incrementunique identification number for each review
author_idintegerauthor identification number
literature_idintegerliterature identification number
reviewtextmemothe review text

Now we create a new form called "reviewform1" based on the "review" table. Add two comboboxes (you will find the detailed descriptions of the properties of the comboboxes in the following text), two labels, two buttons and a memofield to the form as shown below. Connect the memofield with the column "reviewtext" and improve the buttons.

Figure 6.9. The form "reviewform1"

The form "reviewform1"

A simple combobox

The "author" combobox shows the "author_id" column of the "review" table. The column holds the numeric value, but what we want to select is the name of the author as defined in the "author" table.

For this we will set the values as shown in the figure below. Three extra fields are used for a combobox:

  • listdatasource: this is the datasource that contains the master list

  • listcolumn: the column in the listdatasource which contains the key value for the column field. Its data will be written in the column field.

  • viewcolumn: name of the column in the listdatasource that contains the values that we want to display on the screen

Figure 6.10. A simple combobox

A simple combobox

The "author_id" field in the "authors" listdatasource has an equivalent field in the "review" datasource, called "author_id". Instead of displaying the identification numbers we would rather see the author's names, so we set the viewcolumn to "name". As you can see in Figure 6.11, “The form datasource dialog(3)”, the formdatasource for the listfield contains nothing extraordinary.

Figure 6.11. The form datasource dialog(3)

The form datasource dialog(3)

A complex combobox

The "Literature" combobox should list the literature written by the author who is selected in the "Author" combobox and should immediately update its contents when a new author is selected. This requires a little bit more thought and planning.

Figure 6.12. A complex combobox

A complex combobox

We need to access the "literature" table and set the "review" table as being the master datasource. This is so far "business as usual". If this is all you set you will find that the "Literature" combobox will only update its contents when you change the "Author" combobox and store the changes. To modify this behaviour, click the "react on master changes?" checkbox in the datasource (should now contain an "x").

Figure 6.13. The form datasource dialog(4)

The form datasource dialog(4)

Now it almost works as designed. But when you try to delete a row in the "review" form you get prompted that you can't do this due to row dependency. To modify this behaviour you can set different depending modes. In this case all we need to do is select "no handle" for "depending mode". That's all.

However, if you enter a review and save it, you will discover that although selecting an author selects his works, the same review comment is displayed. That is clearly not satisfactory. It is left as an exercise for you to create the reviewform2 shown below. This form is based on the literature table, for it is the author's work that is to be reviewed. The literature combo has Combomode: Selector; Listdatasource: literature and Viewcolumn: title. The datasource of the author lineedit is authors depending on literature. The datasource of the memofield is review depending on literature.

Figure 6.14. The form "reviewform2"

The form "reviewform2"

Figure 6.15. The depending modes

  • standard - cannot delete a master row when there are depending rows in the depending datasource and cannot change the value of a master connection field.

  • nohandle - allows changes and deletion, but ignores changes to the key fields

  • change - if a master connection field is changed, the equivalent depending fields will change their values too.

  • delete - if the master row is deleted then the depending rows will also be deleted

  • change+delete - combination of the change and delete modes