Friday, 5 June 2015

Membuat Form Transaksi

Aplikasi Penjualan Buku Menggunakan Vb. Net dan Access

Step 3 : Membuat Form Transaksi

A.   Form Transaksi

Tambah kan sebuah form(Langkah-langkahnya seperti pada Step2 : Membuat Form Data Buku) Rename Form1.vb menjadi FormTransaksi.vb lalu rancang lah FormTransaksi.vb seperti gambar dibawah ini :


              Property Setting :
Object
Name
Text
Form 1
FormTransaksi
Transaksi
Label1
Label 1.Text
Nomor Faktur
Label2
Label 2.Text
Nama Pembeli
Label3
Label 3.Text
No. Telp
Label4
Label 4.Text
Tanggal
Label5
Label 5.Text
Pukul
Label6
Label 6.Text
Jumlah Item
Label7
Label 7.Text
Total
Label8
Label 8.Text
Bayar
Label9
Label 9.Text
Kembali
Label10
Label 10.Text
Rp.
TextBox 1
TextBox 1.Text
-
TextBox 2
TextBox 2.Text
-
TextBox 3
TextBox 3.Text
-
TextBox 4
TextBox 4.Text
-
TextBox 5
TextBox 5.Text
-
TextBox 6
TextBox 6.Text
-
TextBox 7
TextBox 7.Text
-
TextBox 8
TextBox 8.Text
-
TextBox 9
TextBox 9.Text
-
Button 1
Button 1
&Simpan
Button 2
Button 2
&Batal
Button 3
Button 3
&Tutup
Button 4
Button 4
&Kode Buku
DataGridView1
DataGridView1
-

Ketikan koding pada masing-masing object seperti kode berikut :

Imports System.Data.OleDb
Public Class FormTransaksi
    Sub Kosong()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
        TextBox8.Clear()
        TextBox4.Focus()
    End Sub
    Sub TidakAktif()
        TextBox1.Enabled = False
        TextBox2.Enabled = False
        TextBox3.Enabled = False
    End Sub
    Sub KolomBaru()
        DataGridView1.Columns.Add("Kode", "KODE BUKU")
        DataGridView1.Columns.Add("Nama", "NAMA BUKU")
        DataGridView1.Columns.Add("Harga", "HARGA")
        DataGridView1.Columns.Add("Jumlah", "QTY")
        DataGridView1.Columns.Add("Total", "SUB TOTAL")
        Call LebarKolom()
    End Sub
    Sub LebarKolom()
        DataGridView1.Columns(0).Width = 50
        DataGridView1.Columns(1).Width = 300
        DataGridView1.Columns(2).Width = 60
        DataGridView1.Columns(3).Width = 40
        DataGridView1.Columns(4).Width = 80
    End Sub
    Private Sub Otomatis()
        cmd = New OleDbCommand("Select * from Transaksi where NoFaktur in (select max(NoFaktur) from Transaksi) order by NoFaktur desc", Conn)
        Dim urutan As String
        Dim hitung As Long
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            urutan = "TR" + Format(Now, "yyMMdd") + "001"
        Else
            If Microsoft.VisualBasic.Mid(rd.GetString(0), 3, 6) <> Format(Now, "yyMMdd") Then
                urutan = "TR" + Format(Now, "yyMMdd") + "001"
            Else
                hitung = Microsoft.VisualBasic.Right(rd.GetString(0), 2) + 1
                urutan = "TR" + Format(Now, "yyMMdd") + Microsoft.VisualBasic.Right("000" & hitung, 3)
            End If
        End If
        TextBox1.Text = urutan
    End Sub
    Sub TampilBuku()
        cmd = New OleDbCommand("Select KodeBuku From Buku", Conn)
        rd = cmd.ExecuteReader
    End Sub

    Private Sub Form4_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Call Otomatis()
        TextBox2.Text = Today
        TextBox3.Text = TimeOfDay
    End Sub

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Koneksi()
        Call KolomBaru()
        Call Kosong()
        Call TidakAktif()
        Call TampilBuku()
    End Sub
    Sub TotalItem()
        Dim HitungItem As Integer = 0
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            HitungItem = HitungItem + Val(DataGridView1.Rows(I).Cells(3).Value)
            TextBox9.Text = HitungItem
        Next
    End Sub
    Sub TotalHarga()
        Dim HitungHarga As Integer = 0
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            HitungHarga = HitungHarga + Val(DataGridView1.Rows(I).Cells(4).Value)
            TextBox6.Text = HitungHarga
            Label10.Text = HitungHarga
        Next
    End Sub
    Sub HapusBaris()
        On Error Resume Next
        Dim Baris As Integer = DataGridView1.CurrentCell.RowIndex
        DataGridView1.Rows(baris).Cells(0).Value = ""
        Chr(30)
    End Sub

    Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Up Then
            DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(3)
        End If
    End Sub

    Private Sub DataGridView1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
        On Error Resume Next
        If e.KeyChar = Chr(27) Then
            DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
            Call TotalItem()
            Call TotalHarga()
            TextBox7.Clear()
            TextBox8.Text = ""
        End If
    End Sub

    Private Sub TextBox7_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        If e.KeyChar = Chr(13) Then
            If Val(TextBox7.Text) < Val(TextBox6.Text) Then
                MsgBox("Pembayaran kurang")
                TextBox8.Text = ""
                TextBox7.Focus()
                Exit Sub
            ElseIf Val(TextBox7.Text) = Val(TextBox6.Text) Then
                TextBox8.Text = 0
                Button2.Focus()
            Else
                TextBox8.Text = Val(TextBox7.Text) - Val(TextBox6.Text)
                Button2.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 Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call Kosong()
        DataGridView1.Columns.Clear()
        Call KolomBaru()
        DataGridView1.Focus()
    End Sub

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If e.Control.GetType.ToString() = "System.Windows.Forms.DataGridViewTextBoxEditingControl" Then
            Dim c As DataGridViewTextBoxEditingControl = CType(e.Control, DataGridViewTextBoxEditingControl)
            RemoveHandler c.KeyPress, AddressOf GridViewTextBox_KeyPress
            AddHandler c.KeyPress, AddressOf GridViewTextBox_KeyPress
        End If
    End Sub
    Private Sub GridViewTextBox_KeyPress(ByVal sender As Object, ByVal ex As KeyPressEventArgs)
        If DataGridView1.CurrentCell.ColumnIndex = 3 Then
            If ((Asc(ex.KeyChar) < 48 Or Asc(ex.KeyChar) > 57) And Asc(ex.KeyChar) <> 8) Then
                ex.Handled = True
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Then
            MsgBox("Data belum lengkap, tidak ada transaksi atau pembayaran masih kosong")
            Exit Sub
        End If
        'Simpan ke tabel Transaksi
        Dim SimpanTransaksi As String = "Insert into Transaksi(NoFaktur,TglFaktur,Pukul,NamaPembeli,NoTlp,Total,Dibayar,Kembali,Item) values " & _
        "('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "')"
        cmd = New OleDbCommand(SimpanTransaksi, Conn)
        cmd.ExecuteNonQuery()
        For baris As Integer = 0 To DataGridView1.Rows.Count - 2
            'Simpan ke tabel DetailTransaksi
            Dim SimpanDetailTransaksi As String = "Insert into DetailTransaksi (NoFaktur,KodeBuku,Judul,HargaJual,Jumlah,SubTotal) values " & _
            "('" & TextBox1.Text & "','" & DataGridView1.Rows(baris).Cells(0).Value & "','" & DataGridView1.Rows(baris).Cells(1).Value & "','" & DataGridView1.Rows(baris).Cells(2).Value & "','" & DataGridView1.Rows(baris).Cells(3).Value & "','" & DataGridView1.Rows(baris).Cells(4).Value & "')"
            cmd = New OleDbCommand(SimpanDetailTransaksi, Conn)
            cmd.ExecuteNonQuery()
            'Kurangi stok Buku
            cmd = New OleDbCommand("select * from Buku where KodeBuku='" & DataGridView1.Rows(baris).Cells(0).Value & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                Dim KurangiStok As String = "update Buku set Jumlah= '" & rd.Item(5) - DataGridView1.Rows(baris).Cells(3).Value & "' where KodeBuku='" & DataGridView1.Rows(baris).Cells(0).Value & "'"
                cmd = New OleDbCommand(KurangiStok, Conn)
                cmd.ExecuteNonQuery()
            End If
        Next baris
        DataGridView1.Columns.Clear()
        Call KolomBaru()
        Call Otomatis()
        Call Kosong()
    End Sub
    Sub kena(ByVal myGrid As DataGrid)
        myGrid.CurrentCell = New DataGridCell(1, 1)
    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
            cmd = New OleDbCommand("select * from Buku where KodeBuku='" & DataGridView1.Rows(e.RowIndex).Cells(0).Value & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                DataGridView1.Rows(e.RowIndex).Cells(1).Value = rd.Item(2)
                DataGridView1.Rows(e.RowIndex).Cells(2).Value = rd.Item(6)
                DataGridView1.Rows(e.RowIndex).Cells(3).Value = 1
                DataGridView1.Rows(e.RowIndex).Cells(4).Value = DataGridView1.Rows(e.RowIndex).Cells(2).Value * DataGridView1.Rows(e.RowIndex).Cells(3).Value
                Call TotalItem()
                Call TotalHarga()
            Else
                MsgBox("Kode buku tidak terdaftar")
            End If
        End If
        If e.ColumnIndex = 3 Then
            cmd = New OleDbCommand("select * from Buku where KodeBuku='" & DataGridView1.Rows(e.RowIndex).Cells(0).Value & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                If DataGridView1.Rows(e.RowIndex).Cells(3).Value > rd.Item(5) Then
                    MsgBox("Stok Buku hanya ada " & rd.Item(5) & "")
                    DataGridView1.Rows(e.RowIndex).Cells(3).Value = 1
                    DataGridView1.Rows(e.RowIndex).Cells(4).Value = DataGridView1.Rows(e.RowIndex).Cells(2).Value * DataGridView1.Rows(e.RowIndex).Cells(3).Value
                    Call TotalItem()
                    Call TotalHarga()
                Else
                    DataGridView1.Rows(e.RowIndex).Cells(4).Value = DataGridView1.Rows(e.RowIndex).Cells(2).Value * DataGridView1.Rows(e.RowIndex).Cells(3).Value
                    Call TotalItem()
                    Call TotalHarga()
                End If
            End If
            DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)
        End If
    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox4.Text = UCase(TextBox4.Text)
            TextBox5.Focus()
        End If
    End Sub

    Private Sub TextBox5_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox5.Text = UCase(TextBox5.Text)
            DataGridView1.Focus()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FormData.ShowDialog()
    End Sub
End Class

B.   Tabel Transaksi

Tambah kan tabel Transaksi pada database Buku.accdb yang sudah dibuat pada step 1.

Struktur Tabel Buku :

Field
Type
Size
Keterangan
NoFaktur
Text
10
PK (Primary Key)
TglFaktur
Date/Time
-
FK (Foreign Key)
Pukul
Date/Time
-

Nama_Pembeli
Text
50

NoTlp
Text
15

Total
Number
Long Int

Dibayar
Number
Long Int

Kembali
Number
Long Int

Item
Number
Long Int


 Tutorial bisa didownload Disini


0 komentar:

Post a Comment

Kunjungan Sobat

Popular Posts

Enter your email address:

Delivered by FeedBurner