473,544 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 9753
Seth Schrock
2,965 Recognized Expert Specialist
Check out this discussion: Cascaded Form Filtering
Jan 13 '12 #2
sheela gupta
3 New Member
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 Recognized Expert Moderator Expert
In your ComboBox1_Selec tedIndexChanged 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
sheela gupta
3 New Member
so what should i change in that to populate
Jan 13 '12 #5
Frinavale
9,735 Recognized Expert Moderator Expert
Do the same thing you are doing in the fillcombo to fill the first combo box ;)

-Frinny
Jan 13 '12 #6
Monty sing
5 New Member
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 SelectedIndexCh anged Event
Jan 15 '12 #7
Frinavale
9,735 Recognized Expert Moderator Expert
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
12069
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 combobox. What is the solution? Thank you in advance.
2
10244
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 "descr", and datasource to the array of MyObjects. How can i set the selected value of that combobox based on the ValueMember OR how can i find the...
4
6660
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 value as a varible. I am using the code below but it isn't working any ideas am I using the right method. combobox1.selecteditem = getcolvalue(0)...
2
5191
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 display area of a combobox after removing all of its
4
2508
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 with DataTextField and DataValueField; this works as expected. However, my problems arises when I want to read the selected value (i.e. after a...
2
3670
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 different item no in it..then, these records will be saved in a temporary datatable (which i made in a separate class, the name is WBASKET)...The item nos...
2
9033
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 the other combobox based on the first combo value
1
5218
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 datasource. When the user selects an item in the combobox,and enters the item(text) in the 2 text box , I want all the 3(combobox selected item, and...
1
1396
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 the item that user selects in a text box. However, instead of the desired result being displayed the text box is returning system.data.datarowview....
0
1405
by: yousof | last post by:
i have web page that contian combobox filled from table If CtvAct.GetRecords("Fill_ActivityTb") = True Then AcivityCombo.DataSource = CtvAct.MainDataset.Tables("tbOLActivity").DefaultView AcivityCombo.DataTextField = "ActivityName" AcivityCombo.DataValueField = "ActivityId" ...
0
7437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7373
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7717
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5306
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3427
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.