Insert, Edit & Delete Data Tabel dengan Visual Basic
Di tutorial Insert, Edit & Delete Data Tabel dengan Visual Basic, anda akan belejar bagaimana menambah record ke database (save data), mengedit record yang telah ada di tabel (edit data) dan menghapus record yang telah ada di tabel (delete data).
Bukalah kembali project yang telah anda buat pada tutorial sebelumnya.
Pada tutorial sebelumnya anda telah membuat admin_from seperti di bawah:
Gambar1. Admin Form
Pilih menu project -> Components atau tekan Crtl+T pada keybord anda. Centang pada "Microsoft ADO Data Control 6.0 (OLEDB)" dan "Microsoft DataGrid Control 6.0 (OLEDB)" kemudian tekan tombol OK. Pada bagian pallet window akan muncul icon seperti yang diberi tanda di bawah, yang diberi tanda merah adalah "Microsoft ADO Data Control 6.0 (OLEDB)" atau disingkat ADODC dan yang diberi tanda biru adalah "Microsoft DataGrid Control 6.0 (OLEDB)" atau disingkat DataGrid.
Gambar 2. ADODC dan DataGrid .
Sekarang pilih menu Project -> Add From, pada window Add Form pilih Form dan tekan tombol Open. Sebuah form baru akan terbentuk. Tambahkan 4 label, 4 text box, 4 button, 1 Adodc dan 1 datagrid, seperti di bawah:
Gambar 3.From baru
Kemudian atur properti form tersebut seperti berikut :
Komponen | Properti | Keterangan |
---|---|---|
Form1 | Name : form_product Caption : Product Entry | |
Label1 | Caption : Product ID | |
Adodc1 | Name : ado_product Caption : Data Product | |
Text1 | Name : txt_prd_ID Text : "" DataSource : ado_product DataField : product_id Locked : true | DataField diisi dengan nama field pada tabel yang ingin ditampilkan pada textbox |
Label2 | Caption : Product Name | |
Text2 | Name :txt_prd_name Text : "" DataSource :ado_product DataField : product_name Locked : true | pada textbox ini akan ditampilkan field product_name |
Label3 | Caption : Price | |
Text3 | Name : txt_price Text : "" DataSource : ado_product DataField : price Locked : true | |
Label4 | Caption : Stock | |
Text4 | Name : txt_stock Text : "" DataSource : ado_product DataField : stock Locked : true | |
Command1 | Name : btn_add Caption : Add | |
Command2 | Name : btn_edit Caption : Edit | |
Command3 | Name : btn_delete Caption : Delete | |
Command4 | Name : btn_close Caption : Close | |
DataGrid1 | Name : dg_product DataSource : ado_product AllowUpdate : False |
Gambar 4. form_product
Langkah selanjunya, double click pada from dan ketik kode di bawah :
Public rs_Product As ADODB.Recordset
Private Sub Form_Load()
On Error GoTo salah
'membuka koneksi
Call Koneksi
Set rs_Product = New ADODB.Recordset
Dim SQl As String
'membentuk query untuk menampilkan data
SQl = "select * from product order by product_id"
'membuka tabel
rs_Product.CursorLocation = adUseClient
rs_Product.Open SQl, conn, adOpenDynamic, adLockOptimistic
'manampilkan data pada datagrid
Set ado_product.Recordset = rs_Product
Exit Sub
salah:
MsgBox err.Description
End Sub
Sekarang double click tombol Add dan ketik kode berikut:
On Error GoTo salah
If btn_add.Caption = "Add" Then
btn_add.Caption = "Save"
'menambah record baru ke tabel
ado_product.Recordset.AddNew
txt_prd_ID.Locked = False
txt_prd_name.Locked = False
txt_price.Locked = False
txt_stock.Locked = False
btn_edit.Enabled = False
btn_delete.Enabled = False
Else
'menyimpan record baru
ado_product.Recordset.Update
txt_prd_name.Locked = True
txt_prd_ID.Locked = True
txt_price.Locked = True
txt_stock.Locked = True
btn_edit.Enabled = True
btn_delete.Enabled = True
btn_add.Caption = "Add"
End If
Exit Sub
salah:
MsgBox err.Description
End Sub
On Error GoTo salah
If btn_edit.Caption = "Edit" Then
'edit record pada tabel
ado_product.Recordset.Update
txt_prd_ID.Locked = False
txt_prd_name.Locked = False
txt_price.Locked = False
txt_stock.Locked = False
btn_edit.Caption = "Save"
btn_delete.Enabled = False
btn_add.Enabled = False
Else
'simpan perubahan data
'dan kembalikan kondisi tombol dan text box
txt_prd_name.Locked = True
txt_prd_ID.Locked = True
txt_price.Locked = True
txt_stock.Locked = True
btn_add.Enabled = True
btn_delete.Enabled = True
btn_edit.Caption = "Edit"
End If
Exit Sub
salah:
MsgBox err.Description
End Sub
On Error GoTo salah
Dim pil As VbMsgBoxResult
pil = MsgBox("Are you sure to delete?", vbYesNo + vbQuestion, "Delete")
If pil = vbYes Then
'hapus record
ado_product.Recordset.Delete
'memapilkan kembali record-record tabel
Call Form_Load
End If
Exit Sub
salah:
MsgBox err.Description
End Sub
ado_product.Caption = "Data ke-" & ado_product.Recordset.AbsolutePosition & " Dari total " & ado_product.Recordset.RecordCount & " data"
End Sub
Private Sub btn_close_Click()
Unload Me
End Sub
Sampai disini anda telah selesai membuat form untuk menambah record ke database, mengedit record yang telah ada, dan menghapus record yang telah ada di tabel. Sekarang anda buka form_admin (Gambar 1) dan double click pada tombol Product dan ketik kode berikut:
Private Sub btn_product_Click()
Load form_product
form_product.Show
End Sub
Sekarang simpan project anda kembali dan cobalah untuk menjalanknya dengan menekan tombol F5 pada keybord atau dengan memilih menu Run -> Start.
Di tutorial Membuat Aplikasi Client Server dengan VB dan MYSQL, pokok bahasan Insert, Edit & Delete Data Tabel dengan Visual Basic, anda telah belajar tentang cara insert data ke tabel mysql, edit data, dan delete data yang ada di tabel menggunakan ADODB. Belajar cara menggunakan Microsoft ADO Data Control 6.0 (OLEDB)" atau disingkat ADODC dan "Microsoft DataGrid Control 6.0 "
Free Template Blogger collection template Hot Deals BERITA_wongANteng SEO theproperty-developer