Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
1
MODUL III MEMBUAT FORM MENYIMPAN PROSES PEMBELIAN
Langkah mengerjakan : 1.Buka Sql Server 2.Attact database anda, ditemmpat folder anda menyimpan pada pertemuan sebelumnya 3.Buat Tabel :H_beli
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
2
4.Buat Tabel :tabel_dbeli
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
3
4. Masuk ke visual studio 5. Buka project Sebelumnya 7.Tambahkan form :fhbeli a.Pilih menu project b. Add windows form c. beri nama form
Kemudian tekan ok D . Buat desain dan ubah propertises seperti dibawah : Oject Label1 Label 2 Label 3 Datetimepicker1 Combobox1 Textbox1 Textbox2 Button
Propertise Text Text Text Name name Name Name Name Text
Isikan Tanggal Pembelian Supplier No referensi xtanggal_beli ComboBox1 xnoref xnofaktur TAMBAH TAMBAH
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
4
E. Tambahkan coding e.1 form load dan tambahkan fungsi combotables Imports System.Data.Sql Imports System.Data.SqlClient Public Class ftrans_beli Private Sub ftrans_beli_Load(sender As Object, e As EventArgs) Handles MyBase.Load Call buka() ComboTables() End Sub
Sub ComboTables() koneksi.Close() buka() Try 'piye cmd = New SqlClient.SqlCommand("SELECT * FROM tabel_supplier", koneksi) 'cmd = New MySqlCommand("SELECT kode,nama FROM coba", koneksi) baca = cmd.ExecuteReader() While baca.Read() ComboBox1.Items.Add(baca("nama_supplier")) End While Catch ex As Exception MessageBox.Show("Koneksi Gagal !!!, karena " & ex.Message) End Try ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems koneksi.Close() End Sub
e.2 Pada tombol TAMBAH Private Sub TAMBAH_Click(sender As Object, e As EventArgs) Handles TAMBAH.Click 'buat nobukti baru Dim n As Integer n = 0 buka() Try 'piye cmd = New SqlClient.SqlCommand("SELECT * FROM h_beli", koneksi) baca = cmd.ExecuteReader() While baca.Read() 'xsupplier.Items.Add(baca("nama_supplier"))
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
5
n = n + 1 End While Catch ex As Exception MessageBox.Show("Koneksi Gagal !!!, karena " & ex.Message) End Try koneksi.Close() n = n + 1 'Dim EnteredDate As Date = Date.ParseExact(TextBox1.Text.ToString(), "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo) koneksi.Close() koneksi.Open() sql = "INSERT INTO h_beli(supplier,tanggal) VALUES ('" & ComboBox1.Text & "','" & Format(xtanggal_beli.Value, "yyyy/MM/dd") & "')" cmd.Dispose() cmd = New SqlClient.SqlCommand(sql) cmd.Connection = koneksi cmd.ExecuteNonQuery() TextBox2.Text = Format(Me.xtanggal_beli.Value, "dd‐MM‐yyyy") Ftrandbeli.xtgl.Text = Form1.DateTimePicker1.Value.ToString("dd‐MM‐yyyy") Ftrandbeli.xno.Text = n Ftrandbeli.xsup.Text = ComboBox1.Text Ftrandbeli.xnoref = xnoref Ftrandbeli.xnofaktur = xnofaktur Ftrandbeli.Show() End Sub
8.Tambahkan form dengan nama : Ftrandbeli 8.a.Buat desain seperti dibawah
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
6
8.b.Ubah propertise : Oject Label1 Label2 Label3 Label4 Label5 Textbox1 Textbox2 Textbox3 Textbox4 Textbox5 Label6 Lebel7 Label8 Combobox1 Textbox6 Textbox7 Textbox8 Button1 Button2 Datagridview Label9 Label10 Label11 Textbox9 Textbox10 Textbo11 Button3
Propertise Text Text Text Text Text Name Name Locked name Locked name Locked name Locked Text Text Text Name Name Name Name Name Text Name Text Name Text Text Text Name Readonly Name Name Name Text
Isikan No.Pencatatan Supplier Tanggal No Referensi No Faktur dan Supplier Xno Xsup True Xtgl True Xnoref True Xnofaktur True Nama barang Harga beli Jumlah beli Combobox1 Xharga Xjum_beli xkunci_hapus SIMPAN SIMPAN HAPUS HAPUS Databeli Total Beli DIBAYAR Kembali / Kurang Xtotal True Xbayar xsisa_kurang SELESAI SELESAI
8.c Pada awal form, cara doubleclik form , tambahkan koding dibawah: Imports System.Data.Sql Imports System.Data.SqlClient Public Class Ftranbeli Sub tampil() koneksi.Close() buka()
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
7
cmd = New SqlClient.SqlCommand("SELECT kode_barang,nama_barang,jumlah_beli,harga_beli FROM tabel_dbeli where id_hbeli='" & xno.Text & "'", koneksi) adaptor = New SqlClient.SqlDataAdapter adaptor.SelectCommand = cmd data = New Data.DataSet adaptor.Fill(data, "c") DATABELI.DataSource = data DATABELI.DataMember = "c" atur() Hitung() End Sub Sub atur() With DATABELI.ColumnHeadersDefaultCellStyle DATABELI.Columns(0).HeaderText = "Kode Barang" DATABELI.Columns(1).HeaderText = "Nama Barang" DATABELI.Columns(2).HeaderText = "Jumlah" DATABELI.Columns(3).HeaderText = "Harga satuan" DATABELI.Columns(3).DefaultCellStyle.Format = "Rp" + "###,###,###" .Alignment = DataGridViewContentAlignment.MiddleCenter .BackColor = Color.DarkRed .ForeColor = Color.Gold '.Columns(3).DefaultCellStyle.Format = "#,##" .Font = New Font(.Font.FontFamily, .Font.Size, _ .Font.Style Or FontStyle.Bold, GraphicsUnit.Point) .Font = New Font("cambria", 11) End With End Sub Sub Hitung() 'total di data grid Dim total As Double total = 0 For t As Integer = 0 To DATABELI.Rows.Count ‐ 1 total = total + (Val(DATABELI.Rows(t).Cells(2).Value) * Val(DATABELI.Rows(t).Cells(3).Value)) 'total = total + Val(DATABELI.Rows(t).Cells(3).Value) 'cell (2) disini menunjukan posisi kolom pada datagridview yang akan kita jumlahkan Next XTOTAL.Text = total End Sub 8.d Event untuk form load Private Sub Ftranbeli_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'koneksi.Close() buka() Try 'piye cmd = New SqlClient.SqlCommand("SELECT * FROM Table_barang", koneksi) 'cmd = New MySqlCommand("SELECT kode,nama FROM coba", koneksi) baca = cmd.ExecuteReader() While baca.Read() ComboBox1.Items.Add(baca("nama_barang")) End While Catch ex As Exception MessageBox.Show("Koneksi Gagal !!!, karena " & ex.Message) End Try ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
8
koneksi.Close() tampil() End Sub
8.e Tombol simpan Private Sub SIMPAN_Click(sender As Object, e As EventArgs) Handles SIMPAN.Click Dim vkd As String Dim beli As Decimal ' beli = Decimal.Parse(XJUM_BELI.Text) koneksi.Close() koneksi.Open() sql = "SELECT * FROM Table_barang WHERE nama_barang='" + ComboBox1.Text + "'" cmd = New SqlCommand(sql, koneksi) baca = cmd.ExecuteReader() If baca.HasRows = True Then baca.Read() vkd = baca.Item("kode_barang") koneksi.Close() koneksi.Open() sql = "INSERT INTO tabel_dbeli(id_hbeli, kode_barang, nama_barang, harga_beli, jumlah_beli) VALUES" + "('" & xno.Text & "','" & vkd & "','" & ComboBox1.Text & "','" & XHARGA.Text & "','" & XJUM_BELI.Text & "')" cmd.Dispose() cmd = New SqlClient.SqlCommand(sql) cmd.Connection = koneksi cmd.ExecuteNonQuery() Call Hitung() Call tampil() ComboBox1.Text = "" XJUM_BELI.Text = "" XHARGA.Text = "" MsgBox("Data berhasil di Input", MsgBoxStyle.Information, "Information") End If End Sub 8.f Datagridview double clik
Private Sub DATABELI_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DATABELI.CellContentClick 'MsgBox(DATABELI.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Dim baris As Integer With DATABELI baris = .CurrentRow.Index xkunci_hapus.Text = .Item(0, baris).Value ' TextBox1.Text = baris
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
9
End With End Sub
8.g Tombol Hapus Private Sub HAPUS_Click(sender As Object, e As EventArgs) Handles HAPUS.Click Dim vkd As String vkd = xkunci_hapus.Text koneksi.Close() koneksi.Open() sql = "delete from tabel_dbeli where kode_barang='" & vkd & "' and id_hbeli ='" & xno.Text & "'" cmd.Dispose() cmd = New SqlClient.SqlCommand(sql) cmd.Connection = koneksi cmd.ExecuteNonQuery() Call tampil() End Sub Tombol Selesai Private Sub SELESAI_Click(sender As Object, e As EventArgs) Handles SELESAI.Click ‘tambahkan update tabel h_beli pada total, uang_muka, sisa,kurang sql = "update hbeli set total='"& xtotal.text & "', sisa='"& xtotal.text & "', kurang='"& xtotal.text & "'where id_hbeli ='" & xno.Text & "'" cmd.Dispose() cmd = New SqlClient.SqlCommand(sql) cmd.Connection = koneksi cmd.ExecuteNonQuery() sql = "insert into hutang(,,)values(,,)” cmd.Dispose() cmd = New SqlClient.SqlCommand(sql) cmd.Connection = koneksi cmd.ExecuteNonQuery() Me.Hide() End Sub
9 . Ubah start up a. Pilih menu view, Solusition explore b.Pilih nama aplikasi, click kanan, propertise
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
10
d.Star up ,pilih formnya
STMIK SINAR NUSANTARA,SURAKARTA
Materi Pemrograman Database Didik Nugroho,S.Kom,M.kom
11
Tugas ! 1.Buatlah Transaksi Pembelian diatas 2.Buatlah form untuk mencatat penjualan pembelian.
STMIK SINAR NUSANTARA,SURAKARTA