Thursday, September 15, 2016

Visual Basic (Form Barang)

Design Barang :
Codding Barang :
Imports System.Data.Odbc

Public Class Barang
   Sub Kosongkan()
       TKode.Text = ""
       TNama.Text = ""
       TBeli.Text = ""
       TJual.Text = ""
       TJumlah.Text = ""
       CmbSatuan.Text = ""
       TKode.Focus()
   End Sub
   Sub DataBaru()
       TNama.Text = ""
       TBeli.Text = ""
       TJual.Text = ""
       TJumlah.Text = ""
       CmbSatuan.Text = ""
       CmbSatuan.Focus()
   End Sub
   Sub Tampilkan()
       da = New OdbcDataAdapter("Select * from barang", Conn)
       ds = New DataSet
       ds.Clear()
       da.Fill(ds, "barang")
       DGV.DataSource = (ds.Tables("barang"))
       DGV.ReadOnly = True
   End Sub
   Sub TampilSatuan()
       CmbSatuan.Items.Clear()
       cmd = New OdbcCommand("select distinct satuan from barang", Conn)
       rd = cmd.ExecuteReader
       While rd.Read
           CmbSatuan.Items.Add(rd.GetString(0))
       End While
   End Sub
   Private Sub NamaBarang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Call Koneksi()
       Call Tampilkan()
       Call TampilSatuan()
   End Sub

   Private Sub TKode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TKode.KeyPress
       If e.KeyChar = Chr(13) Then
           cmd = New OdbcCommand("select * from barang where Kode_barang='" & TKode.Text & "'", Conn)
           rd = cmd.ExecuteReader
           rd.Read()
           If rd.HasRows = True Then
               TNama.Text = rd.GetString(1)
               TBeli.Text = rd.GetValue(2)
               TJual.Text = rd.GetValue(3)
               TJumlah.Text = rd.GetValue(4)
               CmbSatuan.Text = rd.GetString(5)
               TNama.Focus()
           Else
               Call DataBaru()
               TNama.Focus()
           End If
       End If
       If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True
   End Sub
   Private Sub CmbSatuan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CmbSatuan.KeyPress
       If e.KeyChar = Chr(13) Then btnSimpan.Focus()
   End Sub

   Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
       If TKode.Text = "" Or TNama.Text = "" Or TBeli.Text = "" Or TJual.Text = "" Or TJumlah.Text = "" Or CmbSatuan.Text = "" Then
           MsgBox("Data Belum Lengkap")
           Exit Sub
       Else
           cmd = New OdbcCommand("Select * from barang where Kode_barang='" & TKode.Text & "'", Conn)
           rd = cmd.ExecuteReader
           rd.Read()
           If Not rd.HasRows Then
               Dim sqltambah As String = "Insert into barang(Kode_barang,Nama_barang,Harga_beli,Harga_jual,Jumlah_barang,Satuan) values " & _
               "('" & TKode.Text & "','" & TNama.Text & "','" & TBeli.Text & "','" & TJual.Text & "','" & TJumlah.Text & "','" & CmbSatuan.Text & "')"
               cmd = New OdbcCommand(sqltambah, Conn)
               cmd.ExecuteNonQuery()
               Call Kosongkan()
               Call Tampilkan()
           Else
               Dim sqledit As String = "Update barang set " & _
               "Nama_barang='" & TNama.Text & "', " & _
               "Harga_beli='" & TBeli.Text & "', " & _
               "Harga_jual='" & TJual.Text & "', " & _
               "Jumlah_barang='" & TJumlah.Text & "', " & _
               "Satuan='" & CmbSatuan.Text & "' " & _
               "where Kode_barang='" & TKode.Text & "'"
               cmd = New OdbcCommand(sqledit, Conn)
               cmd.ExecuteNonQuery()
               Call Kosongkan()
               Call Tampilkan()
           End If
       End If
   End Sub

   Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
       Call Kosongkan()
   End Sub

   Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Me.Close()
   End Sub

   Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
       If TKode.Text = "" Then
           MsgBox("Isi kode barang terlebih dahulu")
           TKode.Focus()
           Exit Sub
       Else
           If MessageBox.Show("Yakin akan dihapus..?", "Konfirmasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
               cmd = New OdbcCommand("Delete from barang where Kode_barang='" & TKode.Text & "'", Conn)
               cmd.ExecuteNonQuery()
               Call Kosongkan()
               Call Tampilkan()
           Else
               Call Kosongkan()
           End If
       End If
   End Sub

   Private Sub TKode_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TKode.Validated
       cmd = New OdbcCommand("select * from barang where Kode_barang='" & TKode.Text & "'", Conn)
       rd = cmd.ExecuteReader
       rd.Read()
       If rd.HasRows = True Then
           If rd.HasRows = True Then
               TNama.Text = rd.GetString(1)
               TBeli.Text = rd.GetValue(2)
               TJual.Text = rd.GetValue(3)
               TJumlah.Text = rd.GetValue(4)
               CmbSatuan.Text = rd.GetString(5)
               TNama.Focus()
           Else
               Call DataBaru()
               TNama.Focus()
           End If
       End If
   End Sub

   Private Sub TBeli_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TBeli.KeyPress
       If e.KeyChar = Chr(13) Then TJual.Focus()
       If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True
   End Sub

   Private Sub TBeli_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBeli.TextChanged
       With Me.TBeli ' mengatur format angka pada saat diinput
           If (.Text >= "0" And .Text <= "9") Then .Text = FormatNumber(.Text, 0)
           .SelectionStart = .Text.Length
       End With
   End Sub

   Private Sub TJual_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TJual.KeyPress
       If e.KeyChar = Chr(13) Then TJumlah.Focus()
       If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True
   End Sub

   Private Sub TJual_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TJual.TextChanged
       With Me.TJual ' mengatur format angka pada saat diinput
           If (.Text >= "0" And .Text <= "9") Then .Text = FormatNumber(.Text, 0)
           .SelectionStart = .Text.Length
       End With
   End Sub

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

   Private Sub TJumlah_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TJumlah.KeyPress
       If e.KeyChar = Chr(13) Then CmbSatuan.Focus()
       If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True
   End Sub
   Private Sub btnTutup_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
       Me.Close()
   End Sub
End Class


Previous Post
Next Post

0 Post a Comment: