You are on page 1of 5

Imports System.Data.

OleDb
Public Class Barang
Sub KodeOtomatis()
cmd = New OleDbCommand("select Kd_Barang from Tabel_Barang order by Kd_Barang
desc", conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
txtkodebarang.Text = "BRG01"
Else
txtkodebarang.Text = "BRG" +
Format(Microsoft.VisualBasic.Right(rd.Item("Kd_Barang"), 2) + 1, "00")
End If
End Sub
Sub isigrid()
koneksi()
da = New OleDb.OleDbDataAdapter("SELECT * FROM Tabel_Barang ", conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "Tabel_Barang")
DataGridView1.DataSource = (ds.Tables("Tabel_Barang"))
DataGridView1.Enabled = True
End Sub
Sub mati()
txtkodebarang.Enabled = False
txtnamabarang.Enabled = False
txthargabeli.Enabled = False
txthargajual.Enabled = False
txtsatuan.Enabled = False
txtstok.Enabled = False
dtmkadaluarsa.Enabled = False
End Sub
Sub hidup()
txtkodebarang.Enabled = True
txtnamabarang.Enabled = True
txthargabeli.Enabled = True
txthargajual.Enabled = True
txtsatuan.Enabled = True
txtstok.Enabled = True
dtmkadaluarsa.Enabled = True
End Sub
Sub kosong()
txtkodebarang.Text = ""
txtnamabarang.Text = ""
txthargabeli.Text = ""
txthargajual.Text = ""
txtsatuan.Text = ""
txtstok.Text = ""
dtmkadaluarsa.Text = ""
End Sub
Private Sub Barang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
mati()
isigrid()
End Sub
Private Sub TampilData()
Using Conn As New OleDbConnection(My.Settings.Aplikasi_Penjualan)
Using Adp As New OleDbDataAdapter("Select * From Tabel_Barang", Conn)
Conn.Open()
Dim Tabel As New DataTable
Adp.Fill(Tabel)
DataGridView1.DataSource = Tabel
End Using
End Using
End Sub

// Double Klik Tombol Tambah


Private Sub CMDTAMBAH_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDTAMBAH.Click
CMDTAMBAH.Enabled = False
Call hidup()
KodeOtomatis()
txtkodebarang.Focus()
CMDEDIT.Enabled = False
CMDSIMPAN.Enabled = True
CMDUPDATE.Enabled = False
CMDHAPUS.Enabled = False
CMDKELUAR.Enabled = True
End Sub

// Double Klik Tombol Edit


Private Sub CMDEDIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles CMDEDIT.Click
Call hidup()
txtkodebarang.Focus()
CMDHAPUS.Enabled = False
CMDEDIT.Enabled = False
CMDUPDATE.Enabled = True
End Sub

// Double Klik Tombol Hapus


Private Sub CMDHAPUS_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDHAPUS.Click
Dim Hapus As String
Dim pesan As Integer
pesan = MsgBox("Apakah anda yakin Ingin Menghapus " + txtkodebarang.Text,
vbExclamation + vbYesNo, "perhatian")
If pesan = vbNo Then Exit Sub
Hapus = "DELETE FROM Tabel_Barang WHERE Kd_Barang='" & txtkodebarang.Text & "'"
jalankansql(Hapus)
Me.Cursor = Cursors.WaitCursor
DataGridView1.Refresh()
isigrid()
Me.Cursor = Cursors.Default
kosong()
mati()
CMDHAPUS.Enabled = False
CMDEDIT.Enabled = False
End Sub

// Double Klik Tombol Simpan


Private Sub CMDSIMPAN_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDSIMPAN.Click
Dim Simpan As String
If txtkodebarang.Text = "" Or txtnamabarang.Text = "" Or txthargabeli.Text = ""
Or txthargajual.Text = "" Or txtsatuan.Text = "" Or txtstok.Text = "" Or
dtmkadaluarsa.Text = "" Then
MsgBox("Data belum lengkap, Pastikan Kode Barang 5 Digit dan Semua Textbox
terisi")
Exit Sub
Else
Call koneksi()
cmd = New OleDbCommand("Select * from Tabel_Barang where Kd_Barang='" &
txtkodebarang.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
Call koneksi()
Simpan = "insert into Tabel_Barang values ('" & txtkodebarang.Text &
"','" & txtnamabarang.Text & "','" & txthargabeli.Text & "','" & txthargajual.Text &
"','" & txtsatuan.Text & "','" & txtstok.Text & "','" & dtmkadaluarsa.Text & "')"
jalankansql(Simpan)
DataGridView1.Refresh()
isigrid()
kosong()
mati()
CMDTAMBAH.Enabled = True
CMDSIMPAN.Enabled = False
MsgBox("Data berhasil di Simpan", MsgBoxStyle.Information, "Information")
Else
MsgBox("Kode Barang Sudah Pernah Ada!!!!")
kosong()
End If
End If
End Sub
Private Sub isiTextBox(ByVal x As Integer)
Try
txtkodebarang.Text = DataGridView1.Rows(x).Cells(0).Value
txtnamabarang.Text = DataGridView1.Rows(x).Cells(1).Value
txthargabeli.Text = DataGridView1.Rows(x).Cells(2).Value
txthargajual.Text = DataGridView1.Rows(x).Cells(3).Value
txtsatuan.Text = DataGridView1.Rows(x).Cells(4).Value
txtstok.Text = DataGridView1.Rows(x).Cells(5).Value
dtmkadaluarsa.Text = DataGridView1.Rows(x).Cells(6).Value
Catch ex As Exception
End Try
End Sub

// Double Klik DataGridViw dan pilih Cellclik


Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
isiTextBox(e.RowIndex)
End Sub
Private Sub jalankansql(ByVal sQl As String)
Dim objcmd As New System.Data.OleDb.OleDbCommand
Call koneksi()
Try
objcmd.Connection = conn
objcmd.CommandType = CommandType.Text
objcmd.CommandText = sQl
objcmd.ExecuteNonQuery()
objcmd.Dispose()
MsgBox("Berhasil Menyimpan !", vbInformation)
Catch ex As Exception
MsgBox("Gagal Menyimmpan ! " & ex.Message)
End Try
End Sub

// Double Klik DataGridView dan pilih CellContentClick


Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
CMDTAMBAH.Enabled = True
CMDSIMPAN.Enabled = False
CMDHAPUS.Enabled = True
CMDEDIT.Enabled = True
txtkodebarang.Focus()
End Sub

// Double Klik Tombol Update


Private Sub CMDUPDATE_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDUPDATE.Click
Dim perbaiki As String
perbaiki = "update Tabel_Barang set " + "Kd_Barang = '" & txtkodebarang.Text &
"', " + "Nama_Barang = '" & txtnamabarang.Text & "', " + "Harga_Beli = '" &
txthargabeli.Text & "', " + "Harga_Jual = '" & txthargajual.Text & "', " + "Satuan = '" &
txtsatuan.Text & "', " + "Stok = '" & txtstok.Text & "', " + "Tgl_Kadaluarsa = '" &
dtmkadaluarsa.Text & "' where Kd_Barang = '" & txtkodebarang.Text & "'"
jalankansql(perbaiki)
DataGridView1.Refresh()
isigrid()
kosong()
mati()
CMDUPDATE.Enabled = False
CMDEDIT.Enabled = True
End Sub

// Double Klik Tombol Edit


Private Sub CMDBATAL_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDBATAL.Click
Call hidup()
Call aktifkantombol()
End Sub
Sub aktifkantombol()
CMDTAMBAH.Enabled = True
CMDSIMPAN.Enabled = True
CMDHAPUS.Enabled = True
CMDEDIT.Enabled = True
CMDUPDATE.Enabled = True
CMDKELUAR.Enabled = True

End Sub

// Double Klik Tombol Keluar


Private Sub CMDKELUAR_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CMDKELUAR.Click
Me.Hide()
End Sub

Private Sub CMDCARI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles CMDCARI.Click
Dim cari As String
cari = "Select * from Tabel_Barang where Kd_Barang like '%" & txtcari.Text & "%'"
da = New OleDbDataAdapter(cari, conn)
Dim SRT As New DataTable
SRT.Clear()
da.Fill(SRT)
DataGridView1.DataSource = SRT
End Sub

// Double Klik TextBox Kode Barang


Private Sub txtkodebarang_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtkodebarang.KeyPress
If (e.KeyChar = Chr(13)) Then
txtnamabarang.Focus()
End If
End Sub

// Double Klik TextBox Nama Barang


Private Sub txtnamabarang_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtnamabarang.KeyPress
If (e.KeyChar = Chr(13)) Then
txthargabeli.Focus()
End If
End Sub

// Double Klik TextBox harga Barang


Private Sub txthargabeli_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txthargabeli.KeyPress
If (e.KeyChar = Chr(13)) Then
txthargajual.Focus()
End If
End Sub
End Class

You might also like