
Terlebih dahulu persiapkan tabel Access.
Nama Database : SmartSolution.mdb
Nama Table : Buku
Field
1. Kode tipe Text
2. Judul tipe Text
3. Pengarang tipe Text
4. Penerbit tipe Text
Design Program Edit dan Hapus Data Database Access Dengan VB Net

Listing Program Edit dan Hapus Data Database Access Dengan VB Net
Imports System.Data.OleDb
Public Class Form1 Public DB As OleDbConnection
Public CMD As OleDbCommand
Public ADP As OleDbDataAdapter
Public DS As New DataSet
Dim SQL As String
' membuat koneksi ke Database Access Public Sub OpenDB() Dim LOKASI = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =E:DatabaseSmartSolution.mdb"
DB = New OleDbConnection(LOKASI)
If DB.State = ConnectionState.Closed Then
DB.Open()
End If
End Sub
' untuk menampilkan table buku pada datagridview melalui komponen Dataset
' perintah SELEC tambahkan huruf T Sub TampilBuku() ADP = New OleDbDataAdapter("SELEC * FROM Buku", DB)
DS = New DataSet
ADP.Fill(DS, "Buku")
DataGridView1.DataSource = DS.Tables("Buku")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call OpenDB()
Call btnBaru_Click(sender, e)
Call TampilBuku()
End Sub
' Double click button baru dan ketik perintah berikut ini
' membersihkan isian textbox Private Sub btnBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaru.Click Kode.Text = ""
Judul.Text = ""
Pengarang.Text = ""
Penerbit.Text = ""
End Sub
' Double click DataGridView dan rubah event menjadi CellClick dan ketik perintah berikut ini
' menampilkan data dari datagridview ke textbox Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Dim ubah As Integer = Nothing
ubah = DataGridView1.CurrentRow.Index
With DataGridView1
Kode.Text = .Item(0, ubah).Value
Judul.Text = .Item(1, ubah).Value
Pengarang.Text = .Item(2, ubah).Value
Penerbit.Text = .Item(3, ubah).Value
End With
End Sub
' Double click button hapus dan ketik perintah berikut ini :
' bagian menghapus data Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click SQL = "DELETE FROM BUKU Where Kode='" & Kode.Text & "'"
CMD = New OleDbCommand(SQL, DB)
CMD.ExecuteNonQuery()
Call btnBaru_Click(sender, e)
Call TampilBuku()
End Sub
' Double click button edit ketik perintah berikut ini :
' bagian mengedit data Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click SQL = "UPDATE Buku SET Judul='" & Judul.Text & "'," & _
" Pengarang='" & Pengarang.Text & "'," & _
" Penerbit='" & Penerbit.Text & "' " & _
" Where Kode='" & Kode.Text & "'"
CMD = New OleDbCommand(SQL, DB)
CMD.ExecuteNonQuery()
Call btnBaru_Click(sender, e)
Call TampilBuku()
End Sub
End Class
Tidak ada komentar:
Posting Komentar