Sunday, September 7, 2014

Ricky Andes || Pada tahap ini bagian yang harus kita buat adalah membuat koneksi antara Ms. acces dan Ms visual bassic.net yang sering kita sebut Module.
Cara nya ==> project => module - lalu klik. lalu ketikan seperti dibawah ini :
Imports System.Data.Odbc
Imports System.Data
Imports System.Data.OleDb
Module Module1

    Public X As Integer
    Public S As String = ""
    Public T As String = ""
    Public proses As New CsMHS
    Public tblmhs As DataTable
    Public CNN As OleDbConnection
    Public OLECMD As OleDbCommand
    Public RD As OleDbDataReader
    Public cmd As OleDbCommand
    Public KONEKSI As String = "provider=microsoft.ACE.OLEDB.12.0;Data source =" & _
    Application.StartupPath & "\register.mdb;"

End Module


pada bagian text berwana hijau yang saya garis bawahi itu merupakan nama tabel ms. acces yang sudah agan - agan buat sebelumnya.
setelah tahap penghubungan selesai silakan agan bentuk desain sendiri menurut imajinasi agan - agan atau juga bisa mengikuti desain yang sudah saya siapkan di bawah ini. :

klik gambar untuk memperbesar
untuk coding simpan/ save- nya tenang aja gan udah saya siapin sebagai referensi agan-agan semua untuk bahan experiment hehehe
nee :-D


Imports System.Data.OleDb
Imports System.Data
Public Class REGITER
    Dim cn As OleDbConnection
    Dim cmmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim ds As New DataSet
    Dim dv As DataView
    Dim da As OleDbDataAdapter
    Dim sqlda1 As OleDbDataAdapter
    Sub kondisi1()
        btnsimpan.Enabled = False
        btnedit.Enabled = False
        btnhapus.Enabled = False
        txtnpm.ReadOnly = False
    End Sub
    Sub kondisi2()
        btnsimpan.Enabled = True
        btnhapus.Enabled = False
        btnedit.Enabled = False
        txtnpm.ReadOnly = False
    End Sub
    Sub kondisi3()
        btnsimpan.Enabled = False
        btnhapus.Enabled = True
        btnedit.Enabled = True
    End Sub
    Sub bersih()
        txtalamat.Clear()
        txtnama.Clear()
        txtnpm.Clear()
        cmbmijus.Text = ""
        cmbjekel.Text = ""
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CNN = New OleDbConnection(KONEKSI)
        Dim sqlda1 As New OleDbDataAdapter("select * from mhs ", CNN)
        sqlda1.Fill(ds, "mhs")
        dv = New DataView(ds.Tables("mhs"))
        Me.DataGridView1.DataSource = dv
        rfs()
        kondisi2()
    End Sub
    Sub kode_otomatis()

        tblmhs = proses.ExecuteQuery("select * from mhs order by npm desc")
        txtnama.Focus()
        If tblmhs.Rows.Count = 0 Then
            txtnpm.Text = "MHS - 000000001"
        Else
            With tblmhs.Rows(0)
                txtnpm.Text = .Item("npm")
            End With
            txtnpm.Text = Val(Microsoft.VisualBasic.Mid(txtnpm.Text, 5, 3)) + 1
            If Len(txtnpm.Text) = 1 Then
                txtnpm.Text = "MHS - 000000001" & txtnpm.Text & " "
            ElseIf Len(txtnpm.Text) = 2 Then
                txtnpm.Text = "MHS - 000000001" & txtnpm.Text & " "
            ElseIf Len(txtnpm.Text) = 3 Then
                txtnpm.Text = "MHS - 000000001" & txtnpm.Text & " "
            End If
        End If
    End Sub
    Sub rfs()
        da = New OleDbDataAdapter("select * from mhs", CNN)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "mhs")
        Me.DataGridView1.Columns("npm").Width = 100
        Me.DataGridView1.Columns("nama").Width = 150
        Me.DataGridView1.Columns("jekel").Width = 150
        Me.DataGridView1.Columns("alamat").Width = 150
        Me.DataGridView1.Columns("tgl_daftar").Width = 130
        Me.DataGridView1.Columns("mijus").Width = 150
        Me.DataGridView1.Columns(0).HeaderText = "NPM"
        Me.DataGridView1.Columns(1).HeaderText = "NAMA MAHASISWA"
        Me.DataGridView1.Columns(2).HeaderText = "JENIS KELAMIN "
        Me.DataGridView1.Columns(3).HeaderText = "ALAMAT "
        Me.DataGridView1.Columns(4).HeaderText = "TGL DAFTAR"
        Me.DataGridView1.Columns(5).HeaderText = "MINAT STUDY"
        Me.DataGridView1.ReadOnly = True

    End Sub
    Sub binding()

        Me.txtnpm.DataBindings.Clear()
        Me.txtnpm.DataBindings.Add("text", dv, "npm")
        Me.txtnama.DataBindings.Clear()
        Me.txtnama.DataBindings.Add("text", dv, "nama")
        Me.cmbjekel.DataBindings.Clear()
        Me.cmbjekel.DataBindings.Add("text", dv, "jekel")
        Me.txtalamat.DataBindings.Clear()
        Me.txtalamat.DataBindings.Add("text", dv, "alamat")
        Me.Dtanggal.DataBindings.Clear()
        Me.Dtanggal.DataBindings.Add("text", dv, "tgl_daftar")
        Me.cmbmijus.DataBindings.Clear()
        Me.cmbmijus.DataBindings.Add("text", dv, "mijus")

    End Sub

 

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        binding()
        kondisi3()
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        If txtnpm.Text = "" Then
            MsgBox("Isi data dengan benar", MsgBoxStyle.Exclamation, "Kesalahan")
            Exit Sub
        End If
        Try

            CNN = New OleDbConnection(KONEKSI)
            Dim sqlda1 As New OleDbDataAdapter("update mhs set nama= '" & txtnama.Text & _
                                                "', jekel= '" & cmbjekel.Text & _
                                                "', alamat= '" & txtalamat.Text & _
                                                "', tgl_daftar= '" & Dtanggal.Value & _
                                                 "', mijus= '" & cmbmijus.Text & _
                                                "' where npm= '" & txtnpm.Text & "'", CNN)
            sqlda1.Fill(ds, "mhs")
            MsgBox("Data berhasil di ubah", MsgBoxStyle.Information)
            rfs()
            Call bersih()
            txtnpm.Focus()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub btnhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
        Dim konfirm As String
        konfirm = MsgBox("Ingin menghapus data", MsgBoxStyle.Question + MsgBoxStyle.OkCancel)
        If konfirm = vbOK Then

            CNN = New OleDbConnection(KONEKSI)
            Dim sqlda1 As New OleDbDataAdapter("Delete from mhs where npm= '" & txtnpm.Text & "'", CNN)
            sqlda1.Fill(ds, "mhs")
            MsgBox("Data berhasil di hapus", MsgBoxStyle.Information)
            Call bersih()
            txtnpm.Focus()
            rfs()

        Else
            Exit Sub
        End If
    End Sub

   

    Private Sub btnreset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreset.Click
        bersih()
    End Sub

    Private Sub btntambah_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntambah.Click
        kode_otomatis()
    End Sub

    Private Sub btnsimpan_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpan.Click
        If txtnpm.Text = "" Then
            MsgBox("isi data dengan benar..!!", MsgBoxStyle.Exclamation, "Kesalahan")
            Exit Sub
        End If
        CNN = New OleDbConnection(KONEKSI)
        Dim sqlda1 As New OleDbDataAdapter(" insert into mhs(npm,nama,jekel,alamat,tgl_daftar,mijus) values ('" & txtnpm.Text & "','" & txtnama.Text & "','" & cmbjekel.Text & "','" & txtalamat.Text & "','" & Dtanggal.Value & "','" & cmbmijus.Text & "')", CNN)

        sqlda1.Fill(ds, "mhs")
        MsgBox(" Data berhasil di simpan", MsgBoxStyle.Information)
        Call bersih()
        txtnpm.Focus()
    End Sub

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

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class

Selamat ber Experiment dan Semoga bermanfaat hehehe
#jangan lupa Perhatikan penempatan coding dan titik koma

0 comments:

Post a Comment