PEMROGRAMAN VB.NET
Sintaks Pada Class
Hal : 1
MEMBUAT CLASS BARANG Tambahkan sebuah class dengan cara Klik kanan pada Project lalu pilih Add Class Atau klik menu Project Add Class Simpan dengan nama clsBarang Tambahkan code untuk menambahkan attribute Tambahkan code untuk mendefinisikan dirinya sebagai field (method properti)
Elly Yanuarti , M.Kom
2
1
Imports System.Data.OleDb Imports System.Data Public Class clsBarang #Region "Variabel" Private xkdbrg As String Private xnmbrg As String Private xsatuan As String Private xhrgsat As Integer Private xstok As Integer #End Region #Region "Property" Public Property kdbrg() As String Get Return xkdbrg End Get Set(ByVal value As String) xkdbrg = value End Set End Property (tambahkan property nmbrg, satuan, hrgsat dan stok) #End Region Elly Yanuarti , M.Kom
3
TAMBAHKAN METHOD “SIMPAN”
Tambahkan deklarasi variabel yang akan digunakan pada method simpan dan letakkan pada area/Region “Variabel” Dim query As String = “ ” Dim cmd As OleDbCommand
Tambahkan method simpan, ubah dan hapus dan ketik perintah berikut : #Region "Method" Public Function Simpan( ) As Integer query = "INSERT INTO BARANG (KDBRG,NMBRG,SATUAN,HRGSAT,STOK) " & _ "VALUES('" & xkdbrg & "','" & xnmbrg & "','" & xsatuan & "','" & xhrgsat & "', '" & xstok & "')" cmd = New OleDbCommand(query, conn) Return cmd.ExecuteNonQuery End Function #End Region Elly Yanuarti , M.Kom
4
2
TAMBAHKAN METHOD “UBAH” DAN METHOD “HAPUS” Public Function Ubah(ByVal varkdbrg As String) As Integer query = "UPDATE BARANG SET NMBRG = '" & xnmbrg & "', SATUAN='" & xsatuan & "', HRGSAT= '" & xhrgsat & "', STOK= '" & xstok & "'" & _ "WHERE KDBRG = '" & varkdbrg & "'" cmd = New OleDbCommand(query, conn) Return cmd.ExecuteNonQuery End Function Public Function Hapus(ByVal varkdbrg As String) As Integer query = "DELETE FROM BARANG WHERE KDBRG = '" & varkdbrg & "'" cmd = New OleDbCommand(query, conn) Return cmd.ExecuteNonQuery End Function #End Region
Elly Yanuarti , M.Kom
5
TAMBAHKAN METHOD “AUTOCODE”
Tambahkan deklarasi variabel yang akan digunakan pada method AutoKode dan letakkan pada area/Region “Variabel” Dim baca As OleDbDataReader Dim strtemp As String = "" Dim strvalue As String Dim no As Integer
Elly Yanuarti , M.Kom
6
3
SINTAKS KODE BARANG OTOMATIS #Region "Other Method" Public Sub AutoKode( ) query = "SELECT * FROM BARANG ORDER BY KDBRG DESC" cmd = New OleDbCommand(query, conn) baca = cmd.ExecuteReader If baca.Read Then strtemp = Mid(baca.Item("KDBRG"), 2, 3) Else xkdbrg = "B001" If baca.IsClosed = False Then baca.Close() Exit Sub End If strvalue = Val(strtemp) + 1 xkdbrg = Mid("B000", 1, 4 - strvalue.Length) & strvalue If baca.IsClosed = False Then baca.Close() End Sub Elly Yanuarti , M.Kom
7
TAMBAHKAN METHOD “TAMPILLIST” Public Sub TampilList( ) no = 1 cmd = New OleDbCommand("SELECT * FROM BARANG ORDER BY KDBRG ASC", conn) baca = cmd.ExecuteReader FormBarang.ListView1.Items.Clear() While baca.Read FormBarang.ListView1.Items.Add((no).ToString) FormBarang.ListView1.Items(no - 1).SubItems.Add(baca.Item("KDBRG").ToString) FormBarang.ListView1.Items(no - 1).SubItems.Add(baca.Item("NMBRG").ToString) FormBarang.ListView1.Items(no - 1).SubItems.Add(baca.Item("SATUAN").ToString) FormBarang.ListView1.Items(no - 1).SubItems.Add(baca.Item("HRGSAT").ToString) FormBarang.ListView1.Items(no - 1).SubItems.Add(baca.Item("STOK").ToString) no = no + 1 End While cmd.Dispose() baca.Close() End Sub #End Region Elly Yanuarti , M.Kom
8
4
PEMROGRAMAN VB.NET
Sintaks Pada Form Master
Hal : 9
MEMBUAT FUNCTION “KOSONG”
Tambahkan sebuah function pada FormBarang seperti berikut : Public Class FormBarang Dim ObjB As New clsBarang Private Sub kosong( ) ObjB.AutoKode( ) txtKdBrg.Text = ObjB.kdbrg txtKdBrg.Enabled = False txtNmBrg.Text = "" txtSatuan.Text = "" txtHrgSat.Text = "" txtStok.Text = "" txtNmBrg.Focus( ) ObjB.TampilList( ) btnSimpan.Enabled = True End Sub
Elly Yanuarti , M.Kom
10
5
MEMANGGIL FUNCTION “KOSONG”
Tambahkan code pada form barang untuk menampilkan data yang sudah tersimpan. Private Sub FormBarang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call kosong() End Sub
Elly Yanuarti , M.Kom
11
SINTAKS TOMBOL “SIMPAN”
Double click pada tombol simpan lalu ketik perintah berikut : Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click ObjB.kdbrg = txtKdBrg.Text ObjB.nmbrg = txtNmBrg.Text ObjB.satuan = txtSatuan.Text ObjB.hrgsat = txtHrgSat.Text ObjB.stok = txtStok.Text ObjB.Simpan() Call kosong() End Sub
Elly Yanuarti , M.Kom
12
6
SINTAKS TOMBOL “UBAH”
Double click pada tombol Ubah lalu ketik perintah berikut : Private Sub btnUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUbah.Click ObjB.nmbrg = txtNmBrg.Text ObjB.satuan = txtSatuan.Text ObjB.hrgsat = txtHrgSat.Text ObjB.stok = txtStok.Text ObjB.Ubah(txtKdBrg.Text) kosong( ) End Sub
Elly Yanuarti , M.Kom
13
SINTAKS TOMBOL “HAPUS”
Double click pada tombol hapus lalu ketik perintah berikut : Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click Dim pesan As MsgBoxResult pesan = MsgBox("Yakin Akan Menghapus Data ?", MsgBoxStyle.YesNo + 32, "Penting !!") If pesan = MsgBoxResult.Yes Then ObjB.Hapus(txtKdBrg.Text) kosong( ) End If End Sub
Elly Yanuarti , M.Kom
14
7
SINTAKS TOMBOL “BATAL” DAN TOMBOL “KELUAR”
Double click pada tombol Batal lalu ketik perintah berikut : Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click Call kosong( ) End Sub
Double click pada tombol Keluar lalu ketik perintah berikut : Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click Close( ) End Sub
Elly Yanuarti , M.Kom
15
MENAMPILKAN DATA BARANG YANG SUDAH ADA
Double click pada listview lalu ketik perintah berikut : Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged txtKdBrg.Text = ListView1.FocusedItem.SubItems(1).Text txtNmBrg.Text = ListView1.FocusedItem.SubItems(2).Text txtSatuan.Text = ListView1.FocusedItem.SubItems(3).Text txtHrgSat.Text = ListView1.FocusedItem.SubItems(4).Text txtStok.Text = ListView1.FocusedItem.SubItems(5).Text txtKdBrg.Enabled = False txtNmBrg.Focus( ) btnSimpan.Enabled = False End Sub
Elly Yanuarti , M.Kom
16
8