Insert, Update, Delete Vb Net 2010 Database Access -
Apa kabar para programer sekalian, alhamdulilah atas kesempatan yang diberikan Allah swt yang diberikan kepada saya untuk bisa membuat posting yang berjudul Insert, Update, Delete Vb Net 2010 Database Access. Sebelumnya saya sudah membuat posting yang berjudul Membuat Form Insert VB Net 2010 Database Access.
Untuk membuat form Insert, Update, Delete Vb Net 2010 Database Access ikuti langkah - langkah sebagai berikut.
- Buatlah form dengan nama Barang di VB net seperti berikut.
- Form diatas terdiri dair 5 Label, 5 TextBox, 4 Bottom dan 1 DataGridView. Ubah nama textbox, bottom dan Datagridviewnya sesuai nomer dan tulisan merah yang ada disamping gambar form diatas.
- Untuk membuat database dan menampilkan database ke dalam DataGridView silahkan baca dulu Menampilkan Database Ke DataGridView VB.net 2010
- Jika sudah berhasil membuat database dan menampilkannya ke dalam DataGridView, selanjutnya ganti semua coding yang ada pada form dengan coding dibawah ini :
Imports System.Data.OleDb
Public Class Barang
Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = |datadirectory|\db_barang.mdb; persist security info = false"
Public conn As New OleDbConnection
Public strsql As String
Public acsconn As New OleDb.OleDbConnection
Public acsdr As OleDbDataReader
Private Sub Barang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = connstring
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Membuka DataBase")
Else
MsgBox("close")
End If
Me.RefreshDataView()
Me.BtnDelete.Enabled = False
Me.BtnUpdate.Enabled = False
End Sub
Sub RefreshDataView()
Dim SqlQuery As String = " SELECT * FROM barang "
Dim SqlCommand As New OleDbCommand
Dim sqlAdapter As New OleDbDataAdapter
Dim TABLE As New DataTable
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
End With
With sqlAdapter
.SelectCommand = SqlCommand
.Fill(TABLE)
End With
dgvbarang.Rows.Clear()
For i = 0 To TABLE.Rows.Count - 1
With dgvbarang
.Rows.Add(TABLE.Rows(i)("id_barang"), TABLE.Rows(i)("nama_barang"), TABLE.Rows(i)("merk"), TABLE.Rows(i)("jenis"), TABLE.Rows(i)("harga"))
End With
Next
End Sub
Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
Try
Dim SqlQuery As String = "Insert Into barang (id_barang, nama_barang,merk, jenis, harga) Values ('" & Me.txtid_barang.Text & "','" & Me.txtnama.Text & "','" & Me.txtmerk.Text & "','" & Me.txtjenis.Text & "','" & Me.txtharga.Text & "')"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Satu Data Barang sudah disimpan ")
Me.RefreshDataView()
Me.bersih()
Me.BtnDelete.Enabled = False
Me.BtnUpdate.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Sub bersih()
txtid_barang.Text = ""
txtnama.Text = ""
txtmerk.Text = ""
txtjenis.Text = ""
txtharga.Text = ""
End Sub
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Try
Dim SqlQuery As String = "UPDATE barang SET nama_barang = '" & txtnama.Text & "', merk = '" & txtmerk.Text & "',jenis = '" & txtjenis.Text & "', harga = '" & txtharga.Text & "' WHERE ID_barang = '" & txtid_barang.Text & "';"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Data berhasil di Ubah")
Me.BtnDelete.Enabled = False
Me.BtnUpdate.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub dgvbarang_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvbarang.MouseClick
Dim dgv As DataGridView = CType(sender, DataGridView)
Dim colom0 As DataGridViewCell = dgvbarang.SelectedCells(0)
Dim colom1 As DataGridViewCell = dgvbarang.SelectedCells(1)
Dim colom2 As DataGridViewCell = dgvbarang.SelectedCells(2)
Dim colom3 As DataGridViewCell = dgvbarang.SelectedCells(3)
Dim colom4 As DataGridViewCell = dgvbarang.SelectedCells(3)
Dim selCell0 As Integer = colom0.ColumnIndex
Dim selCell1 As Integer = colom1.ColumnIndex
Dim selCell2 As Integer = colom2.ColumnIndex
Dim selCell3 As Integer = colom3.ColumnIndex
Dim selCell4 As Integer = colom3.ColumnIndex
txtid_barang.Text = dgvbarang.CurrentRow.Cells(selCell0).Value.ToString()
txtnama.Text = dgvbarang.CurrentRow.Cells(selCell1).Value.ToString()
txtmerk.Text = dgvbarang.CurrentRow.Cells(selCell2).Value.ToString()
txtjenis.Text = dgvbarang.CurrentRow.Cells(selCell3).Value.ToString()
txtharga.Text = dgvbarang.CurrentRow.Cells(selCell4).Value.ToString()
Me.BtnUpdate.Enabled = True
Me.BtnDelete.Enabled = True
Me.BtnInsert.Enabled = False
End Sub
Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
Me.bersih()
Me.BtnDelete.Enabled = False
Me.BtnUpdate.Enabled = False
Me.BtnInsert.Enabled = True
End Sub
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
Try
Dim SqlQuery As String = "DELETE FROM barang WHERE ID_barang = '" & txtid_barang.Text & "'"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox(" Satu Data Terhapus... ")
Me.BtnInsert.Enabled = True
Me.BtnUpdate.Enabled = False
Me.BtnDelete.Enabled = False
Me.RefreshDataView()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
- Jalankan aplikasinya, jika berhasil tampilan form akan seperti dibawah ini.
Jika tampilan aplikasi anda seperti gambar di atas dan tidak ada error. Saya acungin jempol buat anda.
Tapi buat yang masih Error dari pada bingung nich saya kasih yang udah mateng alias program yang udah jadi. Silahkan download From Insert Update delete Vb net 2010 di SINI. Terima kasih atas kunjungan dan kesempatannya bisa membaca posttingan saya kali ini. Semoga bisa membantu.
Baca juga Postingan sebelumnya :
ada kesalahan di conn.open
BalasHapus