KELOMPOK (045,043,080) Tampilan Aplikasi
Setelah kemarin kita membuat 3 tabel yaitu tabel barang, tabel supplier dan tabel pembelian. Sekarang kita akan membuat aplikasi beradasarkan 3 tabel tersebut menggunakan VB.net sebagai bahasa pemogramannya dan SQL Server 2014 sebagai databasenya.
Cara Mengoneksikan Ke Database SQL Server 2014
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Module mdlkoneksi
Public comSQL As New SqlClient.SqlCommand
Public sql As String
Public conn As New SqlConnection
Public str As String = "Data Source=SARJONO-PC;Initial Catalog=KelompokSangar;Integrated Security=True"
Public Sub bukakoneksi()
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = str
Try
conn.Open()
'MsgBox("Koneksi Berhasil")
Catch ex As Exception
MsgBox("Koneksi Gagal: " & ex.ToString)
End Try
End If
End Sub
Public Sub tutupkoneksi()
If conn.State = ConnectionState.Open Then
Try
conn.Close()
Catch ex As Exception
MsgBox("Gagal menutup koneksi: " & ex.ToString)
End Try
End If
End Sub
End Module
Tampilan Form beserta Coding Simpan, Edit, Delete dan Tampilkan
1. From Menu
2. From Data Barang
Cara Mengoneksikan Ke Database SQL Server 2014
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Module mdlkoneksi
Public comSQL As New SqlClient.SqlCommand
Public sql As String
Public conn As New SqlConnection
Public str As String = "Data Source=SARJONO-PC;Initial Catalog=KelompokSangar;Integrated Security=True"
Public Sub bukakoneksi()
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = str
Try
conn.Open()
'MsgBox("Koneksi Berhasil")
Catch ex As Exception
MsgBox("Koneksi Gagal: " & ex.ToString)
End Try
End If
End Sub
Public Sub tutupkoneksi()
If conn.State = ConnectionState.Open Then
Try
conn.Close()
Catch ex As Exception
MsgBox("Gagal menutup koneksi: " & ex.ToString)
End Try
End If
End Sub
End Module
Tampilan Form beserta Coding Simpan, Edit, Delete dan Tampilkan
1. From Menu
Public Class FormMenu
Private Sub BarangToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BarangToolStripMenuItem.Click
Form1.MdiParent = Me
Form1.Show()
Form2.Hide()
Form3.Hide()
End Sub
Private Sub SupplierToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SupplierToolStripMenuItem.Click
Form2.MdiParent = Me
Form2.Show()
Form1.Hide()
Form3.Hide()
End Sub
Private Sub PembelianToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PembelianToolStripMenuItem.Click
Form3.MdiParent = Me
Form3.Show()
Form1.Hide()
Form2.Hide()
End Sub
End Class
2. From Data Barang
Imports System.Data.SqlClient
Public Class Form1
Private Sub tampil(ByVal aksi As String)
bukakoneksi()
sql = "SELECT * FROM Barang"
Dim da As New SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
Dim dt As New DataTable
For Each dt In ds.Tables
DataGridView1.DataSource = dt
Next
tutupkoneksi()
End Sub
Private Sub BtnSimpan_Click(sender As Object, e As EventArgs) Handles BtnSimpan.Click
bukakoneksi()
sql = "INSERT INTO Barang(kdbrg,nmbrg,jnsbrg,harga,jumlah) VALUES ( " &
"'" & TxtKodeBarang.Text & "'," & "'" & TxtNamaBarang.Text & "'," & "'" & TxtJenisBarang.Text & "'," & "'" & TxtHarga.Text & "'," &
"'" & TxtJumlah.Text & "')"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
Catch ex As Exception
End Try
tutupkoneksi()
tampil("")
lb_status.Text = "Simpan Berhasil"
End Sub
Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
bukakoneksi()
sql = "UPDATE barang SET nmbrg = '" & TxtNamaBarang.Text & "'," &
"jnsbrg='" & TxtJenisBarang.Text & "'," &
"harga='" & TxtHarga.Text & "'," &
"jumlah='" & TxtJumlah.Text & "'" &
"WHERE kdbrg='" & TxtKodeBarang.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("edit berhasil")
lb_status.Text = "Edit Berhasil"
Catch ex As Exception
'MsgBox("edit gagal")
End Try
tutupkoneksi()
tampil("")
End Sub
Private Sub BtnTampilkan_Click(sender As Object, e As EventArgs) Handles BtnTampilkan.Click
tampil("")
lb_status.Text = "Ok"
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TxtKodeBarang.Text = DataGridView1.Item(0, i).Value
TxtNamaBarang.Text = DataGridView1.Item(1, i).Value
TxtJenisBarang.Text = DataGridView1.Item(2, i).Value
TxtHarga.Text = DataGridView1.Item(3, i).Value
TxtJumlah.Text = DataGridView1.Item(4, i).Value
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
bukakoneksi()
sql = "DELETE FROM Barang Where kdbrg='" & TxtKodeBarang.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("hapus berhasil")
lb_status.Text = "Hapus Berhasil"
Catch ex As Exception
'MsgBox("hapus gagal")
End Try
tutupkoneksi()
tampil("")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tampil("")
End Sub
End Class
Imports System.Data.SqlClient
Public Class Form2
Private Sub tampil(ByVal aksi As String)
bukakoneksi()
sql = "SELECT * FROM Supplier"
Dim da As New SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
Dim dt As New DataTable
For Each dt In ds.Tables
DataGridView1.DataSource = dt
Next
tutupkoneksi()
End Sub
Private Sub BtnTampilkan_Click(sender As Object, e As EventArgs) Handles BtnTampilkan.Click
tampil("")
lb_status.Text = "Ok"
End Sub
Private Sub BtnSimpan_Click(sender As Object, e As EventArgs) Handles BtnSimpan.Click
bukakoneksi()
sql = "INSERT INTO Supplier(kdsup,nmsup,tgllhr,alamatkantor,telpkantor,jekel,email) VALUES
( " & "'" & TxtKodeSupplier.Text & "'," & "'" & TxtNamaSupplier.Text & "'," & "'" & TxtTanggalLahir.Text & "'," &
"'" & TxtAlamatKantor.Text & "'," & "'" & TxtTelponKantor.Text & "'," & "'" & TxtJenisKelamin.Text & "'," &
"'" & TxtEmail.Text & "')"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
Catch ex As Exception
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Simpan Berhasil"
End Sub
Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
bukakoneksi()
sql = "UPDATE Supplier SET " &
"nmsup='" & TxtNamaSupplier.Text & "'" &
"tgllhr='" & TxtTanggalLahir.Text & "'" &
"alamatkantor='" & TxtAlamatKantor.Text & "'" &
"telpkantor='" & TxtTelponKantor.Text & "'" &
"jekel='" & TxtJenisKelamin.Text & "'" &
"email='" & TxtEmail.Text & "'" &
"WHERE kdsup='" & TxtKodeSupplier.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("edit berhasil")
Catch ex As Exception
'MsgBox("edit gagal")
End Try
tutupkoneksi()
tampil("")
lb_status.Text = "Update Berhasil"
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
bukakoneksi()
sql = "DELETE FROM Supplier Where kdsup='" & TxtKodeSupplier.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("hapus berhasil")
Catch ex As Exception
'MsgBox("hapus gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Delete Berhasil"
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TxtKodeSupplier.Text = DataGridView1.Item(0, i).Value
TxtNamaSupplier.Text = DataGridView1.Item(1, i).Value
TxtTanggalLahir.Text = DataGridView1.Item(2, i).Value
TxtAlamatKantor.Text = DataGridView1.Item(3, i).Value
TxtTelponKantor.Text = DataGridView1.Item(4, i).Value
TxtJenisKelamin.Text = DataGridView1.Item(5, i).Value
TxtEmail.Text = DataGridView1.Item(6, i).Value
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tampil("")
End Sub
End Class
4.Form Data Pembelian
Imports System.Data.SqlClient
Public Class Form3
Private Sub tampil(ByVal aksi As String)
bukakoneksi()
sql = "SELECT * FROM Pembelian"
Dim da As New SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
Dim dt As New DataTable
For Each dt In ds.Tables
DataGridView1.DataSource = dt
Next
tutupkoneksi()
End Sub
Private Sub BtnSimpan_Click(sender As Object, e As EventArgs) Handles BtnSimpan.Click
bukakoneksi()
sql = "INSERT INTO Pembelian(nonota,kdbrg,kdsup,tanggalbeli,jumlahbeli,hargabeli,total) VALUES ( " &
"'" & TxtNoNota.Text & "'," & "'" & TxtKodeBarang.Text & "'," & "'" & TxtKodeSupplier.Text & "'," & "'" & TxtTanggalBeli.Text & "'," &
"'" & TxtJumlahBeli.Text & "'," & "'" & TxtHargaBeli.Text & "'," & "'" & TxtTotal.Text & "')"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("simpan berhasil")
Catch ex As Exception
'MsgBox("simpan gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Simpan Berhasil"
End Sub
Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
bukakoneksi()
sql = "UPDATE Pembelian SET " &
"kdbrg='" & TxtKodeBarang.Text & "'" &
"kdsup='" & TxtKodeSupplier.Text & "'" &
"tanggalbeli='" & TxtTanggalBeli.Text & "'" &
"jumlahbeli='" & TxtJumlahBeli.Text & "'" &
"hargabeli='" & TxtHargaBeli.Text & "'" &
"total='" & TxtTotal.Text & "'" &
"WHERE nonota='" & TxtNoNota.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("edit berhasil")
Catch ex As Exception
'MsgBox("edit gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Update Berhasil"
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
bukakoneksi()
sql = "DELETE FROM Pembelian Where nonota='" & TxtNoNota.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("hapus berhasil")
Catch ex As Exception
'MsgBox("hapus gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Delete Berhasil"
End Sub
Private Sub BtnTampilkan_Click(sender As Object, e As EventArgs) Handles BtnTampilkan.Click
tampil("")
lb_status.Text = "Ok"
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TxtNoNota.Text = DataGridView1.Item(0, i).Value
TxtKodeBarang.Text = DataGridView1.Item(1, i).Value
TxtKodeSupplier.Text = DataGridView1.Item(2, i).Value
TxtTanggalBeli.Text = DataGridView1.Item(3, i).Value
TxtHargaBeli.Text = DataGridView1.Item(4, i).Value
TxtJumlahBeli.Text = DataGridView1.Item(5, i).Value
TxtTotal.Text = DataGridView1.Item(6, i).Value
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tampil("")
End Sub
End Class
Public Class Form3
Private Sub tampil(ByVal aksi As String)
bukakoneksi()
sql = "SELECT * FROM Pembelian"
Dim da As New SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
Dim dt As New DataTable
For Each dt In ds.Tables
DataGridView1.DataSource = dt
Next
tutupkoneksi()
End Sub
Private Sub BtnSimpan_Click(sender As Object, e As EventArgs) Handles BtnSimpan.Click
bukakoneksi()
sql = "INSERT INTO Pembelian(nonota,kdbrg,kdsup,tanggalbeli,jumlahbeli,hargabeli,total) VALUES ( " &
"'" & TxtNoNota.Text & "'," & "'" & TxtKodeBarang.Text & "'," & "'" & TxtKodeSupplier.Text & "'," & "'" & TxtTanggalBeli.Text & "'," &
"'" & TxtJumlahBeli.Text & "'," & "'" & TxtHargaBeli.Text & "'," & "'" & TxtTotal.Text & "')"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("simpan berhasil")
Catch ex As Exception
'MsgBox("simpan gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Simpan Berhasil"
End Sub
Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
bukakoneksi()
sql = "UPDATE Pembelian SET " &
"kdbrg='" & TxtKodeBarang.Text & "'" &
"kdsup='" & TxtKodeSupplier.Text & "'" &
"tanggalbeli='" & TxtTanggalBeli.Text & "'" &
"jumlahbeli='" & TxtJumlahBeli.Text & "'" &
"hargabeli='" & TxtHargaBeli.Text & "'" &
"total='" & TxtTotal.Text & "'" &
"WHERE nonota='" & TxtNoNota.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("edit berhasil")
Catch ex As Exception
'MsgBox("edit gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Update Berhasil"
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
bukakoneksi()
sql = "DELETE FROM Pembelian Where nonota='" & TxtNoNota.Text & "'"
comSQL = New SqlCommand(sql, conn)
Try
comSQL.ExecuteNonQuery()
'MsgBox("hapus berhasil")
Catch ex As Exception
'MsgBox("hapus gagal")
End Try
tampil("")
tutupkoneksi()
lb_status.Text = "Delete Berhasil"
End Sub
Private Sub BtnTampilkan_Click(sender As Object, e As EventArgs) Handles BtnTampilkan.Click
tampil("")
lb_status.Text = "Ok"
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TxtNoNota.Text = DataGridView1.Item(0, i).Value
TxtKodeBarang.Text = DataGridView1.Item(1, i).Value
TxtKodeSupplier.Text = DataGridView1.Item(2, i).Value
TxtTanggalBeli.Text = DataGridView1.Item(3, i).Value
TxtHargaBeli.Text = DataGridView1.Item(4, i).Value
TxtJumlahBeli.Text = DataGridView1.Item(5, i).Value
TxtTotal.Text = DataGridView1.Item(6, i).Value
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tampil("")
End Sub
End Class
Komentar
Posting Komentar