30 January 2008

Delphi-DB: Form Master Detil

Form Jadwal Mengajar Dosen


Lanjutan dari pembuatan form dosen, dengan penambahan konsep master-detil untuk mengatur jadwal mengajar dosen:
- Buat Form
+ Label: Label1, Label2, Label3, Label4
+ Button: Button1, Button2, Button3
+ TTable: Table1, Table2
+ TDataSource: DataSource1, DataSource2
+ TQuery: Query1
+ TDBNavigator: DBNavigator1
+ TDBGrid: DBGrid1
- Atur Database:
dengan menggunakan Database Desktop, buat database dan tabel nya:
+DatabaseName: dbQ
+TableName: tDosen, tJadwal
+ Pada tabel tJadwal, buat sebuah secondary index sebagai penghubung dengan tabel master ( tDosen):
Table: tJadwal
Index Name: KodeDosenIdx
Secondary Index Field: KodeDosen
- Aturan Mengenai Master-Detil adalah:
+ Tabel Master berupa dosen, tabel Detil berupa jadwal mengajar
+ Secondary Index pada tabel detil tidak bersifat unik, fungsinya untuk menghubungkan dengan primary key pada tabel master, atau berupa foreign key pada tabel detil.
+ Tabel Master berasal dari input bagian komponen Edit
+ Tabel detail digambarkan dalam bentuk grid
+ Isi grid selalu sesuai dengan kode dosen yang dipilih di bagian edit1
+ segala perubahan data disimpan dalam dua tabel, master dan detil
+ Contoh ini tidak menggunakan dasar normalisasi database untuk penyederhanaan konsep

//Kode Lengkap:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids,
ComCtrls;

type
TForm2 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Table1: TTable;
Query1: TQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
Button3: TButton;
DateTimePicker1: TDateTimePicker;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Table2: TTable;
DataSource2: TDataSource;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Kosongkan();
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure DateTimePicker1KeyPress(Sender: TObject; var Key: Char);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
table1.DatabaseName := 'dbQ' ;
table1.TableName := 'tDosen.db';
table1.Active:= true;
datasource1.DataSet:= table1;
Query1.DataSource:=DataSource1;
Query1.Active:=false;

table2.DatabaseName:='dbQ';
table2.TableName:='tJadwal.db';
table2.Active:=true;
table2.MasterSource:=DataSource1;

table2.IndexName:='KodeDosenIdx';
table2.MasterFields:='FKode';


datasource2.DataSet:=table2;
dbgrid1.DataSource := datasource2;
dbNavigator1.DataSource:=datasource2;

label1.Caption:='Kode';
label2.Caption:='Nama';
label3.Caption:='Alamat';
button1.Caption:='Simpan';
button2.Caption:='Hapus';
button3.Caption:='Batal';
edit1.text:='';
Form2.Caption:='Form Input Dosen';
Kosongkan();
end;

procedure TForm2.Button1Click(Sender: TObject);
var
str1:String;
begin
with Query1 do begin
Close;
SQL.Clear;
SQL.Add('SELECT count(*) as jml FROM tDosen WHERE FKode = :f1 ');
Params[0].AsString:=Edit1.Text;
Active:=true;
Open;

if Query1['jml']<>NULL then //Mencegah error apabila tabel masih kosong
str1:=Query1['jml']
else
str1:='0';

Active:=False;
end;
if strtoint(str1)>0 then
begin
table1.FindNearest([edit1.Text]);
table1.Edit;
table1.Fields[1].AsString:=edit2.Text;
table1.Fields[2].AsString:= edit3.Text;
table1.Fields[3].AsDateTime:= DateTimePicker1.DateTime;
table1.Post;
end
else
begin
table1.InsertRecord([edit1.text,edit2.text,edit3.text,DateTimePicker1.Date]);
end;
kosongkan;
edit1.SetFocus;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
table1.FindNearest([edit1.Text]);
table1.Delete;
kosongkan;
end;
procedure TForm2.Kosongkan();
begin
edit2.text:='';
edit3.text:='';
end;
procedure TForm2.Edit1KeyPress(Sender: TObject; var Key: Char);
var
str1:String;
begin
if Key=#13 then
begin

with Query1 do begin
Close;
SQL.Clear;
SQL.Add('SELECT count(*) as jml FROM tDosen WHERE FKode = :f1 ');
Params[0].AsString:=Edit1.Text;
Active:=true;
Open;

if Query1['jml']<>NULL then //Mencegah error apabila tabel masih kosong
str1:=Query1['jml']
else
str1:='0';

Active:=False;
end;
if strtoint(str1)>0 then
begin
table1.FindNearest([edit1.Text]);
edit2.Text:=table1.Fields[1].AsString;
edit3.Text:=table1.Fields[2].AsString;
DateTimePicker1.DateTime:=table1.Fields[3].AsDateTime;
end
else
kosongkan;
edit2.SetFocus;
end;
end;

procedure TForm2.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
edit3.SetFocus;
end;
end;

procedure TForm2.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
DateTimePicker1.SetFocus;
end;
end;

procedure TForm2.DateTimePicker1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
button1.SetFocus;
end;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
kosongkan;
end;
end.

0 °C:

Post a Comment