Good day. I would like to know how to organize the connection between the tables using the DBGrid component to display from the database.

For example, there are tables "Товар" , structure:

  • [id] key field (auto increment, counter)
  • [nazvanie] product name
  • [id_ed_izm] here is the ID from the unit table

And the table "Units of measurement" `, structure:

  • [id] key field (auto increment, counter)
  • [nazvanie] title

And there is such an entry in the table "Товар" :

 |1|Ручка|1| 

And also "Единицы измерения" :

 |1|Шт.| 

Well, in a toga, if you take the records out of the goods, you get (1 handle 1), but I would like to see (1 pen of a piece), i.e. so that instead of 1, the name from another table is automatically displayed.

The example is simple, but will help for more complex tasks.

  • A Join is not satisfied? - Vahan Av
  • I don’t know why I don’t see the comment here, but I came by mail with the following: “Doesn’t suit Join?” I would like without using SQL, it seems you can establish static links like this (and it is even possible to use the fields as wildcard with a drop-down list in this case), but I don’t know how. In books, ADO is usually used, but I would use DataSource, Table, DBGrid components. - zamachuga

2 answers 2

Working with fields, that for technology ADO, that for BDE, is practically the same, because the field is an independent class (TField). You can link both tables in different ways (to start, open the table fields editor by double-clicking each table, right-click the context menu and select Add all fields):

  1. Specify in the subordinate table (TTable, TADOTable) for which field to bind (MasterFields property) and specify the main table (MasterSource property);
  2. Or in the code editor window go to the Diagram tab, drag the necessary tables from the Object TreeView there, then move the mouse with the left button pressed to the edge of the main table and, without releasing, move to the subordinate table, then the window will open select the fields of the main table (MasterFields) and the fields of the subordinate table (DetailFields), click on Ok - the tables are related.

And to create a wildcard field you need:

  1. Remove the id_ed_izm field from the table by setting the property Visible = false;
  2. Add a new field to this table (call the context menu in the table editor and select the Add New Field item) and specify: FieldType - lookup, Type - string, size - for example, 5, Key fields - id_ed_izm, Lookup keys - id, Data Set - table "Units", Result field - nazvanie. If I did not mess up, it should work.
  • Thank you so much for all your answers. and detailed and abbreviated :) Very helpful. - zamachuga

Create a field of type Lookup in Delphi in your Table and link it to the "Units of Measure" table.