Home > contoh, input data > input Data

input Data

January 2, 2012

kali ini kita akan membuat inputan data sederhana,dengan menggunakan komponen adotable.
untuk lebih jelasnya silakan bwt form seperti berikut :


tambahkan variable query : TADOQuery; pada variable global pada form1.

perintah pada :

procedure cek(sql:string);
begin
query:=TADOQuery.Create(nil);
query.ConnectionString:=form1.ADOTable1.ConnectionString;
query.SQL.Clear;
query.SQL.Add(sql);
query.Open;
end;

form create :

procedure TForm1.FormCreate(Sender: TObject);
begin

adotable1.ConnectionString:=’Provider=Microsoft.Jet.OLEDB.4.0;’+
‘Data   Source=’+ExtractFilePath(Application.ExeName)+’Penjualan.mdb;Persist Security Info=False’;
adotable1.Open;

end;

button tambah :

procedure TForm1.tambahClick(Sender: TObject);
begin
adotable1.Append;
dbedit1.SetFocus;
end;

button simpan :

procedure TForm1.simpanClick(Sender: TObject);
var
a : integer;
begin

cek(‘select kodekaryawan from karyawan where               kodekaryawan=’+QuotedStr(dbedit1.Text));
if ADOTable1.State in [dsinsert] then
begin

if query.RecordCount > 0 then
begin
showmessage(‘Kode karyawan tersebut sudah digunakan’);
DBEdit1.SetFocus;
exit;
end;
for a:=0 to ComponentCount – 1 do
begin
if (Components[a] is TDBEdit) and (length(Tdbedit(Components[a]).Text) = 0) then
begin
showmessage(Components[a].Name+’ tidak boleh kosong ‘);
Tdbedit(Components[a]).SetFocus;
exit;
end;
end;
end;
ADOTable1.Post;
end;

Button batal :

procedure TForm1.batalClick(Sender: TObject);
begin
adotable1.Cancel;
end;

Button ubah :

procedure TForm1.ubahClick(Sender: TObject);
begin
adotable1.Edit;
end;

Button Hapus :

procedure TForm1.hapusClick(Sender: TObject);
begin
if MessageDlgPos(‘Yakin akan menghapus karyawan dengan kode ‘+adotable1[‘kodekaryawan’],mtConfirmation, mbOKCancel, 0, 300, 300) = 1 then
adotable1.Delete;

end;

button keluar :

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
adotable1.Close;
end;

perintah pada adotable :

procedure TForm1.ADOTable1BeforeInsert(DataSet: TDataSet);
begin
tambah.Enabled:=false;  simpan.Enabled:=true;
ubah.Enabled:=false;  batal.Enabled:=true;
Hapus.Enabled:=false;  keluar.Enabled:=false;
if adotable1.State in [dsinsert] then
dbedit1.ReadOnly:=true
else
begin
dbedit1.ReadOnly:=false;
dbedit2.SetFocus;
end;
dbgrid1.Enabled:=false;
end;

procedure TForm1.ADOTable1AfterPost(DataSet: TDataSet);
begin
tambah.Enabled:=True;   simpan.Enabled:=false;
ubah.Enabled:=true;  batal.Enabled:=false;
Hapus.Enabled:=true;  keluar.Enabled:=true;
if adotable1.State in [dsinsert] then
dbedit1.ReadOnly:=true
else
dbedit1.ReadOnly:=false;
dbgrid1.Enabled:=true;
end;

Categories: contoh, input data Tags:
%d bloggers like this: