Audit Salary Changes (IBX General Tutorial)

From RAD Studio
Jump to: navigation, search

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

Add a dialog to monitor employee salary changes using the TDataModule created in Add a Data Module.

When the components are added, the form looks like this:

SalaryChangeAuditing.png


Create the Form

Add another VCL Form to the project. In the Project Manager, right-click the project name and click the menu item Add New > VCL Form from the context menu.

Adjust the new form's properties:

Save the form:

  • For Delphi, save the file as Frmtrigg.pas.

To make use of the TDataModule in this form:

  • For Delphi, add the following line at the beginning of the implementation section of Frmtrigg.pas:
uses DmCSDemo;

Add Visual Components

You do not need to add any database components, because you will use the database components in the "DmCSDemo" TDataModule.

  • Add three TPanel components, positioning them as shown in the figure. These TPanels provide a framework for positioning the visual components. Resize them to fill the form.

SalaryFrmPanels.png

  • Add a TDBNavigator to "Panel1" to navigate database records. Set DataSource to "DmEmployee.EmployeeSource" from the drop-down menu. Note that this data source is furnished by the TDataModule. Under the VisibleButtons property, check all but these buttons: nbInsert and nbDelete. Set ShowHint to true, so that hints for the TDBNavigator tool buttons are displayed.
  • Place a TBitBtn on the right side of "Panel1". Set the following properties:
    • Set Caption to "E&xit", so that pressing ALT+x is the same as clicking the button.
    • Set Kind to "bkClose" from the drop-down menu. This setting makes the button execute a command to close the dialog when you click the TBitBtn.
  • Place a TDBGrid on "Panel2". Resize it to fill the TPanel. Set DataSource to "DmEmployee.EmployeeSource" from the drop-down menu.
  • Place another TDBGrid on "Panel3". Resize it to fill the TPanel. Set DataSource to "DmEmployee.SalaryHistorySource" from the drop-down menu.

Add Event Handler

Add an event handler for the form. When the form is selected, in the Object Inspector on the Events tab, double click the OnShow event. Use this code for the event:

procedure TFrmTriggerDemo.FormShow(Sender: TObject);
begin
  DmEmployee.EmployeeTable.Open;
  DmEmployee.SalaryHistoryTable.Open;
end;

Display the Table Viewing Form

Modify the main form created in Create Main Form. Add a button on that form to display the form you just created.

Delphi

Add an additional line to the uses clause at the beginning of the implementation section of Frmmain.pas, so the main form knows about the FrmTrigg unit you just added:

uses
   FrmTrigg,   { The Trigger Demo }
   FrmViews;   { The View Demo }

Add another TButton to the Frmmain form, similar to the TButton that was added in Create Main Form. Set TButton.Caption "Salary Change &Trigger Demo". Set TButton.Name to "BtnTrigg". Then add an event handler for the new button by double-clicking that TButton in the Design tab of Frmmain.pas. Use the following code for the event handler:

procedure TFrmLauncher.BtnTriggClick(Sender: TObject);
begin
  FrmTriggerDemo.ShowModal;
end;

Run the Application

Build and run the application. The main form displays:

MainFrmAudit Salary.png


Click the new "Salary Change Trigger Demo" TButton to display the new form:

Salary Change Auditing Frm.png


Select an employee in the top TDBGrid, which shows data from the EMPLOYEE table. Change the employee's salary, then click the Post edit button (check mark) in the TDBNavigator to update the database. After you do this, the lower TDBGrid, which shows the SALARY_HISTORY table, should refresh to show this change.

SalaryChangeAuditingPost.png


This table is refreshed, because there is an event handler in the "DmCSDemo" TDataModule: TDmEmployee.EmployeeTableAfterPost. This event occurs after there is a post on the "EmployeeTable" TIBDataSet in the "DmCSDemo" TDataModule. TDmEmployee.EmployeeTableAfterPost refreshes the "SalaryHistoryTable" TIBDataSet if it is active, resulting in the refresh you observed.

Previous

Add a Data Module

Next

Get Employee Project Assignments with a Stored Procedure