Senin, 28 November 2011

Latihan 39 APKOM 4

Public Class LATIHAN039012
'DEKLARASI CONECTION
Dim CONECT As New OleDb.OleDbConnection("provider=microsoft.ace.oledb.12.0;data source= " & Application.StartupPath & "\DATAMAJEMUK.accdb")
Dim DT As New DataTable
Dim cari As New ByIskandar.CariKeDataBaseByIskandar

Private Sub Latihan039012_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DT.Columns.Add(New DataColumn("KODEBARANG", GetType(String))) 'untuk angka
DT.Columns.Add(New DataColumn("NAMABARANG", GetType(String))) 'untuk angka
DT.Columns.Add(New DataColumn("UNIT", GetType(Double))) 'untuk angka
DT.Columns.Add(New DataColumn("HARGA", GetType(Integer))) 'untuk angka
DT.Columns.Add(New DataColumn("JUMLAH", GetType(Double)))
DataGridView1.DataSource = DT
'datagridwiew digunakan untuk menampilkan tabel yg sdh dibuat jika tdk maka tabel tdk akn muncul

'rumus dibwh ini untuk membuat primary key dimana dcp adalah nama sembarangji
Dim dcp(1) As DataColumn
dcp(0) = DT.Columns("KODEBARANG")
DT.PrimaryKey = dcp

DT.Columns("UNIT").DefaultValue = 0
DT.Columns("HARGA").DefaultValue = 0
DT.Columns("JUMLAH").DefaultValue = 0

End Sub
Private Sub hitung()
Dim manis As Integer
For Each r As DataGridViewRow In DataGridView1.Rows
manis = manis + r.Cells("JUMLAH").Value
Next
Tot.Text = manis

End Sub

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If e.ColumnIndex = 0 Then
'untuk mencari jgn sampai data yg akan dimasukkan sudah ada
cari.AturPencarianDataBase("BARANG", "kodebarang", DataGridView1.CurrentRow.Cells("kodebarang").Value, 1, CONECT) '1 untuk huruf
If cari.JumlanBaris > 0 Then
DataGridView1.CurrentRow.Cells("KODEBARANG").Value = cari.DataTablenya.Rows(0).Item("KODEBARANG")
DataGridView1.CurrentRow.Cells("NAMABARANG").Value = cari.DataTablenya.Rows(0).Item("NamaBarang")
Else
MsgBox("kode barang tidak tersedia")
If Latihan383940012.ShowDialog = Windows.Forms.DialogResult.OK Then
DataGridView1.CurrentRow.Cells("KODEBARANG").Value = Latihan383940012.DataGridView1.CurrentRow.Cells("KodeBarang").Value
DataGridView1.CurrentRow.Cells("NAMABARANG").Value = Latihan383940012.DataGridView1.CurrentRow.Cells("namabarang").Value
DataGridView1.CurrentRow.Cells("HARGA").Value = Latihan383940012.DataGridView1.CurrentRow.Cells("HARGAJUAL").Value
Else
DataGridView1.CurrentRow.Cells("KODEBARANG").Value = ""
DataGridView1.CurrentRow.Cells("NAMABARANG").Value = ""
End If
End If
End If
If e.ColumnIndex = 2 Or e.ColumnIndex = 3 Then
DataGridView1.CurrentRow.Cells("JUMLAH").Value = DataGridView1.CurrentRow.Cells("UNIT").Value * DataGridView1.CurrentRow.Cells("HARGA").Value

End If

hitung()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'untuk mencari jgn sampai data yg akan dimasukkan tidak lengkap
If TextBox1.Text.Length = 0 Then 'untuk pencarian yang bersifat angka
MsgBox("no transaksi belum terisi")
Exit Sub 'jgn lanjut ke proses berikutnya
End If


If jt.Text.Length = 0 Then 'untuk pencarian yang bersifat angka
MsgBox("jenis transaksi belum terisi")
Exit Sub 'jgn lanjut ke proses berikutnya
End If

If DT.Rows.Count = 0 Then
MsgBox("data tidak tersedia")
End If

'untuk mencari jgn sampai data yg akan dimasukkan sudah ada
cari.AturPencarianDataBase("mastertransaksi", "notrans", TextBox1.Text, 1, CONECT)

If cari.JumlanBaris > 0 Then
MsgBox("kode sudah ada")
Exit Sub
End If



'untuk memasukkan data dari visual basc ke dlm acces
Dim com As New OleDb.OleDbCommand
com = New OleDb.OleDbCommand("insert into mastertransaksi (notrans, tanggaltransaksi, jenistransaksi) values ('" & TextBox1.Text & "',#" & DateTimePicker1.Value.Date & "#," & Val(jt.Text) & ")", CONECT)

CONECT.Open()
com.ExecuteNonQuery()
CONECT.Close()

For Each r As DataRow In DT.Rows

com = New OleDb.OleDbCommand("insert into detailtransaksi (notrans, kodebarang, unit, harga ) values ('" & TextBox1.Text & "','" & r("kodebarang") & "'," & r("unit") & "," & r("harga") & ")", CONECT)
CONECT.Open()
com.ExecuteNonQuery()
CONECT.Close()

com.Dispose()

Next

TextBox1.Text = ""
jt.Text = ""
Tot.Text = ""

DT.Rows.Clear()
hitung()
End Sub
End Class

Tidak ada komentar: