Senin, 06 Mei 2013

Belajar Bahasa Pemogram Visual Basic




Hai, kali ini saya akan posting hasil program saya yaitu  Sistem Informasi Karyawan, Program ini Merupakan program berbasis database. Database yang saya pakai untuk dikoneksikan ke VB adalah dengan memakai Microsoft Acces 2007.
Nah, langsung saja yah?
pertama-tama kita harus buat databse dulu di Acces nya. database ini saya simpan dengan nama karyawan, yang saya simpan di drive C, Saya buat Folder Baru dengan Nama vb2, dengan nama karyawan.
di Folder vb2 ini saya simpan semua database-database yang udah pernah saya buat untuk dikoneksikan ke VB. Trus dalam database Karyawan ini, saya buat tiga tabel dengan nama tabel Karyawan, Jabatan, Bagian.

Gambar di atas adalah Nama-nama tabel dari database karyawan.
Pertama-tama kita buat tabel untuk karyawan dulu yah.. 

Saya isi field name nya dengan NIK,Nama,Jabatan,JenisKelamin dan Bagian.
trus type nya semua saya buat text.
Seperti gambar di bawah ini.


Nah, bentuk dari tabel nya kan belum nampak, yaudah save aja. Buat nama nya karyawan yah...?::J
Kira-kira nanti seperti ini tabel nya.

Kemudian terserah kita mau isi table nya, ane ngisi tabel nya seperti yang di atas. :D
Lalu kita ke tabel berikut nya yuk....!
Langkah nya sama seperti sebelum nya, nah untuk mempersuingkat ane tunjukin table nya aja yah??

Gambar di atas tabel bagian

Gambar di atas tabel jabatan, nah semua tabel nya kan udah siap. Berarti database nya udah siap.
sekarang kita langsung ke VB aja.
Kita buat from Menu Dulu

Untuk mendesain From Menu di atas kita gunakan tool MenuStrip  yang kita cari di toolbox trus kita isi deh kotak-kotak yang mau di isi, seperti gambar di atas, Kalo untuk buat gambar-gambar icon nya tunggu di tutorial berikut nya yah?? :-D
langsung aja yah listing program nya yah.?
Listing Program
Public Class Form1
    Private Sub KeluarProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarProgramToolStripMenuItem.Click
        End
    End Sub

    Private Sub KaryawanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KaryawanToolStripMenuItem.Click
        karyawan.MdiParent = Me
        karyawan.Show()
    End Sub

    Private Sub BagianToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BagianToolStripMenuItem.Click
        bagian.MdiParent = Me
        bagian.Show()
    End Sub

    Private Sub JabatanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JabatanToolStripMenuItem.Click
        jabatan.MdiParent = Me
        jabatan.Show()
    End Sub

End Class

Sekarang kita buat from karyawan

Tuh, saya udah siapin form karyawan nya. sekarang kita akan buat listing program nya juga.
Kalo name-name nya sesuaikan aja sama yang di listing program, name-name nya gag jauh aneh-aneh kok.
Listing Program Untuk From Karyawan
Imports System.Data.OleDb
Public Class karyawan
    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=C:\VB2\karyawan.mdb"
        DB = New OleDbConnection(lokasi)
        If DB.State = ConnectionState.Closed Then
            DB.Open()
        End If
    End Sub
    Sub tampilrecordkaryawan()
        ADP = New OleDbDataAdapter("select * from karyawan", DB)
        DS = New DataSet
        ADP.Fill(DS, "karyawan")
        DataGridView1.DataSource = DS.Tables("karyawan")
    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()
        nik.Text = ""
        nama.Text = ""
        jabatan.Text = ""
        jeniskelamin.Text = ""
        bagian.Text = ""
        Call buttonbaru()
    End Sub


    Private Sub btnhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
        SQL = "delete from karyawan where NIK='" & nik.Text & "'"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordkaryawan()
    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 btnbaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbaru.Click
        Call hapustext()
        nik.Focus()
    End Sub
    Sub tampilkodejabatan()
        KdJabatan.Items.Clear()
        SQL = "SELECT * From Jabatan"
        CMD = New OleDbCommand(SQL, DB)
        DR = CMD.ExecuteReader
        While DR.Read
            KdJabatan.Items.Add(DR("KdJabatan"))
        End While
        DR.Close()
    End Sub
    Sub tampilkodebagian()
        KdBagian.Items.Clear()
        SQL = "SELECT * From Bagian"
        CMD = New OleDbCommand(SQL, DB)
        DR = CMD.ExecuteReader
        While DR.Read
            KdBagian.Items.Add(DR("KdBagian"))
        End While
        DR.Close()
    End Sub
    Private Sub karyawan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call opendb()
        Call hapustext()
        Call tampilrecordkaryawan()
        Call tampilkodejabatan()
        Call tampilkodebagian()


        jeniskelamin.Items.Add("laki-laki")
        jeniskelamin.Items.Add("perempuan")

    End Sub

    Private Sub nik_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles nik.KeyPress
        Dim tombol As Integer = Asc(e.KeyChar)
        Dim xkode As String
        Dim xkd As String
        Dim xKdbag As String

        If tombol = 13 Then
            SQL = "select *from karyawan where NIK='" & nik.Text & "'"
            CMD = New OleDbCommand(SQL, DB)
            DR = CMD.ExecuteReader
            DR.Read()
            If DR.HasRows = True Then
                nama.Text = DR.Item(1)
                xkd = DR.Item(2)
                jeniskelamin.Text = DR.Item(3)
                xKdbag = DR.Item(4)
                Call buttonedit()
                MsgBox("data ditemukan !!")
            Else
                xkode = nik.Text
                Call hapustext()
                nik.Text = xkode
                Call buttonsimpan()
            End If
            DR.Close()
            KdJabatan.Text = xkd
            KdBagian.Text = xKdbag
            nama.Focus()
        End If
    End Sub

    Private Sub btnbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbatal.Click
        Call hapustext()
        nik.Focus()
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        SQL = "update karyawan set Nama='" & nama.Text & "'," & _
                                 "jabatan='" & KdJabatan.Text & "'," & _
                                 "jeniskelamin='" & jeniskelamin.Text & "'," & _
                                 "bagian='" & KdBagian.Text & "' " & _
                                 " where NIK='" & nik.Text & "' "
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordkaryawan()
    End Sub

    Private Sub btnsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpan.Click
        SQL = "insert into karyawan(NIK, nama, jabatan, jeniskelamin, bagian) " & " values('" & nik.Text & "','" & nama.Text & "','" & KdJabatan.Text & "','" & jeniskelamin.Text & "','" & KdBagian.Text & "' )"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordkaryawan()
    End Sub

    Private Sub KdJabatan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KdJabatan.SelectedIndexChanged
        SQL = "SELECT * From Jabatan where KdJabatan='" & KdJabatan.Text & "'"
        CMD = New OleDbCommand(SQL, DB)
        DR = CMD.ExecuteReader
        If DR.HasRows Then
            DR.Read()
            jabatan.Text = DR("nmjabatan")
        End If
    End Sub

    Private Sub KdBagian_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KdBagian.SelectedIndexChanged
        SQL = "SELECT * From Bagian where KdBagian='" & KdBagian.Text & "'"
        CMD = New OleDbCommand(SQL, DB)
        DR = CMD.ExecuteReader
        If DR.HasRows Then
            DR.Read()
            bagian.Text = DR("nmbagian")
        End If
    End Sub
End Class


 Sekarang kita desain from Jabatan trus langsung ke listing program nya aja.



Listing Program Untuk From Jabatan

Imports System.Data.OleDb
Public Class jabatan
    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=C:\VB2\karyawan.mdb"
        DB = New OleDbConnection(LOKASI)
        If DB.State = ConnectionState.Closed Then
            DB.Open()
        End If
    End Sub

    Sub tampilrecordjabatan()
        ADP = New OleDbDataAdapter("SELECT * from jabatan", DB)
        DS = New DataSet
        ADP.Fill(DS, "jabatan")
        DataGridView1.DataSource = DS.Tables("jabatan")
    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()
        kdjabatan.Text = ""
        nmjabatan.Text = ""
        Call buttonbaru()
    End Sub
    Private Sub btnhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
        SQL = "delete from jabatan where kdjabatan='" & kdjabatan.Text & "'"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordjabatan()
    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 btnbaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbaru.Click
        Call hapustext()
        kdjabatan.Focus()
    End Sub

    Private Sub jabatan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call openDB()
        Call hapustext()
        Call tampilrecordjabatan()
    End Sub

    Private Sub kdjabatan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles kdjabatan.KeyPress
        Dim tombol As Integer = Asc(e.KeyChar)
        Dim xkode As String
        If tombol = 13 Then
            SQL = "select *from jabatan where kdjabatan='" & kdjabatan.Text & "'"
            CMD = New OleDbCommand(SQL, DB)
            DR = CMD.ExecuteReader
            DR.Read()
            If DR.HasRows = True Then
                nmjabatan.Text = DR.Item(1)
                Call buttonedit()
                MsgBox("data ditemukan !!")
            Else
                xkode = kdjabatan.Text
                Call hapustext()
                kdjabatan.Text = xkode
                Call buttonsimpan()
            End If
            nmjabatan.Focus()
        End If
    End Sub
    Private Sub btnbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbatal.Click
        Call hapustext()
        kdjabatan.Focus()
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        SQL = "update jabatan set nmjabatan='" & nmjabatan.Text & "' " & _
                                 " where kdjabatan='" & kdjabatan.Text & "' "
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordjabatan()
    End Sub

    Private Sub btnsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpan.Click
        SQL = "insert into jabatan(kdjabatan, nmjabatan) " & " values('" & kdjabatan.Text & "','" & nmjabatan.Text & "' )"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordjabatan()
    End Sub
End Class



Sekarang tinggal desain From bagian sama listing program nya.


Listing Program Untuk From Bagian

Imports System.Data.OleDb
Public Class bagian
    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=C:\VB2\karyawan.mdb"
        DB = New OleDbConnection(LOKASI)
        If DB.State = ConnectionState.Closed Then
            DB.Open()
        End If
    End Sub

    Sub tampilrecordbagian()
        ADP = New OleDbDataAdapter("SELECT * from bagian", DB)
        DS = New DataSet
        ADP.Fill(DS, "bagian")
        DataGridView1.DataSource = DS.Tables("bagian")
    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()
        kdbagian.Text = ""
        nmbagian.Text = ""
        Call buttonbaru()
    End Sub
    Private Sub btnhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
        SQL = "delete from bagian where kdbagian='" & kdbagian.Text & "'"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordbagian()
    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 btnbaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbaru.Click
        Call hapustext()
        kdbagian.Focus()
    End Sub

    Private Sub bagian_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call openDB()
        Call hapustext()
        Call tampilrecordbagian()
    End Sub


    Private Sub kdbagian_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles kdbagian.KeyPress
        Dim tombol As Integer = Asc(e.KeyChar)
        Dim xkode As String
        If tombol = 13 Then
            SQL = "select *from bagian where kdbagian='" & kdbagian.Text & "'"
            CMD = New OleDbCommand(SQL, DB)
            DR = CMD.ExecuteReader
            DR.Read()
            If DR.HasRows = True Then
                nmbagian.Text = DR.Item(1)
                Call buttonedit()
                MsgBox("data ditemukan !!")
            Else
                xkode = kdbagian.Text
                Call hapustext()
                kdbagian.Text = xkode
                Call buttonsimpan()
            End If
            nmbagian.Focus()
        End If
    End Sub
    Private Sub btnbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbatal.Click
        Call hapustext()
        kdbagian.Focus()
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        SQL = "update bagian set nmbagian='" & nmbagian.Text & "' " & _
                                 " where kdbagian='" & kdbagian.Text & "' "
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordbagian()
    End Sub

    Private Sub btnsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpan.Click
        SQL = "insert into bagian(kdbagian, nmbagian) " & " values('" & kdbagian.Text & "','" & nmbagian.Text & "' )"
        CMD = New OleDbCommand(SQL, DB)
        CMD.ExecuteNonQuery()
        Call btnbaru_Click(sender, e)
        Call tampilrecordbagian()
    End Sub

End Class




Nah ini dia hasil nya guys..
Tinggal kita runningkan aja.. ni udah nampak hasil nya













From Karyawan


















From Bagian
























From Jabatan




Ohh iyah,  semua from-from tadi di kerjakan dalam satu project ya gan..
ok, sekian dulu tutorial VB kali ini, semoga bermamfaat, terimakasih.



Tidak ada komentar:

Posting Komentar