Kamis, 15 Desember 2011

APKOM 4 (Latihan 40a dan 40b)

LATIHAN 40A

Public Class Latihan40A_36109005
    Dim ISMI As New OleDb.OleDbConnection("PROVIDER = MICROSOFT.ACE.OLEDB.12.0; DATA SOURCE =" & Application.StartupPath & "/DataMajemuk.accdb")
    Dim DT1 As New DataTable
    Dim DT2 As New DataTable
    Dim DS As New DataSet
    Dim BS1 As New BindingSource
    Dim BS2 As New BindingSource
    Public Sub MIKKY1()
        Dim S As New OleDb.OleDbDataAdapter
        DT2.Rows.Clear()
        DT1.Rows.Clear()
        S = New OleDb.OleDbDataAdapter("select * from mastertransaksi", ISMI)
        S.Fill(DT1)
        S = New OleDb.OleDbDataAdapter("select detailtransaksi.notrans, barang.kodebarang, barang.namabarang, detailtransaksi.unit, detailtransaksi.harga, detailtransaksi.unit*harga as jumlah from barang inner join detailtransaksi on barang.kodebarang = detailtransaksi.kodebarang", ISMI)
        S.Fill(DT2)
    End Sub
    Private Sub Latihan40A_36109005_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MIKKY1()

        DS.Tables.Add(DT1)
        DS.Tables.Add(DT2)

        DT1.TableName = "immy"
        DT2.TableName = "immy2"

        DS.Relations.Add(New DataRelation("S", DS.Tables("immy").Columns("notrans"), DS.Tables("immy2").Columns("notrans")))

        BS1.DataSource = DS
        BS1.DataMember = "immy"

        BS2.DataSource = BS1
        BS2.DataMember = "S"

        Dim DC(1) As DataColumn
        DC(0) = DT1.Columns("notrans")
        DT1.PrimaryKey = dc

        DGV1_36109005.DataSource = BS1
        DGV2_36109005.DataSource = BS2
    End Sub
    Private Sub Hapus_36109005_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hapus_36109005.Click
        Dim COM As New OleDb.OleDbCommand
        COM = New OleDb.OleDbCommand("delete * from mastertransaksi where notrans = '" & DGV1_36109005.CurrentRow.Cells("notrans").Value & "'", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()

        COM = New OleDb.OleDbCommand("delete * from detailtransaksi where notrans = '" & DGV1_36109005.CurrentRow.Cells("notrans").Value & "'", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()
        COM.Dispose()
        MIKKY1()
    End Sub

    Private Sub Tambah_36109005_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tambah_36109005.Click
        If Latihan40B_36109005.Visible = False Then
            Latihan40B_36109005.Show()
        Else
            Latihan40B_36109005.Activate()
        End If

        Latihan40B_36109005.NT_36109005.Text = ""
        Latihan40B_36109005.JT_36109005.Text = ""
        Latihan40B_36109005.NO_36109005.Text = "-"

        Latihan40B_36109005.MIKKY2()
        Latihan40B_36109005.MIKKY3()

    End Sub
    Private Sub Edit_36109005_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit_36109005.Click
        If Latihan40B_36109005.Visible = False Then
            Latihan40B_36109005.Show()
        Else
            Latihan40B_36109005.Activate()
        End If

        Latihan40B_36109005.NT_36109005.Text = DGV1_36109005.CurrentRow.Cells("notrans").Value
        Latihan40B_36109005.T_36109005.Value = DGV1_36109005.CurrentRow.Cells("tanggaltransaksi").Value
        Latihan40B_36109005.JT_36109005.Text = DGV1_36109005.CurrentRow.Cells("jenistransaksi").Value
        Latihan40B_36109005.NO_36109005.Text = DGV1_36109005.CurrentRow.Cells("notrans").Value

        Latihan40B_36109005.MIKKY2()
        Latihan40B_36109005.MIKKY3()
    End Sub
End Class


LATIHAN 40B

Public Class Latihan40B_36109005
    Dim ISMI As New OleDb.OleDbConnection("PROVIDER = MICROSOFT.ACE.OLEDB.12.0; DATA SOURCE =" & Application.StartupPath & "/dataMajemuk.accdb")
    Dim DT As New DataTable
    Dim IMMY As New ByIskandar.CariKeDataBaseByIskandar
    Dim COM As New OleDb.OleDbCommand
    Public Sub MIKKY2()
        Dim S As New OleDb.OleDbDataAdapter
        S = New OleDb.OleDbDataAdapter("select barang.KODEBARANG, barang.NAMABARANG, detailtransaksi.UNIT, detailtransaksi.HARGA, detailtransaksi.unit * detailtransaksi.harga as JUMLAH from barang inner join detailtransaksi on barang.kodebarang = detailtransaksi.kodebarang where notrans = '" & NT_36109005.Text & "'", ISMI)
        DT.Rows.Clear()
        S.Fill(DT)
    End Sub
    Public Sub MIKKY3()
        Dim Manis1 As Integer
        For Each Manis2 As DataGridViewRow In DGV_36109005.Rows
            Manis1 = Manis1 + Manis2.Cells("JUMLAH").Value
        Next
        TO_36109005.Text = Manis1
    End Sub
    Private Sub MIKKY4()
        If NT_36109005.Text.Length = 0 Then
            MsgBox("No Transaksi Belum Di isi")
            Exit Sub
        End If

        If JT_36109005.Text.Length = 0 Then
            MsgBox("Jenis Transaksi Belum Di isi")
            Exit Sub
        End If

        If DT.Rows.Count = 0 Then
            MsgBox("Datatable Bulum Di isi")
            Exit Sub
        End If
    End Sub
    Private Sub Febriani()
        MIKKY4()
        IMMY.AturPencarianDataBase("mastertransaksi", "notrans", NT_36109005.Text, 1, ISMI)
        If IMMY.JumlanBaris > 0 Then
            MsgBox("No Transaksi Seperti Itu Sudah Ada")
            Exit Sub
        End If

        COM = New OleDb.OleDbCommand("insert into mastertransaksi (notrans, tanggaltransaksi, jenistransaksi) values ('" & NT_36109005.Text & "',#" & T_36109005.Value.Month & "/" & T_36109005.Value.Day & "/" & T_36109005.Value.Year & "#,'" & JT_36109005.Text & "')", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()

        For Each x As DataRow In DT.Rows
            COM = New OleDb.OleDbCommand("insert into detailtransaksi (notrans, kodebarang, unit, harga) values ('" & NT_36109005.Text & "', '" & x("kodebarang") & "', '" & x("unit") & "', '" & x("harga") & "') ", ISMI)
            ISMI.Open()
            COM.ExecuteNonQuery()
            ISMI.Close()
        Next
        COM.Dispose()

        NT_36109005.Text = ""
        JT_36109005.Text = ""
        TO_36109005.Text = ""
        NO_36109005.Text = "-"

        DT.Rows.Clear()
        Latihan40A_36109005.MIKKY1()
    End Sub
    Public Sub Edit()
        MIKKY4()
        If NT_36109005.Text <> NO_36109005.Text Then
            IMMY.AturPencarianDataBase("mastertransaksi", "notrans", NT_36109005.Text, 1, ISMI)
            If IMMY.JumlanBaris > 0 Then
                MsgBox("No Transaksi Seperti Itu Sudah Ada")
                Exit Sub
            End If
        End If

        COM = New OleDb.OleDbCommand("delete * from mastertransaksi where notrans = '" & NO_36109005.Text & "'", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()

        COM = New OleDb.OleDbCommand("insert into mastertransaksi(notrans, tanggaltransaksi, jenistransaksi) values ('" & NT_36109005.Text & "',#" & T_36109005.Value.Month & "/" & T_36109005.Value.Day & "/" & T_36109005.Value.Year & "#,'" & JT_36109005.Text & "')", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()

        COM = New OleDb.OleDbCommand("delete * from detailtransaksi where notrans = '" & NO_36109005.Text & "'", ISMI)
        ISMI.Open()
        COM.ExecuteNonQuery()
        ISMI.Close()

        For Each x As DataRow In DT.Rows
            COM = New OleDb.OleDbCommand("insert into detailtransaksi(notrans, kodebarang, unit, harga) values ('" & NT_36109005.Text & "', '" & x("kodebarang") & "', '" & x("unit") & "', '" & x("harga") & "')", ISMI)
            ISMI.Open()
            COM.ExecuteNonQuery()
            ISMI.Close()
        Next
        COM.Dispose()

        NT_36109005.Text = ""
        JT_36109005.Text = ""
        TO_36109005.Text = ""
        NO_36109005.Text = "-"

        DT.Rows.Clear()
        Latihan40A_36109005.MIKKY1()
    End Sub
    Private Sub Latihan40B_36109005_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MIKKY2()

        Dim DC(1) As DataColumn
        DC(0) = DT.Columns("kodebarang")
        DT.PrimaryKey = DC

        DT.Columns("unit").DefaultValue = 0
        DT.Columns("harga").DefaultValue = 0
        DT.Columns("jumlah").DefaultValue = 0

        DGV_36109005.DataSource = DT
    End Sub
    Private Sub DGV_36109005_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_36109005.CellEndEdit
        If DGV_36109005.Columns(e.ColumnIndex).Name = "KODEBARANG" Then

            DGV_36109005.CurrentRow.Cells("NAMABARANG").Value = ""
            DGV_36109005.CurrentRow.Cells("UNIT").Value = 0
            DGV_36109005.CurrentRow.Cells("HARGA").Value = 0
            DGV_36109005.CurrentRow.Cells("JUMLAH").Value = 0

            IMMY.AturPencarianDataBase("barang", "kodebarang", DGV_36109005.CurrentRow.Cells("KODEBARANG").Value, 1, ISMI)
            If IMMY.JumlanBaris > 0 Then
                DGV_36109005.CurrentRow.Cells("NAMABARANG").Value = IMMY.DataTablenya.Rows(0).Item("NAMABARANG")
            Else
                DGV_36109005.CurrentRow.Cells("NAMABARANG").Value = ""
                If Latihan38_39_40_36109005.ShowDialog = Windows.Forms.DialogResult.OK Then
                    DGV_36109005.CurrentRow.Cells("KODEBARANG").Value = Latihan38_39_40_36109005.DGV_36109005.CurrentRow.Cells("KODEBARANG").Value
                    DGV_36109005.CurrentRow.Cells("NAMABARANG").Value = Latihan38_39_40_36109005.DGV_36109005.CurrentRow.Cells("NAMABARANG").Value
                End If
            End If

        ElseIf DGV_36109005.Columns(e.ColumnIndex).Name = "UNIT" Or DGV_36109005.Columns(e.ColumnIndex).Name = "HARGA" Then
            DGV_36109005.CurrentRow.Cells("JUMLAH").Value = DGV_36109005.CurrentRow.Cells("UNIT").Value * DGV_36109005.CurrentRow.Cells("HARGA").Value
            MIKKY3()
        End If
    End Sub
    Private Sub Simpan_36109005_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Simpan_36109005.Click
        If NO_36109005.Text = "-" Then
            Febriani()
        Else
            Edit()
        End If
    End Sub
    Private Sub DGV_36109005_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DGV_36109005.DataError
        MsgBox("Terjadi sedikit kesalahan...")
    End Sub
End Class

Tidak ada komentar:

Posting Komentar