0% found this document useful (0 votes)
67 views10 pages

Nama: Rahmat Darmawan NIM: 5140411186 Jurusan: Teknik Informatika C / Pcs X

This document contains the code for a Delphi application that allows users to manage student data in a database. The application allows users to search for a student by ID number, view all students, add new students, update existing student details, and clear the database grid. The code includes procedures for connecting to the database, executing SQL queries, and handling user input and button clicks to perform CRUD operations on the student data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views10 pages

Nama: Rahmat Darmawan NIM: 5140411186 Jurusan: Teknik Informatika C / Pcs X

This document contains the code for a Delphi application that allows users to manage student data in a database. The application allows users to search for a student by ID number, view all students, add new students, update existing student details, and clear the database grid. The code includes procedures for connecting to the database, executing SQL queries, and handling user input and button clicks to perform CRUD operations on the student data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

NAMA

: RAHMAT DARMAWAN

NIM

: 5140411186

JURUSAN

: TEKNIK INFORMATIKA C / PCS X

1. Tampilan form

2. Run program

3. Input data

4. Pencarian data dengan keypress

5. Tampilkan data

6. Clear DBgrid

Syntax :

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit4: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
dbgrd1: TDBGrid;
edt1: TEdit;
Edit3: TEdit;
Edit5: TEdit;
btn1: TButton;
Button2: TButton;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
ds1: TDataSource;
Button3: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure edt1KeyPress(Sender: TObject; var Key: Char);
procedure btn1Click(Sender: TObject);

procedure Edit1Change(Sender: TObject);


procedure edt1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);

private
{ Private declarations }
public
procedure simpan;
procedure ubah;
procedure cari;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.simpan;
begin
with ADOQuery1 do
begin
Active:=False;
Close;

SQL.Clear;
SQL.Text:='insert into mhs values('+QuotedStr(Edit1.Text)+','+
QuotedStr(Edit2.Text)+','+
QuotedStr(ComboBox1.Text)+','+
QuotedStr(Edit3.Text)+','+
QuotedStr(Edit4.Text)+','+
QuotedStr(Edit5.Text)+')';
ExecSQL;
end;
end;

procedure TForm1.ubah;
begin
with ADOQuery1 do
begin
Active:=False;
Close;
SQL.Clear;
SQL.Text:='update mhs set nama='+QuotedStr(Edit2.Text)+
', jurusan='+QuotedStr(ComboBox1.Text)+
', buku_pinjam='+QuotedStr(Edit3.Text)+
', tgl_pinjam='+QuotedStr(Edit4.Text)+
', tgl_kembali='+QuotedStr(Edit5.Text)+
'where nim='+QuotedStr(Edit1.Text);
ExecSQL;
end;
end;

procedure TForm1.cari;
begin
with ADOQuery1 do
begin
Active:=False;
Close;
SQL.Clear;
SQL.Text:='select * from mhs where nim='+QuotedStr(Edit1.Text)+'';

Active:=True;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);


begin
with ADOConnection1 do
begin
Connected:=False;
LoginPrompt:=False;
ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=perpus;Data Source=MWN\RAHMATMWN';
Connected:=True;
LoginPrompt:=False;
ADOQuery1.Connection:=ADOConnection1;
end;
ShowMessage('Koneksi ke database sukses!');
end;

procedure TForm1.Button1Click(Sender: TObject);


begin
if (Edit1.Text='') or (Edit2.Text='') or (Edit4.Text='') then
begin
ShowMessage('Lengkapi datanya');
Edit1.SetFocus;
end
else
begin
cari;
if ADOQuery1.IsEmpty then
begin
try ADOConnection1.BeginTrans;
simpan;
ADOConnection1.CommitTrans;
ShowMessage('Berhasil Simpan');
except ADOConnection1.RollbackTrans;

ShowMessage('Gagal Simpan');
Edit1.SetFocus;
end;
end
else
begin
try ADOConnection1.BeginTrans;
ubah;
ADOConnection1.CommitTrans;
ShowMessage('Berhasil ubah');
except ADOConnection1.RollbackTrans;
ShowMessage('Gagal ubah');
Edit1.SetFocus;
end;
end;
end;
btn1.Click;
end;
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
cari;
if ADOQuery1.IsEmpty then
begin
Button1.Caption:='SIMPAN';
Edit2.SetFocus
end
else
begin
Button1.Caption:='UBAH';
Edit2.Text:=ADOQuery1['nama'];
ComboBox1.Text:=ADOQuery1['jurusan'];
Edit3.Text:=ADOQuery1['buku_pinjam'];
Edit4.Text:=ADOQuery1['tgl_pinjam'];
Edit5.Text:=ADOQuery1['tgl_kembali'];

end;
end;
end;

procedure TForm1.btn1Click(Sender: TObject);


begin
with ADOQuery1 do
begin
Active :=False;
Close;
SQL.Clear;
SQL.Text:='select * from mhs where nim='+QuotedStr(edt1.Text);
Open;
end;
ds1.DataSet:=ADOQuery1;
dbgrd1.DataSource:=ds1;
end;

procedure TForm1.Edit1Change(Sender: TObject);


begin
if (Edit1.Text ='') then
begin
button1.caption:='Simpan';
Edit2.Clear;
Edit4.Clear;
ComboBox1.text:='';

end;

end;

procedure TForm1.edt1Change(Sender: TObject);


begin
btn1.Click;
end;

procedure TForm1.Button2Click(Sender: TObject);


begin
with ADOQuery1 do
begin
Active :=False;
Close;
SQL.Clear;
SQL.Text:='select * from mhs';
Open;
end;
ds1.DataSet:=ADOQuery1;
dbgrd1.DataSource:=ds1;

end;

procedure TForm1.Button3Click(Sender: TObject);


begin
with ADOQuery1 do
begin
Active :=False;
Close;
SQL.Clear;
SQL.Text:='select * from kosong';
Open;
end;
ds1.DataSet:=ADOQuery1;
dbgrd1.DataSource:=ds1;

end;

end.

You might also like