10 June 2015


Dari judul mungkin udah jelas artikel ini tentang apa, jadi langsung aja yaaah...
Dua tabel dari database yang akan kita gunakan yaitu:
Tabel Kategori:

Tabel Item:

Atur UI di form sebagai berikut:

Seperti biasa kita perlu mengimport OLEDB untuk membuat koneksi Access, di bagian paling atas jendela code.
Imports System.Data.OleDb

Kemudian di antara area class form kita deklarasikan object koneksi dan buat sebuah sub procedure untuk mebuka koneksi.
Dim con As New OleDbConnection   

Sub Open_Koneksi()
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _
                           ";Data Source=latihan.accdb;" & _
                           "Persist Security Info=False;"
    con.Open()
End Sub


Kode untuk menampilkan data di ComboBox1
Sub ListKategori()
    Dim cmd As New OleDbCommand
   
Dim adapt As New OleDbDataAdapter
   
Dim dt As New DataTable

    Try
        If Not con.State = ConnectionState.Open Then Open_Koneksi()

        cmd.Connection = con
        cmd.CommandText = "Select kodekategori, namakategori From kategori"

        adapt.SelectCommand = cmd
        adapt.Fill(dt)

        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "namakategori"
        ComboBox1.ValueMember = "kodekategori"

        con.Close()
    Catch myerror As OleDbException
        MessageBox.Show("Error: " & myerror.Message)
    Finally
          con.Dispose()
    End Try
End Sub


Runtime:


Kode selengkapnya:
Imports System.Data.OleDb

Public Class Form2
  Dim con As New OleDbConnection   

  Sub Open_Koneksi()
      con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _
                             ";Data Source=latihan.accdb;" & _
                             "Persist Security Info=False;"
      con.Open()
  End Sub

  Sub ListKategori()
      Dim cmd As New OleDbCommand
     
Dim adapt As New OleDbDataAdapter
     
Dim dt As New DataTable

      Try
          If Not con.State = ConnectionState.Open Then Open_Koneksi()

          cmd.Connection = con
          cmd.CommandText = "Select kodekategori, namakategori From kategori"

          adapt.SelectCommand = cmd
          adapt.Fill(dt)

          ComboBox1.DataSource = dt
          ComboBox1.DisplayMember = "namakategori"
          ComboBox1.ValueMember = "kodekategori"

          con.Close()
      Catch myerror As OleDbException
          MessageBox.Show("Error: " & myerror.Message)
      Finally
          con.Dispose()
      End Try
  End Sub


  Sub DisplayToGrid(ByVal KodeKategori As String)
      Dim cmd As New OleDbCommand
     
Dim adapt As New OleDbDataAdapter
     
Dim dt As New DataTable

      Try
          If Not con.State = ConnectionState.Open Then Open_Koneksi()

          cmd.Connection = con
          cmd.CommandText = "SELECT kodeitem, namaitem FROM item " & _
                            "WHERE kodekategori = '" & KodeKategori & "'"

          adapt.SelectCommand = cmd
          adapt.Fill(dt)

          DataGridView1.DataSource = dt

      Catch ex As OleDbException
          MessageBox.Show("Error: " & ex.Message)
      Finally
          con.Close()
      End Try
  End Sub


  Private Sub Form2_Load(sender As Object, _
        e As EventArgs) Handles MyBase.Load

      ComboBox1.DropDownStyle = ComboBoxStyle.DropDownLis
      ListKategori()


  End Sub

  Private Sub ComboBox1_SelectedValueChanged(sender As Object, _
          e As EventArgs) Handles ComboBox1.SelectedValueChanged
     
DisplayToGrid(ComboBox1.SelectedValue.ToString)
  End Sub

End Class



Click here if you like this article.


2 comments:

Unknown said...

mba rani kalo menampilkan datagrid dari textboxt_changed gimana yh?? ..

makasih ilmunya bermanfaat banget mba ..

rani irsan said...

sama aja mestinya, itu kan event nya aja yang beda