473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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
7 9732
Seth Schrock
2,965 Expert 2GB
Check out this discussion: Cascaded Form Filtering
Jan 13 '12 #2
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
9,735 Expert Mod 8TB
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
so what should i change in that to populate
Jan 13 '12 #5
Frinavale
9,735 Expert Mod 8TB
Do the same thing you are doing in the fillcombo to fill the first combo box ;)

-Frinny
Jan 13 '12 #6
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
9,735 Expert Mod 8TB
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

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

Similar topics

8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
4
by: Michael Turner | last post by:
Hi I am having a problem with a combobox the values are generated from a recordset, I need to beable to change the selected item to the value specified in a configuration file, I have stored the...
2
by: Brett Romero | last post by:
After I remove all items from a combobox, the last selected item remains in the display area. I've tried: mycbox.SelectedText = string.Empty; and mycbox.refresh(); How do I clear the...
4
by: Ronny Mandal | last post by:
Hi! I have an .aspx with some controls that are created dynamically. The items are populated into the box by setting the DataSource-property to a list. In addition I specify the text and value...
2
by: kurtzky | last post by:
i created a form that should function as follows: i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a...
2
by: eihabisaac | last post by:
Hi everyone I'm using VS2005 C# with MySQL to do a windows application i'm also using Devart for MySQL i was able to populate a combobox from the database but i really want to populate...
1
by: sbandalli | last post by:
Hello, I have a Datagridview which has a combobox,and 2 textbox, The combobox is bound to a Datasource(Database Sql Server and the table name is Category) ,and Datagridview is not bounded to any...
1
by: starlight849 | last post by:
Hello, I am writing a program that is connection to an oracle database using the tableadapter method. I am populating a drop down list with a distinct set of values. I am then wanting to display...
0
by: yousof | last post by:
i have web page that contian combobox filled from table If CtvAct.GetRecords("Fill_ActivityTb") = True Then AcivityCombo.DataSource =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.