Let’s link the tables

In this chapter we’ll see how to take information from one table and insert it into another. Specifically, in our example, we’ll change the Orders table so that it picks up the customer and lets us add items.

If we haven’t already, let’s create a customer so we have some data to display. This isn’t required to link the tables; it’s only so we can immediately see the result of our change.

Now that we have a customer and an item, we can start changing the Orders table by creating a new field in it.

Let’s go to the options and select the Fields page. Click Add, set the right table (in this case Orders) and in Field type choose Subtable. This type of field lets you select data from another table and automatically fill in the fields of the current form according to the mapping rules. Remember that the data is copied, not linked. For example, if a customer’s name is changed, it won’t be updated automatically in their orders.

Creating a Subtable field

In the Field label parameter, enter Customer.

In the Main table parameter, choose the Customers table. This tells the field where to take the data to display and insert into the form.

Finally, we set up the mappings to tell the field which values to retrieve and which fields to put them in.

Click Set next to the Main mappings parameter to open the management window.

Field mapping window

Click Add to insert a new mapping row and select the value [customer] under the Orders fields column.

Then move the mouse to the Name field in the Fields grid of the Customers table, and double-click it to insert it automatically. This tells the program that, once we’ve selected a row from the Subtable, it must take the name and write it automatically into the Customer field.

Then click Ok to confirm.

Save, close the window and try creating a new order.

As you can see, the field appears on the Detail page.

In the next chapter we’ll see how to arrange the layout.

The Customer field in the order form

Opening the field’s drop-down, you’ll see the list of all the customers created so far. Pick a row with the mouse and the program automatically inserts the customer’s name into the field.

We’ve linked our first two tables, and now we’ll do the same for the items to use in our orders. What we want, as we did for the Customer, is for the Code field in the document rows to be filled in automatically when an item is selected.

Again, on the Fields page of the Options, click Add and create our Subtable, which will read data from items and insert it into the orders_rows table. Fields are always created within the “space” where they are used.

Subtable field for items

As with the other Subtable, after setting the table the data is taken from, we set up the mappings.

Mappings for the items subtable

Click Ok and save the field.

I recommend going to the Actions button in the Options and clicking Refresh Program every time you change the database. This reloads and applies all the settings.

Now, when we create a new order and add rows to the main grid, we can select an item from the drop-down.

Selecting an item in the order rows

In this case, since the window is still empty, we can create a new item directly from the drop-down.

We’ve created the main links, and now we can add two fields to manage a description both in the item and in the order rows.

1. Create a “description” field of type Text in the items table.

2. Create a “description” field of type Text in the orders_rows table.

Now let’s edit the “code” field of the orders_rows table: just double-click its row or use the pencil button.

Click Set next to the Main mappings parameter and add a new row, entering “description” in both columns.

Mapping the description field

Save everything and try creating a new order. Now, when you add a row to the main grid and select an item, both the code and the description are filled in automatically.