Kamis, 21 November 2013

Form Supplier

Public Class Form_Supplier
    Dim proses As New ClsKoneksi
    Dim tbSupplier As DataTable
    Dim Query As String

    Private Sub Form_Supplier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        init_form()
        ToolTip1.SetToolTip(BtnAdd, "Add")
        ToolTip1.SetToolTip(BtnEdit, "Edit")
        ToolTip1.SetToolTip(BtnSave, "Save")
        ToolTip1.SetToolTip(BtnDelete, "Delete")
        ToolTip1.SetToolTip(BtnCancel, "Cancel")
        ToolTip1.SetToolTip(BtnExit, "Exit")


    End Sub
    Private Sub init_form()
        TxtId.Enabled = False
        TxtNama.Enabled = False
        TxtAlamat.Enabled = False
        TxtNoTelp.Enabled = False

        BtnAdd.Enabled = True
        BtnSave.Enabled = False
        BtnCancel.Enabled = False
        BtnDelete.Enabled = False
        BtnExit.Enabled = True
        BtnEdit.Enabled = False

        TxtId.Clear()
        TxtNama.Clear()
        TxtAlamat.Clear()
        TxtNoTelp.Clear()
        fill_grid()

    End Sub
    Private Sub add()
        TxtId.Enabled = False
        TxtNama.Enabled = True
        TxtAlamat.Enabled = True
        TxtNoTelp.Enabled = True

        BtnAdd.Enabled = False
        BtnSave.Enabled = True
        BtnCancel.Enabled = True
        BtnDelete.Enabled = False
        BtnExit.Enabled = True
        BtnEdit.Enabled = False
        auto_code()

        TxtNama.Focus()
    End Sub
    Private Function isNotEmpty()
        If (TxtId.Text = "" Or TxtNama.Text = "" Or TxtAlamat.Text = "" Or TxtNoTelp.Text = "") Then
            Return False
        Else
            Return True
        End If
    End Function
    Private Sub fill_grid()
        tbSupplier = proses.ExecuteQuery("select id_supplier as 'ID Supplier', nama_supplier as 'Nama Supplier', alamat_supplier as 'Alamat', no_telp as 'No Telepon' from tb_supplier")
        DgSupplier.DataSource = tbSupplier
    End Sub
    Private Sub save()
        If Not isNotEmpty() Then
            MsgBox("Data belum lengkap, Silahkan priksa kembali", MsgBoxStyle.Critical, "Error")
        Else
            tbSupplier = proses.ExecuteQuery("select id_supplier from tb_supplier where id_supplier = '" & TxtId.Text & "'")
            If tbSupplier.Rows.Count <> 0 Then
                MsgBox("Id Supplier " & TxtId.Text & " sudah ada", MsgBoxStyle.Critical, "Error")
            Else
                Query = "insert into tb_supplier values ('" & TxtId.Text & "','" & TxtNama.Text & "','" & TxtAlamat.Text & "','" & TxtNoTelp.Text & "')"

                Try
                    proses.ExecuteNonQuery(Query)
                    MsgBox("Data berhasil disimpan", MsgBoxStyle.Information, "Sukses")
                    init_form()
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try

            End If

        End If
    End Sub
    Private Sub fill_form(ByVal row As Integer)
        If row < 0 Then
            Exit Sub
        End If
        If Not DgSupplier.Rows(row).Cells(0).Value.ToString = "" Then
            BtnDelete.Enabled = True
            BtnEdit.Enabled = True
            BtnSave.Enabled = False
            BtnAdd.Enabled = False
            BtnCancel.Enabled = True

            TxtId.Enabled = False
            TxtNama.Enabled = True
            TxtAlamat.Enabled = True
            TxtNoTelp.Enabled = True

            TxtId.Text = DgSupplier.Rows(row).Cells(0).Value.ToString
            TxtNama.Text = DgSupplier.Rows(row).Cells(1).Value.ToString
            TxtAlamat.Text = DgSupplier.Rows(row).Cells(2).Value.ToString
            TxtNoTelp.Text = DgSupplier.Rows(row).Cells(3).Value.ToString
        End If
    End Sub
    Private Sub edit()
        If isNotEmpty() Then
            Query = "Update tb_supplier set nama_supplier = '" & TxtNama.Text & "',alamat_supplier = '" & TxtAlamat.Text & "',no_telp = '" & TxtNoTelp.Text & "' where id_supplier = '" & Trim(TxtId.Text) & "'"
            Try
                proses.ExecuteNonQuery(Query)
                MsgBox("Data berhasil diubah", MsgBoxStyle.Information, Title:="Information")
                Call init_form()
            Catch ex As Exception
            End Try
        End If
    End Sub
    Private Sub delete()
        If MessageBox.Show("anda yakin ingin hapus", "Konfimasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Return
        End If
        tbSupplier = proses.ExecuteQuery("Select merk_barang From tb_barang where merk_barang = '" & Trim(TxtId.Text) & "'")
        If tbSupplier.Rows.Count <> 0 Then
            MsgBox("Data sudah masuk transaksi tidak dapat dihapus ", MsgBoxStyle.Critical, Title:="Error")
            Return
        End If

        tbSupplier = proses.ExecuteQuery("Select id_supplier From tb_supplier where id_supplier = '" & Trim(TxtId.Text) & "'")
        If Not tbSupplier.Rows.Count = 0 Then
            Query = "delete from tb_supplier where id_supplier ='" & Trim(TxtId.Text) & "'"
            Try
                proses.ExecuteNonQuery(Query)
                MsgBox("Data berhasil dihapus ", MsgBoxStyle.Information, Title:="Information")
                init_form()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Sub auto_code()
        tbSupplier = proses.ExecuteQuery("select * from tb_supplier order by id_supplier desc")
        If tbSupplier.Rows.Count = 0 Then
            TxtId.Text = "SP001"
        Else
            With tbSupplier.Rows(0)
                TxtId.Text = .Item("id_supplier")

            End With

            TxtId.Text = Val(Microsoft.VisualBasic.Mid(TxtId.Text, 3, 3)) + 1
            If Len(TxtId.Text) = 1 Then
                TxtId.Text = "SP00" & TxtId.Text & ""
            ElseIf Len(TxtId.Text) = 2 Then
                TxtId.Text = "SP0" & TxtId.Text & ""
            ElseIf Len(TxtId.Text) = 3 Then
                TxtId.Text = "SP" & TxtId.Text & ""
            End If
        End If

    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        save()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        add()
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        edit()
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        delete()

    End Sub

    Private Sub DgSupplier_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgSupplier.CellContentClick

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        init_form()
    End Sub

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

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

    Private Sub DgSupplier_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgSupplier.CellClick
        fill_form(e.RowIndex)
    End Sub

    Private Sub Btnback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnback.Click
        Me.Close()

    End Sub
End Class

Tidak ada komentar:

Posting Komentar