Saturday, November 19, 2016

How To Manipulate Database Delphi Access

How To Manipulate Database Delphi Access


Good morning firends, previously I have posted an articel about "How To Make A Simple Database Application". But in that articel I just explain how both delphi and ms access could be connected. Now I want to explain "How To Manipulate Database Delphi & Access".

In previous articel we have made form, like this picture :

How to manipulate database with delphi & Ms Access | Sofi Rahmatulloh

  • Save is used to fill table, with new database.
  • Update is used to edit old database in the table.
  • Search is used to find field which name is same at esearch.Text.
  • Delete, is used to delete field at the table.

Now, make the event save button.
This is the code :
procedure TForm1.btn1Click(Sender: TObject);
begin
tbl1.Append;
tbl1[NIM] := enumb.Text;
tbl1[NAMA] := ename.Text;
tbl1[ALAMAT] := eaddress.Text;
tbl1[PRODI] := edept.Text;
tbl1.Post;
end;

Then we want to make update funtion. First make event at DBGrid.CellClick, In other to show you the excited data.
This is the code :
procedure TForm1.dbgrd1CellClick(Column: TColumn);
begin
enumb1.Text := dbgrd1.Fields[0].AsString;
ename1.Text := dbgrd1.Fields[1].AsString;
eaddress1.Text := dbgrd1.Fields[2].AsString;
edept1.Text := dbgrd1.Fields[3].AsString;
end;

Second, make the event at update button. This is the code :
procedure TForm1.btn2Click(Sender: TObject);
begin
tbl1.Edit;
tbl1[NIM] := enumb1.Text;
tbl1[NAMA] := ename1.Text;
tbl1[ALAMAT] := eaddress1.Text;
tbl1[PRODI] := edept1.Text;
tbl1.Post;
end;

Then we make search funtion. On button search.click . Like this code :
procedure TForm1.btn3Click(Sender: TObject);
begin
tbl1.Locate(NAMA,ename1.Text,[]); //search by name.
end;

Then finally we make delete funtion. The event is on button delete click. Like this code :
procedure TForm1.btn4Click(Sender: TObject);
begin
tbl1.Delete;
end;

My complete code :
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;

type
  TForm1 = class(TForm)
    enumb: TEdit;
    ename: TEdit;
    eaddress: TEdit;
    edept: TEdit;
    btn1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    enumb1: TEdit;
    enama1: TEdit;
    eaddress1: TEdit;
    edept1: TEdit;
    btn2: TButton;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    esearch: TEdit;
    Label9: TLabel;
    btn3: TButton;
    btn4: TButton;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    con1: TADOConnection;
    tbl1: TADOTable;
    ds1: TDataSource;
    dbgrd1: TDBGrid;
    procedure btn1Click(Sender: TObject);
    procedure dbgrd1CellClick(Column: TColumn);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
tbl1.Append;
tbl1[NIM] := enumb.Text;
tbl1[NAMA] := ename.Text;
tbl1[ALAMAT] := eaddress.Text;
tbl1[PRODI] := edept.Text;
tbl1.Post;
end;

procedure TForm1.dbgrd1CellClick(Column: TColumn);
begin
enumb1.Text := dbgrd1.Fields[0].AsString;
ename1.Text := dbgrd1.Fields[1].AsString;
eaddress1.Text := dbgrd1.Fields[2].AsString;
edept1.Text := dbgrd1.Fields[3].AsString;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
tbl1.Edit;
tbl1[NIM] := enumb1.Text;
tbl1[NAMA] := ename1.Text;
tbl1[ALAMAT] := eaddress1.Text;
tbl1[PRODI] := edept1.Text;
tbl1.Post;
end;

procedure TForm1.btn3Click(Sender: TObject);
begin
tbl1.Locate(NAMA,ename1.Text,[]);
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
tbl1.Delete;
end;

end.



 Oke its all my articel today.  How To Manipulate Database Delphi & Access.


Available link for download

No comments:

Post a Comment