Sie sind auf Seite 1von 6

FORM INPUT BARANG

1
Lain lain :

1. Untuk form1, properties :


- FormBorderStyle : FixedDialog
- KeyPreview : True
- MaximizeBox : False
- MinimizeBox : False
- Size : 607;581
- StartPosition : CenterScreen
2. Design menyesuaikan.

KODING
Imports System.Data.Odbc
Public Class frmbarang
Sub tampil()
Call koneksi()
da = New OdbcDataAdapter("select*from Barang order by Kodebrg", conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, 0)
dgv.DataSource = ds.Tables(0)
conn.Close()
End Sub

Sub batal()
txtkode.Text = ""
txtnama.Text = ""
cbsatuan.Text = ""
txtstok.Text = ""
cmdubah.Enabled = False
cmdhapus.Enabled = False
cmdsimpan.Enabled = True
txtnama.Enabled = False
cbsatuan.Enabled = False
txtstok.Enabled = False
txtcari.Text = ""
txtkode.Focus()
End Sub

Sub kolom()
dgv.Columns(0).Width = 110
dgv.Columns(1).Width = 200
dgv.Columns(2).Width = 100
dgv.Columns(3).Width = 80
End Sub

Sub nama()
dgv.Columns(0).HeaderText = "Kode Barang"
dgv.Columns(1).HeaderText = "Nama Barang"
dgv.Columns(2).HeaderText = "Satuan"
dgv.Columns(3).HeaderText = "Stok"
End Sub

2
Sub item()
txttotal.ReadOnly = True
txttotal.Text = 0
End Sub

Sub ketemu()
Dim i As Integer
i = Me.dgv.CurrentRow.Index
With dgv.Rows.Item(i)
txtkode.Text = .Cells(0).Value
txtnama.Text = .Cells(1).Value
cbsatuan.Text = .Cells(2).Value
txtstok.Text = .Cells(3).Value
End With
End Sub

Sub totalitem()
Dim item As Integer = 0
item = dgv.RowCount - 1
txttotal.Text = item
End Sub

Private Sub frmbarang_Load(sender As Object, e As EventArgs) Handles MyBase.Load


Call batal()
Call tampil()
Call koneksi()
Call kolom()
Call nama()
Call item()
Call totalitem()
labelcari.Visible = False
End Sub

Private Sub frmbarang_FormClosing(sender As Object, e As FormClosingEventArgs) Handles


Me.FormClosing
If MsgBox("Apakah anda yakin ingin keluar?", MsgBoxStyle.YesNo + MsgBoxStyle.Question,
"Konfirmasi") = MsgBoxResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub

Private Sub frmbarang_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown


If e.KeyCode = Keys.F2 Then
cmdsimpan.PerformClick()
ElseIf e.KeyCode = Keys.F3 Then
cmdubah.PerformClick()
ElseIf e.KeyCode = Keys.Delete Then
cmdhapus.PerformClick()
ElseIf e.KeyCode = Keys.Escape Then
cmdbatal.PerformClick()
End If
End Sub

Private Sub txtkode_TextChanged(sender As Object, e As EventArgs) Handles txtkode.TextChanged


labelcari.Visible = True
If txtkode.Text = "" Then
labelcari.Visible = False
End If
End Sub

3
Private Sub txtkode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtkode.KeyDown
If e.KeyCode = Keys.Enter Then
Call koneksi()
str = "select*from Barang where Kodebrg='" & txtkode.Text & "'"
cmd = New OdbcCommand(str, conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
Call ketemu()
If MsgBox("Barang dengan kode " & txtkode.Text & " sudah ada, apakah anda ingin
menghapus/mengubah data?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Perhatian") = MsgBoxResult.Yes
Then
cmdsimpan.Enabled = False
cmdubah.Enabled = True
cmdhapus.Enabled = True
txtnama.Enabled = True
cbsatuan.Enabled = True
txtstok.Enabled = True
labelcari.Visible = False
txtkode.Focus()
Else
Call batal()
End If
Else
txtnama.Enabled = True
cbsatuan.Enabled = True
txtstok.Enabled = True
txtnama.Focus()
labelcari.Visible = False
End If
conn.Close()
End If
End Sub

Private Sub txtnama_KeyDown(sender As Object, e As KeyEventArgs) Handles txtnama.KeyDown


If e.KeyCode = Keys.Enter Then
cbsatuan.Focus()
End If
End Sub

Private Sub cbsatuan_Click(sender As Object, e As EventArgs) Handles cbsatuan.Click


cbsatuan.Items.Add("Buah")
cbsatuan.Items.Add("Pcs")
cbsatuan.Items.Add("Rim")
cbsatuan.Items.Add("Unit")
cbsatuan.Items.Add("Pack")
cbsatuan.Items.Add("Lusin")
End Sub

Private Sub combosatuan_KeyDown(sender As Object, e As KeyEventArgs) Handles cbsatuan.KeyDown


If e.KeyCode = Keys.Enter Then
txtstok.Focus()
End If
End Sub

Private Sub txtstok_KeyDown(sender As Object, e As KeyEventArgs) Handles txtstok.KeyDown


If e.KeyCode = Keys.Enter Then
cmdsimpan.PerformClick()
End If
End Sub

4
Private Sub cmdsimpan_Click(sender As Object, e As EventArgs) Handles cmdsimpan.Click
Call koneksi()
If txtkode.Text = "" Or txtnama.Text = "" Or cbsatuan.Text = "" Or txtstok.Text = "" Then
MsgBox("Harap lengkapi data terlebih dahulu!", MsgBoxStyle.Exclamation, "Perhatian")
txtkode.Focus()
Else
str = "insert into barang values " & "('" & txtkode.Text & "','" & txtnama.Text & "','"
& cbsatuan.Text & "','" & txtstok.Text & "')"
cmd = New OdbcCommand(str, conn)
cmd.ExecuteNonQuery()
MsgBox("Data berhasil disimpan!", MsgBoxStyle.Information, "Input Barang")
Call tampil()
Call batal()
Call totalitem()
End If
conn.Close()
End Sub

Private Sub cmdubah_Click(sender As Object, e As EventArgs) Handles cmdubah.Click


Call koneksi()
If txtkode.Text = "" Or txtnama.Text = "" Or cbsatuan.Text = "" Or txtstok.Text = "" Then
MsgBox("Harap lengkapi data terlebih dahulu!", MsgBoxStyle.Exclamation, "Perhatian")
txtkode.Focus()
Else
str = "update barang set namabrg='" & txtnama.Text & "',satuan='" & cbsatuan.Text &
"',stok='" & txtstok.Text & "'where kodebrg='" & txtkode.Text & "'"
cmd = New OdbcCommand(str, conn)
cmd.ExecuteNonQuery()
MsgBox("Data berhasil diubah!", MsgBoxStyle.Information, "Input Barang")
Call tampil()
Call batal()
Call totalitem()
End If
conn.Close()
End Sub

Private Sub cmdhapus_Click(sender As Object, e As EventArgs) Handles cmdhapus.Click


Call koneksi()
If txtkode.Text = "" Or txtnama.Text = "" Or cbsatuan.Text = "" Or txtstok.Text = "" Then
MsgBox("Harap lengkapi data terlebih dahulu!", MsgBoxStyle.Exclamation, "Perhatian")
txtkode.Focus()
Else
If MsgBox("Apakah anda yakin ingin menghapus data?", MsgBoxStyle.YesNo +
MsgBoxStyle.Question, "Konfirmasi") = MsgBoxResult.Yes Then
str = "delete from barang where kodebrg='" & txtkode.Text & "'"
cmd = New OdbcCommand(str, conn)
cmd.ExecuteNonQuery()
MsgBox("Data berhasil dihapus!", MsgBoxStyle.Information, "Input Barang")
Call tampil()
Call batal()
Call totalitem()
Else
Call batal()
Call totalitem()
End If
End If
conn.Close()
End Sub

Private Sub cmdbatal_Click(sender As Object, e As EventArgs)


Call batal()
Call tampil()
End Sub

5
Private Sub txtcari_TextChanged(sender As Object, e As EventArgs) Handles txtcari.TextChanged
Call koneksi()
str = "select*from barang where namabrg like '%" & txtcari.Text & "%'"
cmd = New OdbcCommand(str, conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
da = New OdbcDataAdapter(str, conn)
ds = New DataSet
da.Fill(ds, "barang")
dgv.DataSource = ds.Tables("barang")
dgv.ReadOnly = True
End If
conn.Close()
End Sub

Private Sub dgv_MouseClick(sender As Object, e As MouseEventArgs) Handles dgv.MouseClick


Call ketemu()
txtnama.Enabled = True
cbsatuan.Enabled = True
txtstok.Enabled = True
cmdhapus.Enabled = True
cmdubah.Enabled = True
txtkode.Focus()
cmdsimpan.Enabled = False
labelcari.Visible = False
End Sub
End Class

Das könnte Ihnen auch gefallen