Вы находитесь на странице: 1из 4

Praktikum Database Delphi 1.

Membuat Tabel Jalankan Delphi, buat satu tabel melalui Database Desktop (dari menu Tools, Database Desktop) dengan nama mhs.db. Struktur tabelnya sebagai berikut:

2. Rancangan Form

Aturlah properti dari tiap object:


Nama Form1 Label1 Label2 Label3 Label4 Label5 ComboBox1 Properti Caption Position Caption FocusControl Caption FocusControl Caption FocusControl Caption FocusControl Caption Name Nilai Data Mahasiswa poScreenCenter &N I M Edit1 Na&ma Edit2 &Tanggal Lahir DateTimePicker1 &Kode Jurusan ComboBox1 Jurusan ComboBox1

Items Button1 Button2 Button3 Button4 Table1 Caption Caption Caption Caption DatabaseName TableName Active DataSet DataSource

DataSource1 DbGrid1

MI TK KA &Simpan &Rubah &Hapus &Keluar Path dari tabel mhs.db Contoh D:\Delphi\mhs.db Mhs.db True Table1 DataSource1

Object-object yang tidak terdaftar pada tabel diatas berarti tidak memerlukan perubahan property. 3. Listing Program
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Grids, DBGrids, Db, DBTables; type TForm1 = class(TForm) Label1: TLabel; . . . . . . . . . . . . procedure Button4Click(Sender: TObject); private // Tambahkan kedua procedure buatan dibawah ini procedure Kosongkan; procedure Aktif(OK: boolean); public { Public declarations } // Baris ini sebenarnya bisa dihapus. end; var Form1: TForm1; // Tambahkan kedua baris dibawah ini resourcestring Pesan = 'Yakin mau menghapus "%s" ' +#13+ 'Nama "%s"??'; implementation {$R *.DFM} procedure TForm1.Kosongkan; begin

Edit2.Text := ''; Edit3.Text := ''; DateTimePicker1.Date := Now; ComboBox1.Text := '' end; procedure TForm1.Aktif(OK: Boolean); begin Edit2.Enabled := OK; Edit3.Enabled := OK; ComboBox1.Enabled := OK; DateTimePicker1.Enabled := OK; Button1.Enabled := OK; Button2.Enabled := not OK; Button3.Enabled := not OK; end; procedure TForm1.FormCreate(Sender: TObject); begin Edit1.Text := ''; Aktif(False); Button2.Enabled := False; Button3.Enabled := False; Kosongkan; end; procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); begin if Key = #13 then begin PostMessage(Handle, WM_NEXTDLGCTL, 0, 0); Key := #0; end; end; procedure TForm1.Edit1Enter(Sender: TObject); begin KeyPreview := False; end; procedure TForm1.Edit1Exit(Sender: TObject); begin KeyPreview := True; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if Key = #13 then begin with Table1 do if FindKey([Edit1.Text]) then begin Aktif(False); Edit2.Text := FieldByName('Nama').AsString; DateTimePicker1.Date := FieldByName('Lahir').AsDateTime; ComboBox1.Text := FieldByName('KodeJur').AsString; //Pastikan Event onChange pda ComboBox1 sudah diketik ComboBox1Change(Sender); end else

begin Aktif(True); Kosongkan; Edit2.SetFocus; end; end; end; procedure TForm1.ComboBox1Change(Sender: TObject); var kdjur : string; begin kdjur := UpperCase(ComboBox1.Text); if kdjur = 'MI' then Edit3.Text := 'Manajemen Informatika' else if kdjur = 'TK' then Edit3.Text := 'Teknik Komputer' else if kdjur = 'KA' then Edit3.Text := 'Komputerisasi Akuntansi' else Edit3.Text := 'Tidak Terdaftar'; end; procedure TForm1.Button1Click(Sender: TObject); begin with Table1 do begin if FindKey([Edit1.Text]) then Edit else Append; FieldByName('Nim').AsString := Edit1.Text; FieldByName('Nama').AsString := Edit2.Text; FieldByName('Lahir').AsDateTime := DateTimePicker1.DateTime; FieldByName('KodeJur').AsString := ComboBox1.Text; Edit1.Text := ''; Edit1.SetFocus; Kosongkan; Aktif(False); Button2.Enabled := False; Button3.Enabled := False; Post; end; end; procedure TForm1.Button2Click(Sender: TObject); begin Aktif(True); end; procedure TForm1.Button3Click(Sender: TObject); begin if Application.MessageBox (Pchar (Format (Pesan, [Edit1.Text, Edit2.Text])), 'Konfirmasi', MB_OKCANCEL + MB_ICONQUESTION) = ID_OK then if Table1.FindKey([Edit1.Text]) then begin Table1.Delete; Edit1.Text := ''; Edit1.SetFocus; Kosongkan; Aktif(False); Button2.Enabled := False; Button3.Enabled := False; end; end; procedure TForm1.Button4Click(Sender: TObject); begin Application.Terminate; // Bisa tongji "Close", Ero'-ero' nu lompo mako end; end.

Вам также может понравиться