Selasa, 19 Maret 2013

Membuat Aplikasi Sederhana Menggunakan Database Acces

Untuk mengisi waktu luang, saya mengajak Anda untuk menuangkan tulisan di dalam Aplikasi Visaul Basic.Net 2008, kali ini saya mengajak anda belajar VB.Net 2008 Lanjutan dimana pada VB.Net 2008 lanjutan ini kita akan membahasnya menggunakan database Acces. Pembelajaran ini saya dapatkan dari seorang guru besar dikampus saya yaitu : Bapak Mesran,S.KOM,M.KOM
Bagaimana caranya inilah program sederhana yang saya buat,dengan rincian : 
1. Database Name : DBMahasiswa.mdb
2. Inilah gambarnya

Inilah Source Codenya :

Imports System.Data.OleDb
Public Class Form1
    Dim db As OleDbConnection
    Dim cmd As OleDbCommand
    Dim adp As OleDbDataAdapter
    Dim rs As OleDbDataReader
    Dim ds As New DataSet
    Dim sql As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenDB()
        TampilMHS()
        Baru_Click(sender, e)
    End Sub

    Sub OpenDB()
        Dim lokasi = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =E:DBMahasiswa.mdb"
        db = New OleDbConnection(lokasi)
        If db.State = ConnectionState.Closed Then
            db.Open()
        End If
    End Sub

    Sub TampilMHS()
        adp = New OleDbDataAdapter("Select * From Mahasiswa", db)
        ds = New DataSet
        adp.Fill(ds, "Mahasiswa")
        DataGridView1.DataSource = ds.Tables("Mahasiswa")
    End Sub

    Private Sub Simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Simpan.Click
        sql = "Insert Into Mahasiswa (NPM,Nama,Jenjang,Jurusan,Semester)" & _
                      " Values('" & NPM.Text & "','" & _
                                    Nama.Text & "','" & _
                                    Jenjang.Text & "','" & _
                                    Jenjang.Text & "','" & _
                                    Semester.Text & "')"
        cmd = New OleDbCommand(sql, db)
        cmd.ExecuteNonQuery()
        Baru_Click(sender, e)
        Call TampilMHS()
    End Sub

    Private Sub Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click
        sql = "Update Mahasiswa Set Nama='" & Nama.Text & "'," & _
                               " Jenjang='" & Jenjang.Text & "'," & _
                               " Jurusan='" & Jurusan.Text & "'," & _
                               " Semester='" & Semester.Text & "'," & _
                               " Where NPM='" & NPM.Text & "'"
        Try
            cmd = New OleDbCommand(sql, db)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Call Baru_Click(sender, e)
        Call TampilMHS()
    End Sub


    Private Sub Hapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hapus.Click
        sql = "Delete From Mahasiswa Where NPM='" & NPM.Text & "'"
        cmd = New OleDbCommand(sql, db)
        cmd.ExecuteNonQuery()
        Call Baru_Click(sender, e)
        Call TampilMHS()
    End Sub

    Private Sub Batal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Batal.Click
        Call Baru_Click(sender, e)
    End Sub

    Private Sub Keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keluar.Click
        End
    End Sub

    Private Sub Baru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Baru.Click
        NPM.Text = ""
        Nama.Text = ""
        Jenjang.Text = ""
        Jurusan.Text = ""
        Semester.Text = ""
    End Sub

    Private Sub NPM_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NPM.KeyPress
        If Asc(e.KeyChar) = 13 Then
            sql = "Select * From Mahasiswa Where NPM='" & NPM.Text & "'"
            cmd = New OleDbCommand(sql, db)
            rs = cmd.ExecuteReader
            rs.Read()

            If rs.HasRows = True Then
                Nama.Text = rs.Item(1)
                Jenjang.Text = rs.Item(2)
                Jurusan.Text = rs.Item(3)
                Semester.Text = rs.Item(4)
            Else
                MsgBox("Data Tidak Ada!")
            End If
        End If
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim pilih As Integer = Nothing
        pilih = DataGridView1.CurrentRow.Index
        With DataGridView1
            NPM.Text = .Item(0, pilih).Value
            Nama.Text = .Item(1, pilih).Value
            Jenjang.Text = .Item(2, pilih).Value
            Jurusan.Text = .Item(3, pilih).Value
            Semester.Text = .Item(4, pilih).Value
        End With
    End Sub

End Class

Semoga berhasil dan bermanfaat ya........






Tidak ada komentar:

Posting Komentar