By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

populating the combobox based on the selected item in the first combobox

P: 3
I want to populate the combobox based on the selected item in the first combobox

The following code is written in vb.net 2008
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Sub fillcombo()
  3.         strsql = "select * from device"
  4.         Dim cmd As New OleDb.OleDbCommand
  5.         cmd.CommandText = strsql
  6.         cmd.Connection = con
  7.         dr = cmd.ExecuteReader
  8.         While (dr.Read())
  9.             ComboBox1.Items.Add(dr("device"))
  10.         End While
  11.         cmd.Dispose()
  12.         dr.Close()
  13.  
  14.     End Sub
  15.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  16.         Module1.connect()
  17.         Me.fillcombo()
  18.     End Sub
  19.  
  20.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  21.         strsql = "select part from part where device='" & ComboBox1.Text & "'"
  22.         Dim cmd As New OleDb.OleDbCommand
  23.         cmd.CommandText = strsql
  24.         cmd.Connection = con
  25.         dr = cmd.ExecuteReader
  26.         If (dr.Read() = True) Then
  27.             ComboBox2.Text = (dr("part"))
  28.         End If
  29.         cmd.Dispose()
  30.         dr.Close()
  31.     End Sub
  32. End Class
for this code I am able to populate the first combobox and when i select the specific item in combobox then the second combobox shows oly the first element of that item

In MS access 2007 i hav created 2 tables:
1)contains the device ex:computer
2)contains the device and its parts ex:cpu,monitor so here the device has duplicate values

table names are device and parts
guys help!!!!!
Jan 10 '12 #1
Share this Question
Share on Google+
7 Replies


Seth Schrock
Expert 2.5K+
P: 2,944
Check out this discussion: Cascaded Form Filtering
Jan 13 '12 #2

P: 3
I am doing a project which has front end of vb.net and back end of ms access.The main focus is on vb.net just for the database purpose i am using access.

I want to link the database to the vb.net program just for accessing the data from the database.
Jan 13 '12 #3

Frinavale
Expert Mod 5K+
P: 9,731
In your ComboBox1_SelectedIndexChanged method...
You are setting the ComboBox2's Text property over and over again in your loop instead of populating the ComboBox2 with items.

-Frinny
Jan 13 '12 #4

P: 3
so what should i change in that to populate
Jan 13 '12 #5

Frinavale
Expert Mod 5K+
P: 9,731
Do the same thing you are doing in the fillcombo to fill the first combo box ;)

-Frinny
Jan 13 '12 #6

P: 5
Expand|Select|Wrap|Line Numbers
  1. Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
  2.  
  3. ComboSecond.items.add(ComboFirst.Items.Item(Me.ComboFirst.SelectedIndex)
  4.  
  5. End sub
Use above code on SelectedIndexChanged Event
Jan 15 '12 #7

Frinavale
Expert Mod 5K+
P: 9,731
Monty,

Sheela doesn't want to populate the second ComboBox with the same item as the firstComboBox...she wants to populate the second ComboBox based on what was selected in the first ComboBox.

The only problem in her posted code is that she's setting the Text property of the second ComboBox instead of adding items to the combo box.

To fix the problem she just has to change one line of code:
Expand|Select|Wrap|Line Numbers
  1.  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2.         strsql = "select part from part where device='" & ComboBox1.Text & "'"
  3.         Dim cmd As New OleDb.OleDbCommand
  4.         cmd.CommandText = strsql
  5.         cmd.Connection = con
  6.         dr = cmd.ExecuteReader
  7.         If (dr.Read() = True) Then
  8.         'The following line needs to be changed 
  9.         '    ComboBox2.Text = (dr("part"))
  10.         'To this:
  11.              ComboBox2.Items.Add(dr("part"))
  12.         End If
  13.         cmd.Dispose()
  14.         dr.Close()
  15.     End Sub
Jan 16 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.