Creating the Table at Design Time

One word of warning before approaching this task: You must be well versed in the TTable class before putting this information to work in a production situation. You are going to be defining every aspect of a Table without the benefit of the Desktop, which masks a lot of this from you and automates many operations. The properties, methods, and events of the Dataset object should be reviewed to ensure that all of the values that you are going to define are appropriate for the object that you want to create.

To explore the design-time creation of a table, we'll approach this in a step-by-step method, creating a sample project as we go along.

Start a new application and drop a Table component on the form.

The first order of business is to assign a Database alias to the DatabaseName property. This is the database in which the new table ; will be a member. Use DBDEMOS for this example by selecting it for the DatabaseName property's value. 1

The table must have a name, which will be assigned through the ^ TableName property Call this table CDCOLL. If the table is going to t be of a database type other than Paradox or dBASE, you must include the appropriate file extension in the string you pass as the table name.'0%

All tables must have a table type that defines the database structure of the object. If the database is going to be something other than ASCII, Paradox, or dBASE, set the TableType property to ttDefault. Otherwise, select the appropriate type from the drop-down list. For the sample that we are building, set this property to ttDBase.

The fields for the table need to be defined. Double-click on the FieldDefs property to call up the collection editor. The process being described here will need to be repeated for each field in the table. The schema that we will use to define the CDCOLL table is shown in Figure 8.8. Click the Add New button and add the data shown for each field.

Column Name Data Type Size

ARTIST

RATING

String

Numeric

Figure 8.8 The schema of the CDCOLL table

When you have completed the definition of the table you will use the Create Table menu option. Right-click on the Table component to invoke the context menu and select Create Table. A physical representation of the table will be created in the background if everything has been defined correctly.

The table definition is complete at this point but one more item should exist with nearly all of your tables, an index. We will define a primary key for this table to keep it sorted and to meet the relational standard. Double-click on the IndexDefs property to start the collection editor again. Click the Add New button to define a new index. Name the index TITLE and the Fields property Title. Click on the Options property to see the properties listed below it. Set the ixPrimary and ixUnique properties to True. Close the collection editor and right-click on the Table again. This time, use the Update Table Definition command to modify the table definition.

Test your table by adding a DataSource and a DBGrid to the form. Set the Active property of the Table to True and execute the application. Try entering a few CDs, making sure that the index is working by inserting them randomly and making sure that they .insert into the correct spot.

Was this article helpful?

0 0

Post a comment