Skenario :
1. Pada saat form di panggil, tampilkan kode_barang di listbox1
2. Pada saat form di panggil, tampilkan kode_barang dan nama_barang
di combobox1
3. Pada saat kode barang di listbox1 diklik, maka tampilkan :
a. Kode_barang di textbox1
b. Nama_barang di textbox2
c. Harga_beli di textbox3
d. Harga_jual di textbox4
e. Jumlah_barang di textbox5
f. Satuan di textbox6
4. Pada saat data di combobox1 diklik, maka tampilkan :
a. Kode_barang di textbox1
b. Nama_barang di textbox2
c. Harga_beli di textbox3
d. Harga_jual di textbox4
e. Jumlah_barang di textbox5
f. Satuan di textbox6
5. Pada saat textbox7 diketik nama_barang, maka jika data ditemukan
tampilkan data tersebut ke dalam grid, jika data tidak ditemukan tampilkan
pesan bahwa data tidak ada
6. Pada saat button1 di klik, maka tampilkan seluruh data barang ke
dalam grid
7. Pada saat button1 di klik, maka tampilkan jumlah seluruh record
yang ada dalam grid
8. Pada saat button1 di klik, maka tampilkan seluruh total harga_beli
yang ada dalam grid
9. Pikirkan ide lain untuk melakukan manipulasi (mencari menampilkan)
database
Jawaban :
Imports System.Data.OleDb
Public Class Form2
Dim Conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim rd As OleDbDataReader
Dim ds As DataSet
Dim cmd As OleDbCommand
Dim str As String
Sub KOneksi()
str = "provider=microsoft.jet.oledb.4.0;data source=db1.mdb"
Conn = New OleDbConnection(str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Call KOneksi()
cmd = New OleDbCommand("select * from
barang", Conn)
rd = cmd.ExecuteReader
Do While rd.Read
ListBox1.Items.Add(rd.GetString(0))
ComboBox1.Items.Add(rd.GetString(0)
& Space(5) &
rd.GetString(1))
Loop
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles
ListBox1.SelectedIndexChanged
cmd = New OleDbCommand("Select * from barang where
kode_barang='"
& ListBox1.Text & "'", Conn)
rd =
cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
TextBox1.Text =
rd.GetString(0)
TextBox2.Text =
rd.GetString(1)
TextBox3.Text =
rd.GetValue(2)
TextBox4.Text =
rd.GetValue(3)
TextBox5.Text =
rd.GetValue(4)
TextBox6.Text =
rd.GetString(5)
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles
ComboBox1.SelectedIndexChanged
cmd = New OleDbCommand("Select * from barang where
kode_barang='"
& Microsoft.VisualBasic.Left(ComboBox1.Text, 2) & "'",
Conn)
rd =
cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
TextBox1.Text =
rd.GetString(0)
TextBox2.Text =
rd.GetString(1)
TextBox3.Text =
rd.GetValue(2)
TextBox4.Text =
rd.GetValue(3)
TextBox5.Text =
rd.GetValue(4)
TextBox6.Text =
rd.GetString(5)
End If
End Sub
Sub TampilGrid()
da = New OleDbDataAdapter("Select * from barang", Conn)
ds = New DataSet
da.Fill(ds, "Barang")
DGV.DataSource =
ds.Tables("Barang")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Call TampilGrid()
Call CariJumlahData()
Call TotalHarga()
End Sub
Sub CariJumlahData()
TextBox8.Text = DGV.RowCount - 1
End Sub
Sub TotalHarga()
cmd = New OleDbCommand("Select sum(Harga_beli) as ketemu from
barang", Conn)
rd =
cmd.ExecuteReader
rd.Read()
TextBox9.Text =
rd.GetValue(0)
End Sub
Sub TampilNama()
da = New OleDbDataAdapter("select * from barang where
nama_barang like
'%" & TextBox7.Text & "%'", Conn)
ds = New DataSet
da.Fill(ds, "Barang")
DGV.DataSource =
ds.Tables("Barang")
End Sub
Private Sub TextBox7_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox7.GotFocus
TextBox7.Clear()
End Sub
Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles TextBox7.KeyPress
If e.KeyChar = Chr(13) Then
cmd = New OleDbCommand("select * from barang where
nama_barang like
'%" & TextBox7.Text & "%'", Conn)
rd =
cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
da = New OleDbDataAdapter("select * from barang where
nama_barang like
'%" & TextBox7.Text & "%'", Conn)
ds = New DataSet
da.Fill(ds, "Barang")
DGV.DataSource =
ds.Tables("Barang")
Call CariJumlahData()
Exit Sub
Else
MsgBox("Nama Barang tidak ditemukan")
Call TampilGrid()
End If
End If
End Sub
End
Class