--> Skip to main content
Pelajar Koding

follow us

Vb.Net Ms Access: Create, Read, Update, Delete (Crud) Part 4

Lanjur dari Part 3 , bakal simpulan telah artikel perihal CRUD VB.NET dengan database Microsoft Access ini, agar bermanfaat yah readers. Ini ia code lengkap nya.... Imports System.Data.OleDb Public Class frmCategory   Dim con As New OleDbConnection   Sub Open_Koneksi()       con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _                              ";Data Source=latihan.accdb;" & _                              "Persist Security Info=False;"       con.Open()   End Sub   Sub Data_Load()       Dim cmd As New OleDbCommand       Dim adapt As New OleDbDataAdapter       Dim dt As New DataTable       Try           If Not con.State = ConnectionState.Open Then Open_Koneksi()           cmd.Connection = con           cmd.CommandText = "SELECT kodekategori, namakategori FROM kategori"           adapt.SelectCommand = cmd           adapt.Fill(dt)           With grdData               .AllowUserToAddRows = False               .ReadOnly = True               .DataSource = dt           End With       Catch ex As OleDbException           MessageBox.Show( "Error: " & ex.Message)       Finally           con.Close()       End Try   End Sub   Private Sub frmCategory_Load(sender As Object, _           e As EventArgs) Handles Me .Load       Data_Load()   End Sub   Private Sub tbrSave_Click(sender As Object, _           e As EventArgs) Handles tbrSave.Click       Dim myCommand As New OleDbCommand       Dim SQL As String       Try         If Not con.State = ConnectionState.Open Then Open_Koneksi()         If tbrEdit.Enabled = True Then           SQL = "INSERT INTO kategori (kodekategori,namakategori) VALUES" & _                 " ('" & txtCode.Text & "', '" & txtName.Text & "')"         Else           SQL = "UPDATE kategori SET namakategori='" & txtName.Text & "'" & _                 " WHERE kodekategori = '" & txtCode.Text & "'"         End If         myCommand.Connection = con         myCommand.CommandText = Sql         myCommand.ExecuteNonQuery()         If tbrEdit.Enabled = True Then             MsgBox( "Data baru tersimpan" )         Else             MsgBox( "Perubahan tersimpan" )         End If         tbrCancel_Click( Nothing , Nothing )         con.Close()       Catch myerror As OleDbException           MessageBox.Show( "Error: " & myerror.Message)       Finally           con.Dispose()       End Try   End Sub   Private Sub grdData_CellMouseDoubleClick( ByVal sender As Object, _   ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _   Handles grdData.CellMouseDoubleClick       txtCode.Text = grdData.CurrentRow.Cells(0).Value       txtName.Text = grdData.CurrentRow.Cells(1).Value       tbrEdit.Enabled = False       txtCode.ReadOnly = True   End Sub   Private Sub tbrEdit_Click( ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles tbrEdit.Click       grdData_CellMouseDoubleClick( Nothing , Nothing )   End Sub   Private Sub tbrCancel_Click( ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles tbrCancel.Click       txtCode.Text = String .Empty       txtName.Text = String .Empty       tbrEdit.Enabled = True       txtCode.ReadOnly = False       Data_Load()   End Sub   Private Sub tbrDelete_Click(sender As Object, _           e As EventArgs) Handles tbrDelete.Click       If MsgBox( "Yakin akan menghapus data?" , MsgBoxStyle.YesNo, _            "Konfirmasi" ) = MsgBoxResult.No Then Exit Sub       Dim myCommand As New OleDbCommand       Dim SQL As String       Try           If Not con.State = ConnectionState.Open Then Open_Koneksi()           SQL = "DELETE FROM kategori WHERE kodekategori = " & _                 "'" & grdData.CurrentRow.Cells(0).Value & "'"           myCommand.Connection = con           myCommand.CommandText = SQL           myCommand.ExecuteNonQuery()           MsgBox( "Data terhapus" )           tbrCancel_Click( Nothing , Nothing )           con.Close()       Catch myerror As OleDbException           MessageBox.Show( "Error: " & myerror.Message)       Finally           con.Dispose()       End Try   End Sub   Private Sub tbrClose_Click(sender As Object, _       e As EventArgs) Handles tbrClose.Click       Me.Close()   End Sub End Class Happy Coding ^_^ Click here if you like this article.
Sumber http://rani-irsan.blogspot.com

You Might Also Like:

Comment Policy: Silahkan tuliskan komentar Anda yang sesuai dengan topik postingan halaman ini. Komentar yang berisi tautan tidak akan ditampilkan sebelum disetujui.
Buka Komentar