Program Visual Basic Net Simpan Edit Hapus Record Pada Database Access

Pada artikel program visual basic net simpan edit hapus record pada database access saya membahas kumpulan program yang terpisah pada artikel :

Program Tambah Record Di Visual Basic Net 2008

Program Edit dan Hapus Data Database Access Dengan VB Net

Pencarian Record Data Database Access Menggunakan Visual Basic Net 2008

menjadi satu program utuh, sehingga anda tinggal menyimpan data, atau edit data, atau hapus data.

Program juga dilengkapi dengan beberapa perintah untuk memodifikasi button saat melakukan penyimpanan data, pengeditan ataupun penghapusan.


Silahkan buat dahulu database access :
Nama Database : SmartSolution.Mdb
Nama Table : Buku
Field
1. Kode type Text
2. Judul type Text
3. Pengarang type Text
4. Penerbit type Text

Design Form Program Visual Basic Net Simpan Edit Hapus Record Pada Database Access

Ketikkan listing program visual basic net simpan edit hapus record pada database access berikut

Imports System.Data.OleDb

Public Class Form1
    Public DB As OleDbConnection
    Public CMD As OleDbCommand
    Public ADP As OleDbDataAdapter
    Public DR As OleDbDataReader
    Public DS As New DataSet
    Dim SQL As String

    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

    Sub TampilRecordBuku()
        ' PERINTAH SELEC TAMBAH HURUF T
        ADP = New OleDbDataAdapter("SELEC * FROM Buku", DB)
        DS = New DataSet
        ADP.Fill(DS, "Buku")
        DataGridView1.DataSource = DS.Tables("Buku")
    End Sub

    Sub ButtonBaru()
        btnBaru.Enabled = True
        btnSimpan.Enabled = False
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnBatal.Enabled = False
        btnkeluar.Enabled = True
    End Sub

    Sub ButtonEdit()
        btnBaru.Enabled = False
        btnSimpan.Enabled = False
        btnEdit.Enabled = True
        btnHapus.Enabled = True
        btnBatal.Enabled = True
        btnkeluar.Enabled = False
    End Sub

    Sub ButtonSimpan()
        btnBaru.Enabled = False
        btnSimpan.Enabled = True
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnBatal.Enabled = True
        btnkeluar.Enabled = False
    End Sub

    Sub HapusText()
        Judul.Text = ""
        Kode.Text = ""
        Pengarang.Text = ""
        Penerbit.Text = ""
        Call ButtonBaru()
    End Sub

    Private Sub btnkeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkeluar.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call OpenDB()
        Call HapusText()
        Call TampilRecordBuku()
    End Sub

    Private Sub btnBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaru.Click
        Call HapusText()
        Kode.Focus()
    End Sub

    Private Sub Kode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Kode.KeyPress
        Dim tombol As Integer = Asc(e.KeyChar)
        Dim xkode As String
        If tombol = 13 Then

            ' PERINTAH SELEC TAMBAH HURUF T
            SQL = "SELEC * FROM BUKU Where Kode='" & Kode.Text & "'"
            CMD = New OleDbCommand(SQL, DB)
            DR = CMD.ExecuteReader
            DR.Read()
            If DR.HasRows = True Then
                Judul.Text = DR.Item(1)
                Pengarang.Text = DR.Item(2)
                Penerbit.Text = DR.Item(3)
                Call ButtonEdit()
                MsgBox("Data ditemukan !")
            Else
                xkode = Kode.Text
                Call HapusText()
                Kode.Text = xkode
                Call ButtonSimpan()
            End If
            Judul.Focus()
        End If
    End Sub

    Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
        Call HapusText()
        Kode.Focus()
    End Sub

    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 TampilRecordBuku()
    End Sub

    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 TampilRecordBuku()
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        SQL = "INSERT INTO BUKU(kode, judul, pengarang, penerbit) " & _
                        " values('" & Kode.Text & "','" & _
                                      Judul.Text & "','" & _
                                      Pengarang.Text & "','" & _
                                      Penerbit.Text & "')"


        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnBaru_Click(sender, e)
        Call TampilRecordBuku()
    End Sub

End Class


Sumber : Mesran.net
Back to top