Delphi Community database tutorials, part 4
Improving the data entry


Sorting the records

In order to have the UFO sightings sorted by field "ID", select cdsUfo in the Design view.
In the Object Inspector, next to IndexFieldNames, type: ID

Data entry

Entering data directly in the DBGrid is not very safe, because the data is not verified before storing it. Let's improve on this.

Firstly, we don't want the user to change data directly in the grid:
in the Object Inspector, set property AutoEdit of dsUfo to False.

Next, we add a panel with a few data controls.

  1. Drop a TPanel somewhere below the DBGrid.
    Set its property Align to alBottom.
  2. Drop a DBMemo on this panel.
    In the Object Inspector, set its property DataSource to dsUfo and set property DataField to NOTES.
  3. Below the DBMemo, drop another panel. Set its Name to panEdit and the property Visible to False.
  4. Drop 13 TDBEdit components onto the panel panEdit.
    Set the property DataSource of all these edit-boxes to dsUfo.
    Set the other properties as follows:

    ComponentNameDataField
    TDBEdit1edIDID
    TDBEdit2edDescDESC
    TDBEdit3edLocationLOCATION
    TDBEdit4edCountryCOUNTRY
    TDBEdit5edCECE
    TDBEdit6edSourceSOURCE
    TDBEdit7edDuratDURAT
    TDBEdit8edWitWIT
    TDBEdit9edShapeSHAPE
    TDBEdit10edAppearAPPEAR
    TDBEdit11edSizeSIZE
    TDBEdit12edOccOCC
    TDBEdit13edLink2LINK2
  5. Below the DBEdits, add two buttons. Name the first one btnSave and the other one btnCancel.
  6. Add OnClick event handlers for the buttons:
    procedure TForm1.btnSaveClick(Sender: TObject);
    begin
      cdsUfo.Post;
    end;
    
    procedure TForm1.btnCancelClick(Sender: TObject);
    begin
      cdsUfo.Cancel;
    end;

additional components

Let's write some source code, using the ClientDataSet's events.

  1. Firstly, we write a procedure that handles the enabling or disabling of certain components, depending from the fact if we are in the course of modifying a record, or just browsing the table. Without this procedure, we would have to repeat almost the same code in 4 places.
    In the interface section, under Private declarations, add the following line:
    procedure UpdateGUI(Modify: Boolean);
  2. In the implementation section, add the following procedure:
    procedure TForm1.UpdateGUI(Modify: Boolean);
    begin
      DBGrid1.Enabled := not Modify;
      DBNavigator1.Enabled := not Modify;
      edSearch.Enabled := not Modify;
      btnSearch.Enabled := not Modify;
      btnWeb.Enabled := not Modify;
      panEdit.Visible := Modify;
      if Modify then
        edID.SetFocus
      else
        DBGrid1.SetFocus;
    end;
    
  3. Select dsUfo. In the Object Inspector, tab Events, double click next to BeforeEdit.
    Complete the new event handler as follows:
    procedure TForm1.cdsUfoBeforeEdit(DataSet: TDataSet);
    begin
      UpdateGUI(True);
    end;
  4. Likewise, create an event handler for BeforeInsert and complete the code:
    procedure TForm1.cdsUfoBeforeInsert(DataSet: TDataSet);
    begin
      UpdateGUI(True);
    end;
  5. For the event AfterCancel:
    procedure TForm1.cdsUfoAfterCancel(DataSet: TDataSet);
    begin
      UpdateGUI(False);
    end;
  6. For the event AfterPost:
    procedure TForm1.cdsUfoAfterPost(DataSet: TDataSet);
    begin
      DataChanged := True;
      UpdateGUI(False);
    end;

Part 3 Database Tutorials - Part 3 Database Tutorials - Part 5 Part 5


Crash Course Delphi  Database tutorials Delphi Starter  FAQ  Tips  Source Code  Downloads  Links