Button Simpan,Edit, & Hapus disini akan kita gunakan untuk mengelola database di vb 6.
Adapun peralatan tempur yang kita gunakan yakni sebagai berikut :
1. ADO Data Control 6.0 (OLEDB). Lihat Tutorial Koneksi Database Access dengan Adodc VB 6.
2. ADO DataGrid Control 6.0 (OLEDB).
3. Database dengan Format.mdb (Access 2002-2003)
Dalam kasus ini saya akan memberi contoh mengelola database pelanggan, adapun tampilan formnya sebagai berikut :
Object Properties :
TextBox 1 = ( Property : Name : Text1 )
TextBox 2 = ( Property : Name : Text2 )
TextBox 3 = ( Property : Name : Text3 )
TextBox 4 = ( Property : Name : Text4 )
Label 1 = ( Property : Name : Label1 - Caption : ID Pelanggan )
Label 2 = ( Property : Name : Label2 - Caption : Nama )
Label 3 = ( Property : Name : Label3 - Caption : Alamat )
Label 4 = ( Property : Name : Label4 - Caption : Telepon )
CommandButton 1 = ( Property : Name : cmdtambah - Caption : TAMBAH )
CommandButton 2 = ( Property : Name : cmdsimpan - Caption : SIMPAN )
CommandButton 3 = ( Property : Name : cmdedit - Caption : EDIT )
CommandButton 4 = ( Property : Name : cmdhapus - Caption : HAPUS )
Adodc = ( Property : Name : Adodc1 )
Adodc = ( Property : Visible : False )
DataGrid = ( Property : Name : DataGrid1 )
TextBox 2 = ( Property : Name : Text2 )
TextBox 3 = ( Property : Name : Text3 )
TextBox 4 = ( Property : Name : Text4 )
Label 1 = ( Property : Name : Label1 - Caption : ID Pelanggan )
Label 2 = ( Property : Name : Label2 - Caption : Nama )
Label 3 = ( Property : Name : Label3 - Caption : Alamat )
Label 4 = ( Property : Name : Label4 - Caption : Telepon )
CommandButton 1 = ( Property : Name : cmdtambah - Caption : TAMBAH )
CommandButton 2 = ( Property : Name : cmdsimpan - Caption : SIMPAN )
CommandButton 3 = ( Property : Name : cmdedit - Caption : EDIT )
CommandButton 4 = ( Property : Name : cmdhapus - Caption : HAPUS )
Adodc = ( Property : Name : Adodc1 )
Adodc = ( Property : Visible : False )
DataGrid = ( Property : Name : DataGrid1 )
Langsung Saja ke kodingannya..
Buatlah Sub Statement Seperti Berikut :
Sub Mati()
Me.Text1.Locked = True
Me.Text2.Locked = True
Me.Text3.Locked = True
Me.Text4.Locked = True
End Sub
Sub Aktif()
Me.Text1.Locked = False
Me.Text2.Locked = False
Me.Text3.Locked = False
Me.Text4.Locked = False
End Sub
Sub Bersih()
Me.Text1.Text = ""
Me.Text2.Text = ""
Me.Text3.Text = ""
Me.Text4.Text = ""
End Sub
Me.Text1.Locked = True
Me.Text2.Locked = True
Me.Text3.Locked = True
Me.Text4.Locked = True
End Sub
Sub Aktif()
Me.Text1.Locked = False
Me.Text2.Locked = False
Me.Text3.Locked = False
Me.Text4.Locked = False
End Sub
Sub Bersih()
Me.Text1.Text = ""
Me.Text2.Text = ""
Me.Text3.Text = ""
Me.Text4.Text = ""
End Sub
1. Form Load
Call Mati
2. Button Tambah
Call Bersih
Call Aktif
Me.Text1.SetFocus
Call Aktif
Me.Text1.SetFocus
3. Button Simpan
With Adodc1.Recordset
.Find "ID_Pelanggan='" & Me.Text1.Text & "'", , adSearchForward
If Not .EOF Then
MsgBox "ID Pelanggan Sudah Terdaftar", vbCritical, ""
.Requery
Me.Text1.Text = ""
Me.Text1.SetFocus
Exit Sub
Else
If Me.Text1.Text = "" Or Me.Text2.Text = "" Or Me.Text3.Text = "" Then
MsgBox "Data Belum Lengkap", vbInformation, ""
Exit Sub
Else
If .EOF Then
.AddNew
!ID_Pelanggan = Me.Text1.Text
!Nama = Me.Text2.Text
!Alamat = Me.Text3.Text
!Telepon = Me.Text4.Text
.Update
End If
End If
End If
End With
MsgBox "Data Tersimpan", vbInformation, ""
End Sub
.Find "ID_Pelanggan='" & Me.Text1.Text & "'", , adSearchForward
If Not .EOF Then
MsgBox "ID Pelanggan Sudah Terdaftar", vbCritical, ""
.Requery
Me.Text1.Text = ""
Me.Text1.SetFocus
Exit Sub
Else
If Me.Text1.Text = "" Or Me.Text2.Text = "" Or Me.Text3.Text = "" Then
MsgBox "Data Belum Lengkap", vbInformation, ""
Exit Sub
Else
If .EOF Then
.AddNew
!ID_Pelanggan = Me.Text1.Text
!Nama = Me.Text2.Text
!Alamat = Me.Text3.Text
!Telepon = Me.Text4.Text
.Update
End If
End If
End If
End With
MsgBox "Data Tersimpan", vbInformation, ""
End Sub
4. Button Edit
With Adodc1.Recordset
!ID_Pelanggan = Me.Text1.Text
!Nama = Me.Text2.Text
!Alamat = Me.Text3.Text
!Telepon = Me.Text4.Text
.Update
End With
MsgBox "Perubahan Data Tersimpan", vbInformation, ""
!ID_Pelanggan = Me.Text1.Text
!Nama = Me.Text2.Text
!Alamat = Me.Text3.Text
!Telepon = Me.Text4.Text
.Update
End With
MsgBox "Perubahan Data Tersimpan", vbInformation, ""
5. Button Hapus
D = MsgBox("Hapus Data...?", vbYesNo + vbQuestion, "")
If D = vbYes Then
Adodc1.Recordset.Delete
End If
MsgBox "Data Terhapus", vbInformation, ""
If D = vbYes Then
Adodc1.Recordset.Delete
End If
MsgBox "Data Terhapus", vbInformation, ""
0 Comment:
Post a Comment