Untuk artikel yang satu ini Rani agak bingung memberi judul sebenarnya.
Intinya jika terdapat 2 buah combobox yang ingin menampilkan data dari database, dimana tampilan combobox kedua merupakan hasil filter dari combobox pertama.
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
Kode untuk menampilkan data di ComboBox2
Sub ListItem(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)
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "namaitem"
ComboBox2.ValueMember = "kodeitem"
con.Close()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
con.Dispose()
End Try
End Sub
Kita akan memanggil sub ListKategori pada saat event form load.
Private Sub Form2_Load(sender As Object, _
e As EventArgs) Handles MyBase.Load
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
ListKategori()
End Sub
Dan memanggil ListItem pada saat event combobox1 selectedvaluechanged.
Private Sub ComboBox1_SelectedValueChanged(sender As Object, _
e As EventArgs) Handles ComboBox1.SelectedValueChanged
ListItem(ComboBox1.SelectedValue.ToString)
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 ListItem(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)
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "namaitem"
ComboBox2.ValueMember = "kodeitem"
con.Close()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub Form2_Load(sender As Object, _
e As EventArgs) Handles MyBase.Load
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
ListKategori()
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, _
e As EventArgs) Handles ComboBox1.SelectedValueChanged
ListItem(ComboBox1.SelectedValue.ToString)
End Sub
End Class
Click here if you like this article.
23 Comments
mbak, mau nanya, gmana cara nya menyimpan ke database melalui combobox tetapi di dalam combobox nya itu terdapat 2 fields., seperti kode kategori dan nama kategori..jd combobox nya itu ada kode dan nama nya mbak., terus disimpan ke database tetapi yang kode tersimpan ke fields kode dan yang nama tersimpan ke nama kategori nya mbak...
tapi klo cuman kode dan nama biasanya kayak contoh diatas
ComboBox2.DisplayMember = "namaitem"
ComboBox2.ValueMember = "kodeitem"
jadi nanti nilai yang diambil buat disimpan ke database nya ComboBox1.selectedvalue dan ComboBox1.Text
saya ga ngecekin codingan, jadi coba cek lebih teliti.
modal dasar seorang coder salah satu nya adalah teliti ^_^
soal nya seperti biasa mbak, buat nya menggunakan adodb di .Net mbak., jd yah gtu lah mbak, pusing mbak..^_^
Sub division()
'rs = New ADODB.Recordset
rs = cn.Execute("SELECT distinct [division_code], [division_description] FROM [AN_SUMATRA].[dbo].[TM_tb_subsection]")
If ((rs.EOF = False) And (rs.BOF = False)) = True Then
While Not rs.EOF
cbo_devision.Items.Add(rs(0).Value.ToString & Space(2) & "|" & Space(2) & rs(1).Value.ToString)
cbo_devision.ValueMember = rs(0).Value.ToString
cbo_devision.DisplayMember = rs(1).Value.ToString
rs.MoveNext()
End While
End If
' cn.Close()
End Sub
ini combobox yg ke 2 nya mbak:
Sub department(KodeDivision As String)
rs = cn.Execute("SELECT distinct [department_code], [department_description] FROM [AN_SUMATRA].[dbo].[TM_tb_subsection] where [division_code] = '" & KodeDivision & "'")
If ((rs.EOF = False) And (rs.BOF = False)) = True Then
While Not rs.EOF
cbo_departemen.Items.Add(rs(0).Value.ToString & Space(2) & "|" & Space(2) & rs(1).Value.ToString)
cbo_departemen.ValueMember = rs(0).Value.ToString
cbo_departemen.DisplayMember = rs(1).Value.ToString
rs.MoveNext()
End While
End If
'cn.Close()
End Sub
Private Sub cbo_devision_SelectedValueChanged(sender As Object, e As EventArgs) Handles cbo_devision.SelectedValueChanged
department(cbo_devision.SelectedValue.ToString)
End Sub
kira2 apa yang salah yah mbak., saya gak bisa2 mbak..
Dim da As New OleDbDataAdapter()
Dim dt As New DataTable()
Dim SQL As String
Dim rsTable As ADODB.Recordset
rsTable = New ADODB.Recordset
OpenConnection() 'Sub untuk membuka koneksi
SQL = "SELECT categorycode, categoryname FROM category"
rsTable = New ADODB.Recordset
With rsTable
.CursorLocation = CursorLocationEnum.adUseClient
.LockType = LockTypeEnum.adLockBatchOptimistic
.CursorType = CursorTypeEnum.adOpenStatic
.CacheSize = 30
.ActiveConnection = xConn
.Open(SQL)
End With
da.Fill(dt, rsTable)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "categoryname"
ComboBox1.ValueMember = "categorycode"
xConn.Close() 'xconn = objek koneksi
Soalnya saya sdh hmpir 1 minggu gak ketumu caranya
errornya cannot bind to the new value member. parameter name : value