Translate

Senin, 17 November 2014

Cara Membuat Sales Order

Public Class SO
    Dim baris, kolom, Hargasatuan As Integer
    Dim kodebrg, namabrg, statusheader As String


    Private Sub SO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call koneksi()
        Call isicombo()
        Call settingdatagrid()
        Call tampilkan_detail_SO()



    End Sub
    Private Sub simpandata()
        If Trim(TextBox1.Text) = "" Then Exit Sub
        If Trim(TextBox2.Text) = "" Then Exit Sub
        If Trim(ComboBox1.Text) = "" Then Exit Sub
        If Trim(TextBox4.Text) = "" Then Exit Sub
        'If Trim(TextBox5.Text) = "" Then Exit Sub


        If Add.Text = "Simpan" Then
            Tampil.Connection = Dtbase
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "insert into SalesOrder(NoSO,tglSO,kdcust,carabayar,total,nopo,tglEntry, useridd) values ('" & Trim(TextBox1.Text) & "', '" & DateTimePicker1.Value & "','" & TextBox2.Text & "','" & Microsoft.VisualBasic.Left(ComboBox1.Text, 2) & "'," & Val(TextBox5.Text) & ",'" & TextBox4.Text & "','" & tglskg & "', '" & nmorang & "' )"
            Tampil.ExecuteNonQuery()
        Else
            If Edit.Text = "Simpan" Then
                Tampil.Connection = Dtbase
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update SalesOrder set tglSO='" & DateTimePicker1.Value & "',kdcust='" & TextBox2.Text & "',carabayar='" & Microsoft.VisualBasic.Left(ComboBox1.Text, 2) & "',total=" & Val(TextBox5.Text) & ",nopo='" & Trim(TextBox4.Text) & "',tglEntry='" & tglskg & "', useridd='" & nmorang & "' where NoSO='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()

            Else
                If Delete.Text = "Ok" Then
                    'Untuk menghapus detail SO
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SODetail where nosoH='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()
                    'Untuk menghapus header SO
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()
                    Call settingdatagrid()


                Else

                End If
            End If

        End If

    End Sub
    Private Sub caristatusheader()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil2.ExecuteReader

        If Tampilkan.HasRows = True Then
            statusheader = "T"
        Else
            statusheader = "F"

        End If
    End Sub

    Private Sub bersih()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        ComboBox1.Text = ""
    End Sub
    Private Sub normal()
        Add.Text = "Add"
        Edit.Text = "Edit"
        Delete.Text = "Delete"
        Keluar.Text = "Exit"
        Add.Enabled = True
        Edit.Enabled = True
        Delete.Enabled = True
        Keluar.Text = "Exit"
    End Sub

    Private Sub isicombo()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Carabayar "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            ComboBox1.Items.Clear()
            While Tampilkan.Read() 'membaca data 1 per 1
                ComboBox1.Items.Add(Trim(Tampilkan("kdcarabyr")) & " " & Trim(Tampilkan("nmcarabyr")))
            End While
        End If
    End Sub
    Private Sub posisiList2()
        ListBox2.Top = TextBox2.Top + TextBox2.Height
        ListBox2.Left = TextBox2.Left
    End Sub

    Private Sub caricustomer()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Customer where nmcust like '%" & Trim(TextBox2.Text) & "%' "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            ListBox2.Items.Clear()
            ListBox2.Visible = True
            Call posisiList2()
            While Tampilkan.Read() 'membaca data 1 per 1
                ListBox2.Items.Add(Trim(Tampilkan("kdcust")) & " " & Trim(Tampilkan("nmcust")))
            End While
        End If

    End Sub
    Private Sub carinamacustomer()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Customer where kdcust ='" & Trim(TextBox2.Text) & "' "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                TextBox3.Text = Trim(Tampilkan("nmcust"))
            End While
        End If

    End Sub
    Private Sub settingdatagrid()
        'untuk menentukan jumlah kolom
        DG1.ColumnCount = 5
        DG1.RowCount = 10

        'untuk menentukan nama header kolom
        DG1.Columns(0).Name = "Kode Barang"
        DG1.Columns(1).Name = "Nama Barang"
        DG1.Columns(2).Name = "Qty"
        DG1.Columns(3).Name = "Satuan Harga"
        DG1.Columns(4).Name = "Total Harga"

        'menentukan lebar kolom
        DG1.Columns(0).Width = 120
        DG1.Columns(1).Width = 250
        DG1.Columns(2).Width = 100
        DG1.Columns(3).Width = 100
        DG1.Columns(4).Width = 100

        Call AddButtonColumn()

        'format kolom sesuai dengan keingninan
        DG1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
        DG1.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
    End Sub
    Private Sub masukandata()
        'untuk memberishkan satu baris datagridview
        DG1.Rows.Item(baris).Cells(0).Value = " "
        DG1.Rows.Item(baris).Cells(1).Value = " "
        DG1.Rows.Item(baris).Cells(2).Value = " "
        DG1.Rows.Item(baris).Cells(3).Value = " "
        DG1.Rows.Item(baris).Cells(4).Value = " "
        DG1.Rows.Item(baris).Cells(5).Value = " "
    End Sub
    Private Sub AddButtonColumn()
        'untuk membuat tombol button di datagridview
        Dim btn = New DataGridViewButtonColumn()
        DG1.Columns.Add(btn)
        btn.HeaderText = "Simpan"
        btn.Text = "Simpan"
        btn.Name = "btnsimpan"
        btn.UseColumnTextForButtonValue = True
    End Sub
    Private Sub bersih_datagrid()
        DG1.RowCount = 1
        DG1.Refresh()
        DG1.RowCount = 10
    End Sub
    Private Sub posisiList()
        ListBox1.Top = GroupBox1.Location.Y + DG1.Top + DG1.Location.Y + (DG1.Rows(0).Height * (baris + 1)) + 2
        ListBox1.Left = GroupBox1.Location.X + DG1.Location.X + 41
    End Sub
    Private Sub caridatabarang()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from Barang where kdbrg='" & Trim(kodebrg) & "'"
        Tampilkan = Tampil.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                If IsDBNull(Tampilkan("kdbrg")) Then '

                Else
                    'memberikan nilai di cel datagridview
                    DG1.Rows.Item(baris).Cells(1).Value = Trim(Tampilkan("nmbrg"))
                    DG1.Rows.Item(baris).Cells(3).Value = Tampilkan("hrgsatuan")
                End If
            End While
        End If
    End Sub

    Private Sub tampil_data_barang()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Barang where nmbrg like '%" & Trim(namabrg) & "%'"
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data
        If Tampilkan.HasRows = True Then 'klw ada record(data) maka akan bertipe true
            ListBox1.Items.Clear()
            ListBox1.Visible = True
            While Tampilkan.Read() 'membaca data satu persatu
                ListBox1.Items.Add(Trim(Tampilkan("kdbrg")) & "" & Trim(Tampilkan("nmbrg")))
            End While
        End If
    End Sub

    Private Sub simpan_datagridview()
        'cek sudah terisi semua belum
        If DG1.Rows.Item(baris).Cells(0).Value = "" Then Exit Sub
        If DG1.Rows.Item(baris).Cells(1).Value = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(2).Value) = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(3).Value) = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(4).Value) = "" Then Exit Sub

        If Add.Text = "Simpan" Then
            Tampil.Connection = Dtbase
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "insert into SODetail(nosoH,kdbrg,qty,hargasatuan,totalharga,tglEntry,useridd) values ('" & Trim(TextBox1.Text) & "', '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'," & Val(DG1.Rows.Item(baris).Cells(2).Value) & "," & Val(DG1.Rows.Item(baris).Cells(3).Value) & "," & Val(DG1.Rows.Item(baris).Cells(4).Value) & ",'" & tglskg & "', '" & nmorang & "')"
            Tampil.ExecuteNonQuery()
            ' Call tampilkan_detail_SO()
        Else
            If Edit.Text = "Simpan" Then
                Tampil.Connection = Dtbase
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update SODetail set qty = " & Val(DG1.Rows.Item(baris).Cells(2).Value) & ",hargasatuan=" & Val(DG1.Rows.Item(baris).Cells(3).Value) & ",totalharga =" & Val(DG1.Rows.Item(baris).Cells(4).Value) & " where nosoH='" & Trim(TextBox1.Text) & "' and kdbrg= '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'"
                Tampil.ExecuteNonQuery()
                'Call tampilkan_detail_SO()
            Else
                If Delete.Text = "Ok" Then
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SODetail where nosoH='" & Trim(TextBox1.Text) & "' and kdbrg= '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'"
                    Tampil.ExecuteNonQuery()
                    Call bersihbaris()
                End If
            End If
        End If
    End Sub
    Private Sub bersihbaris()
        DG1.Rows.Item(baris).Cells(0).Value = ""
        DG1.Rows.Item(baris).Cells(1).Value = ""
        DG1.Rows.Item(baris).Cells(2).Value = ""
        DG1.Rows.Item(baris).Cells(3).Value = ""
        DG1.Rows.Item(baris).Cells(4).Value = ""
    End Sub
    Private Sub tampilkan_detail_SO()
        Call koneksi()
        Dim n As Integer
        DG1.RowCount = 1
        DG1.RowCount = 10

        Tabel = "SELECT SODetail.kdbrg, Barang.nmbrg, SODetail.qty, SODetail.hargasatuan, SODetail.totalharga"
        Tabel = Tabel + "  FROM SODetail INNER JOIN Barang ON SODetail.kdbrg = Barang.kdbrg WHERE  SODetail.nosoH= '" & Trim(TextBox1.Text) & "'"
        Tampil4.Connection = Dtbase
        Tampil4.CommandType = CommandType.Text
        'MsgBox(Tabel)
        Tampil4.CommandText = Tabel
        Tampilkan = Tampil4.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            n = 0
            While Tampilkan.Read() 'membaca data 1 per 1

                DG1.Rows.Item(n).Cells(0).Value = Tampilkan("kdbrg")
                'MsgBox(DG1.Rows.Item(n).Cells(0).Value)
                DG1.Rows.Item(n).Cells(1).Value = Trim(Tampilkan("nmbrg"))
                DG1.Rows.Item(n).Cells(2).Value = Tampilkan("qty")
                DG1.Rows.Item(n).Cells(3).Value = Tampilkan("hargasatuan")
                DG1.Rows.Item(n).Cells(4).Value = Tampilkan("totalharga")

                n = n + 1

            End While
            'dgv1.RowCount = n + 1
            'Dim nn As Integer = dgv1.Rows.Add()
        End If
        'Dim nn As Integer = dgv1.Rows.Add()

        Tampilkan.Close()
        Tampil4.ExecuteReader.Close()
    End Sub


    Private Sub DG1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellClick
        ' Untuk menunjukan baris yang ke berapa
        baris = DG1.CurrentRow.Index

        'untuk menentukan kolom yang mana
        If DG1.Columns(e.ColumnIndex).Name = "Kode Barang" Then
            kolom = 1
        Else
            If DG1.Columns(e.ColumnIndex).Name = "Nama Barang" Then
                kolom = 2
            Else
                If DG1.Columns(e.ColumnIndex).Name = "Harga" Then
                    kolom = 3
                Else
                    If DG1.Columns(e.ColumnIndex).Name = "Qty" Then
                        kolom = 4
                    Else
                        kolom = 5
                    End If
                End If


            End If
        End If
        If kolom = 5 Then
            If Add.Text = "Simpan" Then
                'dgv1.Rows.Add()
                Call caristatusheader()
                If statusheader = "T" Then
                    Call simpan_datagridview()
                    Call penjumlahan()
                    'Call simpandata()

                    Call updateTOTAL()
                Else
                    Call simpandata()
                    Call simpan_datagridview()
                    Call penjumlahan()
                    Call updateTOTAL()

                End If

            Else
                If Edit.Text = "Simpan" Then
                    Call simpan_datagridview()
                    Call penjumlahan()
                Else
                    If Delete.Text = "Ok" Then
                        Call simpan_datagridview()
                        Call penjumlahan()
                    End If
                End If
            End If
        End If
    End Sub
  
    Private Sub DG1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellEnter

        If Len(Trim(DG1.Rows.Item(0).Cells(0).Value)) > 0 Then
            'untuk menunjukan kolom yang mana
            If kolom = 1 Then

                ' untuk mengambil nilai dari cell datagridview
                namabrg = DG1.Rows.Item(baris).Cells(0).Value

                'memanggil method untuk menampilkan data barang
                Call tampil_data_barang()

                'menentukan posisi listbox
                Call posisiList()
                'DG1.Rows.Item(baris).Cells(4).Value = Val(DG1.Rows.Item(baris).Cells(3).Value) * Val(DG1.Rows.Item(baris).Cells(2).Value)

            End If

            If kolom = 3 Then
                'DG1.Rows.Item(baris).Cells(4).Value = Val(DG1.Rows.Item(baris).Cells(3).Value) * Val(DG1.Rows.Item(baris).Cells(2).Value)

            End If

            If kolom = 4 Then

            End If


            'isi total harga
            DG1.Rows.Item(baris).Cells(4).Value = Val(DG1.Rows.Item(baris).Cells(3).Value) * Val(DG1.Rows.Item(baris).Cells(2).Value)

        End If




    End Sub

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        ListBox1.Visible = False
        ListBox1.Top = 12
        DG1.Rows.Item(baris).Cells(0).Value = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
        kodebrg = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
        Call caridatabarang()


    End Sub
    Private Sub penjumlahan()
        Call koneksi()
        Tampil5.Connection = Dtbase
        Tampil5.CommandType = CommandType.Text
        Tampil5.CommandText = "SELECT SUM(totalharga) AS subtotal FROM SODetail WHERE nosoH ='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil5.ExecuteReader
        If Tampilkan.HasRows = True Then
            Do While Tampilkan.Read()
                If IsDBNull(Tampilkan("subtotal")) Then
                Else
                    TextBox6.Text = Format(Tampilkan("subtotal"), "###,###,###")
                    TextBox5.Text = Tampilkan("subtotal")
                End If
            Loop
        End If



    End Sub
    Private Sub updateTOTAL()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "update SalesOrder set total= " & Val(TextBox5.Text) & " where NoSO='" & Trim(TextBox1.Text) & "'"
        Tampil.ExecuteNonQuery()
    End Sub


    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            Call caridata()
            Call tampilkan_detail_SO()

        End If
    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Len(TextBox1.Text) > 0 Then
            Call tampilkan_detail_SO()
            Call penjumlahan()
        End If

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If Len(TextBox2.Text) > 0 Then
            Call caricustomer()
            Call carinamacustomer()
        End If


    End Sub

    Private Sub ListBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.Click
        ListBox2.Visible = False
        ListBox2.Top = 12
        TextBox2.Text = Microsoft.VisualBasic.Left(ListBox2.Text, 4)

    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged

    End Sub

    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
        If Add.Text = "Add" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Add.Text = "Simpan"
            Edit.Enabled = False
            Delete.Enabled = False
            Keluar.Text = "Batal"

        Else
            'Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()
            Call normal()
            Call bersih_datagrid()

        End If

    End Sub

    Private Sub Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click
        If Edit.Text = "Edit" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Edit.Text = "Simpan"
            Add.Enabled = False
            Delete.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()
            Call normal()
            Call bersih_datagrid()

        End If

    End Sub

    Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
        If Delete.Text = "Delete" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Delete.Text = "Ok"
            Add.Enabled = False
            Edit.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()

            Call normal()
            Call bersih_datagrid()

        End If

    End Sub

    Private Sub Keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keluar.Click
        If Keluar.Text = "Batal" Then
            Call normal()
            Call bersih()
            Call bersih_datagrid()
            TextBox1.Focus()
        Else
            Me.Close() 'untuk menutup form diri sendiri'
        End If

    End Sub
    Private Sub caridata()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                If IsDBNull(Tampilkan("NoSO")) Then '
                    TextBox1.Focus()
                Else
                    DateTimePicker1.Value = Tampilkan("tglSO")
                    ComboBox1.Text = Tampilkan("carabayar")
                    ' TextBox5.Text = Tampilkan("total")
                    TextBox4.Text = Tampilkan("nopo")
                    TextBox2.Text = Tampilkan("kdcust")
                   

                    TextBox2.Focus()

                End If
            End While
        End If
    End Sub




    'Private Sub DG1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellEnter
    '    If Len(Trim(DG1.Rows.Item(0).Cells(0).Value)) > 0 Then
    '        'len untuk menentukan jamlah digit
    '        'untuk menunjukan kolom yang mana
    '        If DG1.Columns(e.ColumnIndex).Name = "Kode Barang" Then
    '            'untuk mengambil nilai dari cell DG1
    '            namabrg = DG1.Rows.Item(baris).Cells(0).Value
    '            Call tampil_data_barang()
    '            Call posisilist()

    '        End If
    '        'isi total harga
    '        DG1.Rows.Item(baris).Cells(4).Value = Val(DG1.Rows.Item(baris).Cells(3).Value) * Val(DG1.Rows.Item(baris).Cells(2).Value)


    '    End If


    'End Sub

    'Private Sub posisilist()
    '    ListBox1.Top = GroupBox1.Location.Y + DG1.Top + DG1.Location.Y + (DG1.Rows(0).Height * (baris + 1)) + 2
    '    ListBox1.Left = GroupBox1.Location.X + DG1.Location.X + 41
    'End Sub
    'Private Sub caridatabarang()
    '    Call koneksi()
    '    Tampil.Connection = Dtbase
    '    Tampil.CommandType = CommandType.Text
    '    Tampil.CommandText = "select * from Barang where kdbrg='" & Trim(kodebrg) & "'"
    '    Tampilkan = Tampil.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

    '    If Tampilkan.HasRows = True Then
    '        While Tampilkan.Read
    '            If IsDBNull(Tampilkan("kdbrg")) Then
    '                TextBox1.focus()
    '            Else
    '                'memeberikan nilai di cell DG1
    '                DG1.Rows.Item(baris).Cells(1).Value = Trim(Tampilkan("nmbrg"))
    '                DG1.Rows.Item(baris).Cells(3).Value = Tampilkan("hrgsatuan")


    '            End If
    '        End While

    '    End If
    'End Sub

    'Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
    '    ListBox1.Visible = False
    '    ListBox1.Top = 12
    '    DG1.Rows.Item(baris).Cells(0).Value = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
    '    kodebrg = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
    '    Call caridatabarang()

    'End Sub

    'Private Sub DG1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DG1.Click
    '    'untuk menunjukkan baris yang ke berapa
    '    baris = DG1.CurrentRow.Index

    'End Sub

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

    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged

    End Sub

    Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles print.Click
        noprint = TextBox1.Text
        lapSO.Show()


    End Sub
End Class


Kamis, 09 Oktober 2014

Sales Order Transaction

Public Class SO
    Dim baris, kolom, Hargasatuan As Integer
    Dim kodebrg, namabrg, statusheader As String


    Private Sub SO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call koneksi()
        Call isicombo()
        Call settingdatagrid()
        Call tampilkan_detail_SO()
    End Sub

    Private Sub simpandata()
        If Trim(TextBox1.Text) = "" Then Exit Sub
        If Trim(TextBox2.Text) = "" Then Exit Sub
        If Trim(ComboBox1.Text) = "" Then Exit Sub
        If Trim(TextBox4.Text) = "" Then Exit Sub
        'If Trim(TextBox5.Text) = "" Then Exit Sub


        If Add.Text = "Simpan" Then
            Tampil.Connection = Dtbase
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "insert into SalesOrder(NoSO,tglSO,kdcust,carabayar,total,nopo,tglEntry, useridd) values ('" & Trim(TextBox1.Text) & "', '" & DateTimePicker1.Value & "','" & TextBox2.Text & "','" & Microsoft.VisualBasic.Left(ComboBox1.Text, 2) & "'," & Val(TextBox5.Text) & ",'" & TextBox4.Text & "','" & tglskg & "', '" & nmorang & "' )"
            Tampil.ExecuteNonQuery()
        Else
            If Edit.Text = "Simpan" Then
                Tampil.Connection = Dtbase
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update SalesOrder set tglSO='" & DateTimePicker1.Value & "',kdcust='" & TextBox2.Text & "',carabayar='" & Microsoft.VisualBasic.Left(ComboBox1.Text, 2) & "',total=" & Val(TextBox5.Text) & ",nopo='" & Trim(TextBox4.Text) & "',tglEntry='" & tglskg & "', useridd='" & nmorang & "' where NoSO='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()

            Else
                If Delete.Text = "Ok" Then
                    'Untuk menghapus detail SO
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SODetail where nosoH='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()
                    'Untuk menghapus header SO
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()
                    Call settingdatagrid()


                Else

                End If
            End If

        End If

    End Sub
    Private Sub caristatusheader()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil2.ExecuteReader

        If Tampilkan.HasRows = True Then
            statusheader = "T"
        Else
            statusheader = "F"

        End If
    End Sub

    Private Sub bersih()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        ComboBox1.Text = ""
    End Sub
    Private Sub normal()
        Add.Text = "Add"
        Edit.Text = "Edit"
        Delete.Text = "Delete"
        Keluar.Text = "Exit"
        Add.Enabled = True
        Edit.Enabled = True
        Delete.Enabled = True
        Keluar.Text = "Exit"
    End Sub

    Private Sub isicombo()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Carabayar "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            ComboBox1.Items.Clear()
            While Tampilkan.Read() 'membaca data 1 per 1
                ComboBox1.Items.Add(Trim(Tampilkan("kdcarabyr")) & " " & Trim(Tampilkan("nmcarabyr")))
            End While
        End If
    End Sub
    Private Sub posisiList2()
        ListBox2.Top = TextBox2.Top + TextBox2.Height
        ListBox2.Left = TextBox2.Left
    End Sub

    Private Sub caricustomer()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Customer where nmcust like '%" & Trim(TextBox2.Text) & "%' "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            ListBox2.Items.Clear()
            ListBox2.Visible = True
            Call posisiList2()
            While Tampilkan.Read() 'membaca data 1 per 1
                ListBox2.Items.Add(Trim(Tampilkan("kdcust")) & " " & Trim(Tampilkan("nmcust")))
            End While
        End If

    End Sub
    Private Sub carinamacustomer()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Customer where kdcust ='" & Trim(TextBox2.Text) & "' "
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                TextBox3.Text = Trim(Tampilkan("nmcust"))
            End While
        End If

    End Sub
    Private Sub settingdatagrid()
        'untuk menentukan jumlah kolom
        DG1.ColumnCount = 5
        DG1.RowCount = 10

        'untuk menentukan nama header kolom
        DG1.Columns(0).Name = "Kode Barang"
        DG1.Columns(1).Name = "Nama Barang"
        DG1.Columns(2).Name = "Qty"
        DG1.Columns(3).Name = "Satuan Harga"
        DG1.Columns(4).Name = "Total Harga"

        'menentukan lebar kolom
        DG1.Columns(0).Width = 120
        DG1.Columns(1).Width = 250
        DG1.Columns(2).Width = 100
        DG1.Columns(3).Width = 100
        DG1.Columns(4).Width = 100

        Call AddButtonColumn()

        'format kolom sesuai dengan keingninan
        DG1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
        DG1.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
    End Sub
    Private Sub masukandata()
        'untuk memberishkan satu baris datagridview
        DG1.Rows.Item(baris).Cells(0).Value = " "
        DG1.Rows.Item(baris).Cells(1).Value = " "
        DG1.Rows.Item(baris).Cells(2).Value = " "
        DG1.Rows.Item(baris).Cells(3).Value = " "
        DG1.Rows.Item(baris).Cells(4).Value = " "
        DG1.Rows.Item(baris).Cells(5).Value = " "
    End Sub
    Private Sub AddButtonColumn()
        'untuk membuat tombol button di datagridview
        Dim btn = New DataGridViewButtonColumn()
        DG1.Columns.Add(btn)
        btn.HeaderText = "Simpan"
        btn.Text = "Simpan"
        btn.Name = "btnsimpan"
        btn.UseColumnTextForButtonValue = True
    End Sub
    Private Sub bersih_datagrid()
        DG1.RowCount = 1
        DG1.Refresh()
        DG1.RowCount = 10
    End Sub
    Private Sub posisiList()
        ListBox1.Top = GroupBox1.Location.Y + DG1.Top + DG1.Location.Y + (DG1.Rows(0).Height * (baris + 1)) + 2
        ListBox1.Left = GroupBox1.Location.X + DG1.Location.X + 41
    End Sub
    Private Sub caridatabarang()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from Barang where kdbrg='" & Trim(kodebrg) & "'"
        Tampilkan = Tampil.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                If IsDBNull(Tampilkan("kdbrg")) Then '

                Else
                    'memberikan nilai di cel datagridview
                    DG1.Rows.Item(baris).Cells(1).Value = Trim(Tampilkan("nmbrg"))
                    DG1.Rows.Item(baris).Cells(3).Value = Tampilkan("hrgsatuan")
                End If
            End While
        End If
    End Sub

    Private Sub tampil_data_barang()
        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from Barang where nmbrg like '%" & Trim(namabrg) & "%'"
        Tampilkan = Tampil2.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data
        If Tampilkan.HasRows = True Then 'klw ada record(data) maka akan bertipe true
            ListBox1.Items.Clear()
            ListBox1.Visible = True
            While Tampilkan.Read() 'membaca data satu persatu
                ListBox1.Items.Add(Trim(Tampilkan("kdbrg")) & "" & Trim(Tampilkan("nmbrg")))
            End While
        End If
    End Sub

    Private Sub simpan_datagridview()
        'cek sudah terisi semua belum
        If DG1.Rows.Item(baris).Cells(0).Value = "" Then Exit Sub
        If DG1.Rows.Item(baris).Cells(1).Value = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(2).Value) = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(3).Value) = "" Then Exit Sub
        If Trim(DG1.Rows.Item(baris).Cells(4).Value) = "" Then Exit Sub

        If Add.Text = "Simpan" Then
            Tampil.Connection = Dtbase
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "insert into SODetail(nosoH,kdbrg,qty,hargasatuan,totalharga) values ('" & Trim(TextBox1.Text) & "', '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'," & Val(DG1.Rows.Item(baris).Cells(2).Value) & "," & Val(DG1.Rows.Item(baris).Cells(3).Value) & "," & Val(DG1.Rows.Item(baris).Cells(4).Value) & ")"
            Tampil.ExecuteNonQuery()
            ' Call tampilkan_detail_SO()
        Else
            If Edit.Text = "Simpan" Then
                Tampil.Connection = Dtbase
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update SODetail set qty = " & Val(DG1.Rows.Item(baris).Cells(2).Value) & ",hargasatuan=" & Val(DG1.Rows.Item(baris).Cells(3).Value) & ",totalharga =" & Val(DG1.Rows.Item(baris).Cells(4).Value) & " where nosoH='" & Trim(TextBox1.Text) & "' and kdbrg= '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'"
                Tampil.ExecuteNonQuery()
                'Call tampilkan_detail_SO()
            Else
                If Delete.Text = "Ok" Then
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Delete from SODetail where nosoH='" & Trim(TextBox1.Text) & "' and kdbrg= '" & Trim(DG1.Rows.Item(baris).Cells(0).Value) & "'"
                    Tampil.ExecuteNonQuery()
                    Call bersihbaris()
                End If
            End If
        End If
    End Sub
    Private Sub bersihbaris()
        DG1.Rows.Item(baris).Cells(0).Value = ""
        DG1.Rows.Item(baris).Cells(1).Value = ""
        DG1.Rows.Item(baris).Cells(2).Value = ""
        DG1.Rows.Item(baris).Cells(3).Value = ""
        DG1.Rows.Item(baris).Cells(4).Value = ""
    End Sub
    Private Sub tampilkan_detail_SO()
        Call koneksi()
        Dim n As Integer
        DG1.RowCount = 1
        DG1.RowCount = 10

        Tabel = "SELECT SODetail.kdbrg, Barang.nmbrg, SODetail.qty, SODetail.hargasatuan, SODetail.totalharga"
        Tabel = Tabel + "  FROM SODetail INNER JOIN Barang ON SODetail.kdbrg = Barang.kdbrg WHERE  SODetail.nosoH= '" & Trim(TextBox1.Text) & "'"
        Tampil4.Connection = Dtbase
        Tampil4.CommandType = CommandType.Text
        'MsgBox(Tabel)
        Tampil4.CommandText = Tabel
        Tampilkan = Tampil4.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            n = 0
            While Tampilkan.Read() 'membaca data 1 per 1

                DG1.Rows.Item(n).Cells(0).Value = Tampilkan("kdbrg")
                DG1.Rows.Item(n).Cells(1).Value = Trim(Tampilkan("nmbrg"))
                DG1.Rows.Item(n).Cells(2).Value = Tampilkan("qty")
                DG1.Rows.Item(n).Cells(3).Value = Tampilkan("hargasatuan")
                DG1.Rows.Item(n).Cells(4).Value = Tampilkan("totalharga")

                n = n + 1

            End While
            'dgv1.RowCount = n + 1
            'Dim nn As Integer = dgv1.Rows.Add()
        End If
        'Dim nn As Integer = dgv1.Rows.Add()

        Tampilkan.Close()
        Tampil4.ExecuteReader.Close()
    End Sub


    Private Sub DG1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellClick
        ' Untuk menunjukan baris yang ke berapa
        baris = DG1.CurrentRow.Index

        'untuk menentukan kolom yang mana
        If DG1.Columns(e.ColumnIndex).Name = "Kode Barang" Then
            kolom = 1
        Else
            If DG1.Columns(e.ColumnIndex).Name = "Nama Barang" Then
                kolom = 2
            Else
                If DG1.Columns(e.ColumnIndex).Name = "Harga" Then
                    kolom = 3
                Else
                    If DG1.Columns(e.ColumnIndex).Name = "Qty" Then
                        kolom = 4
                    Else
                        kolom = 5
                    End If
                End If


            End If
        End If
        If kolom = 5 Then
            If Add.Text = "Simpan" Then
                'dgv1.Rows.Add()
                Call caristatusheader()
                If statusheader = "T" Then
                    Call simpan_datagridview()
                    Call penjumlahan()

                Else
                    Call simpandata()
                    Call simpan_datagridview()
                    Call penjumlahan()
                End If

            Else
                If Edit.Text = "Simpan" Then
                    Call simpan_datagridview()
                    Call penjumlahan()
                Else
                    If Delete.Text = "Ok" Then
                        Call simpan_datagridview()
                        Call penjumlahan()
                    End If
                End If
            End If
        End If
    End Sub
  
    Private Sub DG1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellEnter

        If Len(Trim(DG1.Rows.Item(0).Cells(0).Value)) > 0 Then
            'untuk menunjukan kolom yang mana
            If kolom = 1 Then

                ' untuk mengambil nilai dari cell datagridview
                namabrg = DG1.Rows.Item(baris).Cells(0).Value

                'memanggil method untuk menampilkan data barang
                Call tampil_data_barang()

                'menentukan posisi listbox
                Call posisiList()

            End If

            If kolom = 3 Then

            End If

            If kolom = 4 Then

            End If

            'isi total harga
            DG1.Rows.Item(baris).Cells(4).Value = Val(DG1.Rows.Item(baris).Cells(3).Value) * Val(DG1.Rows.Item(baris).Cells(2).Value)

        End If


    End Sub

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        ListBox1.Visible = False
        ListBox1.Top = 12
        DG1.Rows.Item(baris).Cells(0).Value = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
        kodebrg = Microsoft.VisualBasic.Left(ListBox1.Text, 3)
        Call caridatabarang()


    End Sub
    Private Sub penjumlahan()
        Call koneksi()
        Tampil5.Connection = Dtbase
        Tampil5.CommandType = CommandType.Text
        Tampil5.CommandText = "SELECT SUM(totalharga) AS subtotal FROM SODetail WHERE nosoH = '" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil5.ExecuteReader
        If Tampilkan.HasRows = True Then
            Do While Tampilkan.Read()
                If IsDBNull(Tampilkan("subtotal")) Then
                Else
                    TextBox6.Text = Format(Tampilkan("subtotal"), "###,###,###")
                    TextBox5.Text = Tampilkan("subtotal")
                End If
            Loop
        End If



    End Sub
    Private Sub UpdatePenjumlahan()

    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(13) Then
            caridata()
            'caricustomer()


            If e.KeyChar = Chr(13) Then
                TextBox2.Focus()

            End If
        End If
    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Len(TextBox1.Text) > 0 Then
            Call tampilkan_detail_SO()
            Call penjumlahan()
        End If

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If Len(TextBox2.Text) > 0 Then
            Call caricustomer()
            Call carinamacustomer()
        End If


    End Sub

    Private Sub ListBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.Click
        ListBox2.Visible = False
        ListBox2.Top = 12
        TextBox2.Text = Microsoft.VisualBasic.Left(ListBox2.Text, 4)

    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged

    End Sub

    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
        If Add.Text = "Add" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Add.Text = "Simpan"
            Edit.Enabled = False
            Delete.Enabled = False
            Keluar.Text = "Batal"

        Else
            'Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()
            Call normal()

        End If

    End Sub

    Private Sub Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click
        If Edit.Text = "Edit" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Edit.Text = "Simpan"
            Add.Enabled = False
            Delete.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()
            Call normal()

        End If

    End Sub

    Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
        If Delete.Text = "Delete" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Delete.Text = "Ok"
            Add.Enabled = False
            Edit.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()

            Call normal()

        End If

    End Sub

    Private Sub Keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keluar.Click
        If Keluar.Text = "Batal" Then
            Call normal()
            Call bersih()
            Call bersih_datagrid()
            TextBox1.Focus()
        Else
            Me.Close() 'untuk menutup form diri sendiri'
        End If

    End Sub
    Private Sub caridata()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from SalesOrder where NoSO='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil.ExecuteReader       'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                If IsDBNull(Tampilkan("NoSO")) Then '
                    TextBox1.Focus()
                Else
                    DateTimePicker1.Value = Tampilkan("tglSO")
                    ComboBox1.Text = Tampilkan("carabayar")
                    ' TextBox5.Text = Tampilkan("total")
                    TextBox4.Text = Tampilkan("nopo")
                    TextBox2.Text = Tampilkan("kdcust")
                   

                    TextBox2.Focus()

                End If
            End While
        End If
    End Sub
End Class

Minggu, 10 November 2013

Source Code Transaksi

Public Class PO
    Dim baris, kolom As Integer
    Dim data1, data2, data3, data4, data5 As String

    Private Sub PO_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated


    End Sub
    Private Sub PO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call koneksi()
        Call data_pemasok()
        Call data_karyawan()
        Call isiDG1()



    End Sub
    Private Sub data_pemasok()
        ComboBox1.Items.Clear()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from Pemasok"
        Tampilkan = Tampil.ExecuteReader

        If Tampilkan.HasRows = True Then
            While Tampilkan.Read
                ComboBox1.Items.Add("" & Tampilkan("kd_pemasok") & "")
            End While
        End If
    End Sub
    Private Sub data_karyawan()
        ComboBox2.Items.Clear()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from Karyawan"
        Tampilkan = Tampil.ExecuteReader

        If Tampilkan.HasRows = True Then
            While Tampilkan.Read
                ComboBox2.Items.Add("" & Tampilkan("kd_karyawan") & "")
            End While
        End If
    End Sub

    Private Sub simpandata()
        If Tambah.Text = "Simpan" Then
            If TextBox1.Text = "" Then Exit Sub
            If ComboBox1.Text = "" Then Exit Sub
            If TextBox2.Text = "" Then Exit Sub
            If ComboBox2.Text = "" Then Exit Sub
            If TextBox3.Text = "" Then Exit Sub


            Try
                Call koneksi()
                Tampil2.Connection = Dtbase 'tampil2=variabel public class untuk memberikan perintah
                Tampil2.CommandType = CommandType.Text 'tipe commant berbentuk text

                Tabel = "Insert Into PO(no_po,tgl,kd_pemasok,total,kd_karyawan,ket)"
                Tabel = Tabel + "values ('" & Trim(TextBox1.Text) & "',"
                Tabel = Tabel + "'" & Trim(DateTimePicker1.Text) & "',"
                Tabel = Tabel + " '" & Trim(ComboBox1.Text) & "',"
                Tabel = Tabel + " '" & Trim(TextBox2.Text) & "',"
                Tabel = Tabel + " '" & Trim(ComboBox2.Text) & "',"
                Tabel = Tabel + " '" & Trim(TextBox3.Text) & "')"



                Tampil2.CommandText = Tabel
                Tampil2.ExecuteNonQuery() 'perintah untuk mengeksekusi


                Call bersih()
                TextBox1.Focus()

            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try

        Else

            If Ubah.Text = "Simpan" Then
                If TextBox1.Text = "" Then Exit Sub
                If DateTimePicker1.Value = "" Then Exit Sub
                If ComboBox1.Text = "" Then Exit Sub
                If TextBox2.Text = "" Then Exit Sub
                If ComboBox2.Text = "" Then Exit Sub
                If TextBox3.Text = "" Then Exit Sub


                Try
                    Call koneksi()

                    Tampil2.Connection = Dtbase
                    Tampil2.CommandType = CommandType.Text
                    Tabel = "update PO set tgl='" & Trim(DateTimePicker1.Value) & "',kd_pemasok='" & Trim(ComboBox1.Text) & "',total='" & (TextBox2.Text) & "',kd_karyawan='" & Trim(ComboBox2.Text) & "',ket='" & Trim(TextBox3.Text) & "'"

                    Tabel = Tabel + " where no_po='" & Trim(TextBox1.Text) & "'"

                    Tampil2.CommandText = Tabel
                    Tampil2.ExecuteNonQuery()


                    Call bersih()
                    TextBox1.Focus()

                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try
            Else
                If Hapus.Text = "Ok" Then
                    If TextBox1.Text = "" Then Exit Sub
                    If DateTimePicker1.Value = "" Then Exit Sub
                    If ComboBox1.Text = "" Then Exit Sub
                    If TextBox2.Text = "" Then Exit Sub
                    If ComboBox2.Text = "" Then Exit Sub
                    If TextBox3.Text = "" Then Exit Sub


                    Try
                        Call koneksi()
                        Tampil2.Connection = Dtbase
                        Tampil2.CommandType = CommandType.Text
                        Tabel = "Delete from PO where no_po='" & Trim(TextBox1.Text) & "'"
                        Tampil2.CommandText = Tabel
                        Tampil2.ExecuteNonQuery()


                        Call bersih()
                        TextBox1.Focus()

                    Catch ex As Exception
                        MsgBox(ex.ToString())


                    End Try
                End If
            End If
        End If


    End Sub

    Private Sub normal() 'untuk menormalkan kondisi button'
        Tambah.Text = "Tambah"
        Ubah.Text = "Ubah"
        Hapus.Text = "Hapus"
        Keluar.Text = "Keluar"
        Tambah.Enabled = True
        Ubah.Enabled = True
        Hapus.Enabled = True
        Keluar.Enabled = True
    End Sub
    Private Sub bersih() 'untuk membersihkan objek entry data'
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""

    End Sub
    Private Sub caridata()
        Call koneksi()
        Tampil.Connection = Dtbase
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from PO where no_po='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil.ExecuteReader 'tampilkan=menyimpan hasil pembacaan data

        If Tampilkan.HasRows = True Then 'klw ada record(data)maka akan bertipe true
            While Tampilkan.Read() 'membaca data 1 per 1
                If IsDBNull(Tampilkan("no_po")) Then '
                    TextBox1.Focus()
                Else
                    DateTimePicker1.Value = Tampilkan("tgl")
                    ComboBox1.Text = Tampilkan("kd_pemasok")
                    TextBox2.Text = Tampilkan("total")
                    ComboBox2.Text = Tampilkan("kd_karyawan")
                    TextBox3.Text = Tampilkan("ket")

                    TextBox1.Focus()

                End If
            End While
        End If
    End Sub
    Private Sub Tambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tambah.Click
        If Tambah.Text = "Tambah" Then 'jk button1 d klik,mka button 1 berubah jd simpan & button4 jd batal
            TextBox1.Focus()
            Tambah.Text = "Simpan"
            Ubah.Enabled = False
            Hapus.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()   'jk tdak maka call simpan data, call bersih,call normal
            Call bersih()
            Call normal()

        End If
    End Sub

    Private Sub Ubah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ubah.Click
        If Ubah.Text = "Ubah" Then
            TextBox1.Focus() 'akan kembali pada textbox6
            Ubah.Text = "Simpan"
            Tambah.Enabled = False
            Hapus.Enabled = False
            Keluar.Text = "Batal"
        Else
            Call simpandata()   'jk tdk maka call simpan data dan call bersih
            Call bersih()

        End If
    End Sub

    Private Sub Hapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hapus.Click
        If Hapus.Text = "Hapus" Then
            TextBox1.Focus() 'jk button3 d klik maka akn mulcul 'simpan
            Hapus.Text = "Ok"
            Tambah.Enabled = False
            Ubah.Enabled = False
            Keluar.Text = "Batal"

        Else
            Call simpandata()
            Call bersih()

        End If
    End Sub

    Private Sub Keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keluar.Click
        If Keluar.Text = "Batal" Then
            Call normal()
            TextBox2.Focus()
        Else
            Me.Close() 'untuk menutup form diri sendiri'
        End If
    End Sub


    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(13) Then
            Call caridata()

        End If
    End Sub

    Private Sub DG1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DG1.CellBeginEdit
        baris = DG1.CurrentRow.Index

        If DG1.Columns(e.ColumnIndex).Name = "Kode Barang" Then
            kolom = 1
        Else
            If DG1.Columns(e.ColumnIndex).Name = "Nama Barang" Then
                kolom = 2
            Else
                If DG1.Columns(e.ColumnIndex).Name = "Harga" Then
                    kolom = 3
                Else
                    If DG1.Columns(e.ColumnIndex).Name = "Qty" Then
                        kolom = 4
                    Else
                        kolom = 5

                    End If
                End If


            End If
        End If
    End Sub
    Private Sub DG1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellClick
      


    End Sub
    Private Sub simpan()
        Call koneksi()

        If kolom = 1 Then
            Tampil.Connection = Dtbase
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "insert into detail_PO (no_po,kdbrg) values ('" & Trim(TextBox1.Text) & "', '" & Trim(data1) & "')"
            Tampil.ExecuteNonQuery()
        Else
          
            If kolom = 3 Then
                Tampil.Connection = Dtbase
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update detail_PO set harga='" & Trim(data3) & "'where kdbrg='" & Trim(data1) & "' and no_PO='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()

                If kolom = 4 Then
                    Tampil.Connection = Dtbase
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "update detail_PO set qty='" & Trim(data4) & "'where kdbrg='" & Trim(data1) & "' and no_PO='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()
                Else

                    If kolom = 5 Then
                        Tampil.Connection = Dtbase
                        Tampil.CommandType = CommandType.Text
                        Tampil.CommandText = "update detail_PO set sub_total='" & Trim(data5) & "'where kdbrg='" & Trim(data1) & "' and no_PO='" & Trim(TextBox1.Text) & "'"
                        Tampil.ExecuteNonQuery()
                    End If
                End If

            End If
        End If




    End Sub

    Private Sub DG1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellValueChanged
        'untuk mengambil nilai dari cell
        If IsDBNull(DG1.Item(0, baris).Value) Then
        Else
            data1 = DG1.Item(0, baris).Value

        End If
        If IsDBNull(DG1.Item(1, baris).Value) Then
        Else
            data2 = DG1.Item(1, baris).Value
        End If
        If IsDBNull(DG1.Item(2, baris).Value) Then
        Else
            data3 = DG1.Item(2, baris).Value
        End If
        If IsDBNull(DG1.Item(3, baris).Value) Then
        Else
            data4 = DG1.Item(3, baris).Value
        End If
        If IsDBNull(DG1.Item(4, baris).Value) Then
        Else
            data5 = DG1.Item(4, baris).Value

        End If

        'Call simpan()
        ' Call pencarian()
        ' Call tampil_nopo_D()
        'Call isi_detail()




    End Sub
    Private Sub isiDG1()
        DS = New DataSet
        Tabel = "SELECT detail_PO.kdbrg as [Kode Barang], Barang.nmbrg as [Nama Barang], detail_PO.harga as [Harga], detail_PO.qty as [Qty], detail_PO.sub_total as [Sub Total] FROM detail_PO INNER JOIN  Barang ON detail_PO.kdbrg = Barang.kdbrg WHERE  detail_PO.no_PO = '" & Trim(TextBox1.Text) & "'"
        ' Tabel = "select kdbrg as [Kode Barang],nmbrg as [Nama Barang],harga as [Harga],qty as [Qty],sub_total as [Sub Total] FROM detail_PO  "

        '"SELECT Pembayaran.no_ktw, Pembayaran.npm, Pembayaran.semester FROM Pembayaran"
        Grid = New OleDb.OleDbDataAdapter(Tabel, Dtbase)
        Grid.Fill(DS, "detail_PO,Barang")

        Dim Gridview As New DataView(DS.Tables("detail_PO,Barang"))
        DG1.DataSource = Gridview
        DG1.Columns(0).Width = 150 'untuk menentukan lebar kolom
        DG1.Columns(1).Width = 150
        DG1.Columns(2).Width = 100
        DG1.Columns(3).Width = 100
        DG1.Columns(4).Width = 100


    End Sub
    Private Sub CaridataBarang()

        Call koneksi()
        Tampil2.Connection = Dtbase
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select* from Barang where kdbrg ='" & Trim(data1) & "'"
        Tampilkan = Tampil2.ExecuteReader
        If Tampilkan.HasRows = True Then
            While Tampilkan.Read()
                If IsDBNull(Tampilkan("kdbrg")) Then
                Else
                    DG1.Item(1, baris).Value = Tampilkan("nmbrg")
                End If

            End While
        Else
            MsgBox("Data tidak ditemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah data")

        End If




    End Sub
  

    Private Sub DG1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DG1.Enter
      
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Call isiDG1()
    End Sub

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

    End Sub

    Private Sub DG1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellEnter
        If kolom = 1 Then
            Call simpan()
            Call CaridataBarang()
            kolom = 0
        End If
        If kolom = 3 Then
            Call simpan()
            kolom = 0
        End If
        If kolom = 4 Then
            Call simpan()
            kolom = 0
        End If
        If kolom = 5 Then
            Call simpan()
            kolom = 0
        End If
    End Sub
End Class