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

How to populate a second combobox when selecting item from the first combobox

P: 11
Hi, Could anyone help me out!!!

I'm using VB 2010 and Access Database file. My problem is I'm unable to populate the second combobox when selection of item is made from the first combobox.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.OleDb
  4. Public Class Form1
  6.     Dim con As New OleDbConnection
  7.     Dim dbProvider, dbSource As String
  8.     Dim ds1, ds2, ds3 As New DataSet
  9.     Dim da1, da2, da3 As OleDbDataAdapter
  10.     Dim sql1, sql2, sql3, selectcombo As String
  13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14.         dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
  15.         dbSource = "Data Source=C:\Users\Genius\Desktop\VBCode\Usertable.mdb"
  16.         con.ConnectionString = dbProvider & dbSource
  17.         con.Open()
  19.         popcbo3()
  20.         popcbo2()
  22.     End Sub
  24.     Private Sub popcbo3()
  25.         sql3 = "SELECT DISTINCT username FROM Usertbl ORDER BY Username"
  26.         da3 = New OleDb.OleDbDataAdapter(sql3, con)
  27.         da3.Fill(ds3, "Username")
  28.         cboUser.DataSource = ds3.Tables(0)
  29.         cboUser.DisplayMember = "Username"
  30.         cboUser.ValueMember = "username"
  31.     End Sub
  32.     Private Sub popcbo2()
  33.         sql2 = "Select Distinct type From Usertbl where username= '" & cboUser.SelectedValue.ToString() & "'"
  34.         da2 = New OleDb.OleDbDataAdapter(sql2, con)
  35.         da2.Fill(ds2, "type")
  36.         cboType.DataSource = ds2.Tables(0)
  37.         cboType.DisplayMember = "type"
  38.         cboType.ValueMember = "type"
  39.     End Sub
  40. End Class
>>> What code should be entered in the 'CboUser selected Item Index'??? If I put 'pop3 ()' no changes is made when I select from cboUser combobox in the contrary it duplicates the CboType combobox values...

Pls any suggestion from anyone... Thanks in advance
Jan 4 '13 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 10K+
P: 12,383
You should use one of the combo box events to call popcbo2, I don't know why you would call popcbo3 since popcbo2 is the one that has any sort of filtering in its SQL.
Jan 7 '13 #2

Post your reply

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