Show: Delphi C++
Display Preferences

Add a Data Module (IBX General Tutorial)

From RAD Studio
Jump to: navigation, search

Go Up to Tutorial: Using InterBase Express to Access a Database

In this section you create a data module or TDataModule, which is a form that contains nonvisual components your application uses.

Here's a view of the fully populated TDataModule:

TutorialIBXGeneral-DataModulePopulated.png


Once created, you can use this TDataModule in other forms.

Contents

Add the TDataModule

Add the data module to your project by right-clicking the project name in Project Manager then clicking Add New > Other... to display the New Items dialog.

Delphi

Select Data Module and click OK.

TutorialIBXGeneral-AddDataModule.png


Select the new TDataModule. Set its Name property to "DmEmployee". Set OldCreateOrder to true.

Save the new unit:

  • For Delphi, save the unit to DmCSDemo.pas.

Now add components to the new TDataModule.

Add Database and Transaction

Add two fundamental database components: the TIBDatabase and the TIBTransaction.

TutorialIBXGeneral-DBCompEd.png


Note: If you embed the password in the application, as in this example, anyone has access to this database without logging in.
Finish configuring the TIBDatabase by setting the Name to "EmployeeDatabase". Set Connected to true.

Add Stored Procedures

Add two TIBStoredProc components to use stored procedures in the database.

  • Place a TIBStoredProc on the module. Set Name to "ShipOrderProc". Set Database to "EmployeeDatabase" from the drop-down menu. Set StoredProcName to "SHIP_ORDER" from the drop-down menu. SHIP_ORDER is one of the stored procedures in the EMPLOYEE database.
  • Add another TIBStoredProc to the module. Set Name to "DeleteEmployeeProc". Set Database to "EmployeeDatabase" from the drop-down menu. Set StoredProcName to "DELETE_EMPLOYEE" from the drop-down menu.

Add Tables

Add five TIBTable components to the module. Each TIBTable accesses a different database table in the EMPLOYEE database or accesses the table in a different way.

  • "EmployeeLookup" TIBTable
    • Set the Transaction property to "IBTransaction1". This may already be set.
    • Connect this TIBTable to the database by setting its Database property to "EmployeeDatabase" using the drop-down menu.
    • Set the TableName property to "EMPLOYEE" from the drop-down menu.
    • Set the Name property to "EmployeeLookup".
    • Double-click the TIBTable component to display the Fields Editor dialog. Right-click the empty list and select Add all fields from the context menu. Close the Fields Editor.
    • Set IndexFieldNames to "EMP_NO" from the drop-down menu.
    • Set StoreDefs to true.
You use this component to access the EMPLOYEE table in the database.
  • "SalesTable" TIBTable
    • Set the Transaction property to "IBTransaction1".
    • Set Database property to "EmployeeDatabase" using the drop-down menu.
    • Set the TableName property to "SALES" from the drop-down menu.
    • Set the Name property to "SalesTable".
    • Double-click the TIBTable component to display the Fields Editor dialog. Right-click the empty list and select Add all fields from the context menu. Close the Fields Editor.
    • Set IndexFieldNames to "PO_NUMBER" from the drop-down menu.
You use this component to access the SALES table in the database.
  • "CustomerTable" TIBTable
    • Set the Transaction property to "IBTransaction1".
    • Set Database property to "EmployeeDatabase" using the drop-down menu.
    • Set the TableName property to "CUSTOMER" from the drop-down menu.
    • Set the Name property to "CustomerTable".
    • Double-click the TIBTable component to display the Fields Editor dialog. Right-click the empty list and select Add all fields from the context menu. Close the Fields Editor.
    • Set IndexFieldNames to "CUST_NO" from the drop-down menu.
You use this component to access the CUSTOMER table in the database.
  • "EmployeeTable" TIBTable
    • Set the Transaction property to "IBTransaction1".
    • Set Database property to "EmployeeDatabase" using the drop-down menu.
    • Set the TableName property to "EMPLOYEE" from the drop-down menu.
    • Set the Name property to "EmployeeTable".
    • Double-click the TIBTable component to display the Fields Editor dialog. Right-click the empty list and select Add all fields from the context menu. Close the Fields Editor.
    • Set IndexFieldNames to "EMP_NO" from the drop-down menu.
    • In Object Inspector on the FieldDefs property, click the ellipsis (...) button to display a string list editor. Right-click the list of fields and select Select All from the context menu. Press Delete to delete all the entries. Close the dialog.
    • In Object Inspector on the IndexDefs property, click the ellipsis (...) button to display a string list editor. Right-click the list of entries and select Select All from the context menu. Press Delete to delete all the entries. Close the dialog.
You also use this component to access the EMPLOYEE table in the database.
  • "SalaryHistoryTable" TIBTable
    • Set the Transaction property to "IBTransaction1".
    • Set Database property to "EmployeeDatabase" using the drop-down menu.
    • Set the TableName property to "SALARY_HISTORY" from the drop-down menu.
    • Set the Name property to "SalaryHistoryTable".
    • Double-click the TIBTable component to display the Fields Editor dialog. Right-click the empty list and select Add all fields from the context menu. Close the Fields Editor.
    • Set IndexFieldNames to "EMP_NO" from the drop-down menu.
    • In Object Inspector on the FieldDefs property, click the ellipsis (...) button to display a string list editor. Right-click the list of fields and select Select All from the context menu. Press Delete to delete all the entries. Close the dialog.
    • In Object Inspector on the IndexDefs property, click the ellipsis (...) button to display a string list editor. Right-click the list of entries and select Select All from the context menu. Press Delete to delete all the entries. Close the dialog.
You also use this component to access the SALARY_HISTORY table in the database.

Add Data Sources

Add four TDataSource components to the module. The TDataSources are linked to the TIBTables above. A TDataSource component serves as an interface between the TIBTable dataset and data-aware controls.

Add Event Handlers

The final step in constructing the TDataModule is to add a few event handlers.

  • Select the TDataModule. In the Object Inspector, click the Events tab. Double-click the OnCreate event to generate skeleton code for the event. Add the following code for this event:

Delphi

procedure TDmEmployee.DmEmployeeCreate(Sender: TObject);
begin
  EmployeeDatabase.Open;
end;
  • Select the "EmployeeTable" TIBTable. In the Events tab, double-click the AfterPost event. Use the following code for this handler:
procedure TDmEmployee.EmployeeTableAfterPost(DataSet: TDataSet);
begin
  { A change in an employee salary triggers a change in the salary history,
    so if that table is open, it needs to be refreshed now }
  with SalaryHistoryTable do if Active then Refresh;
end;
  • Select the "EmployeeTable" TIBTable. In the Events tab, double-click the BeforeDelete event. Use the following code for this handler:
procedure TDmEmployee.EmployeeTableBeforeDelete(DataSet: TDataSet);
begin
  { Assign the current employee's id to the stored procedure's parameter }
  DeleteEmployeeProc.Params.ParamValues['EMP_NUM'] := EmployeeTable['EMP_NO'];
  DeleteEmployeeProc.ExecProc;          { Trigger the stored proc }
  EmployeeTable.Refresh;                { Refresh the data }
  { Block the EmployeeTable delete since the stored procedure did the work }
  Abort;
end;

This completes the data module, which you will use in the next section. Before moving on, examine the code for this module. Note that numerous objects have been automatically added to the data module class that represent table fields, such as SalesTablePO_NUMBER.

Previous

View Various Tables

Next

Audit Salary Changes

Personal tools
In other languages