473,320 Members | 2,035 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,320 software developers and data experts.

Cascading combo box not working

i am a newbbie am trying to have 3 cascading comboboxes for Country, State and City all contained in a 3 tables in Ms Access Database and related via foreign keys. When the country combobox is has a selected the other two will show the relevant state and cities for that country selected. in the meantime the first combobox is the one populating the countries and nothing for the State and cities. please help
the code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Configuration
  3. Imports System.Data.OleDb
  4.  
  5. Public Class Form1
  6.     Dim strConn As String = "Provider= Microsoft.Ace.Oledb.12.0; Data source =" & Environment.CurrentDirectory & "\Practice_Country_combo1.accdb"
  7.     Dim cn As New OleDbConnection(strConn)
  8.     Dim Cmd As OleDbCommand
  9.     Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.         LoadCountry()
  11.     End Sub
  12.     Private Sub LoadCountry()
  13.         Using cn As New OleDbConnection(strConn)
  14.             Using cmd As New OleDbCommand()
  15.                 Try
  16.                     With cmd
  17.                         .Connection = cn
  18.                         .CommandType = CommandType.Text
  19.                         .CommandText = "SELECT CountryID, Country FROM Country"
  20.                     End With
  21.                     Dim ds As New DataSet()
  22.                     Dim da As New OleDbDataAdapter()
  23.                     da.SelectCommand = cmd
  24.                     cn.Open()
  25.                     da.Fill(ds)
  26.                     cn.Close()
  27.                     ComboBox1.ValueMember = "CountryID"
  28.                     ComboBox1.DisplayMember = "Country"
  29.                     ComboBox1.DataSource = ds.Tables(0)
  30.                 Catch ex As Exception
  31.  
  32.                 End Try
  33.             End Using
  34.         End Using
  35.     End Sub
  36.  
  37.     Private Sub LoadProvince(ByVal countryID As Integer)
  38.         Using cn As New OleDbConnection(strConn)
  39.             Using Cmd As New OleDbCommand()
  40.                 Try
  41.                     With Cmd
  42.                         .Connection = cn
  43.                         .CommandType = CommandType.Text
  44.                         .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"
  45.                     End With
  46.                     Cmd.Parameters.AddWithValue("?CountryID", OleDbType.Integer)
  47.                     Dim ds As New DataSet()
  48.                     Dim da As New OleDbDataAdapter()
  49.                     da.SelectCommand = Cmd
  50.                     cn.Open()
  51.                     da.Fill(ds)
  52.                     cn.Close()
  53.                     If ds.Tables(0).Rows.Count > 0 Then
  54.                         ComboBox2.ValueMember = "ProvinceID"
  55.                         ComboBox2.DisplayMember = "Province"
  56.                         ComboBox2.DataSource = ds.Tables(0)
  57.                     End If
  58.                 Catch ex As Exception
  59.  
  60.                 End Try
  61.             End Using
  62.         End Using
  63.     End Sub
  64.  
  65.     Private Sub LoadCity(ByVal ProvinceID As Integer)
  66.         Using cn As New OleDbConnection(strConn)
  67.             Using cmd As New OleDbCommand()
  68.                 Try
  69.                     With cmd
  70.                         .Connection = cn
  71.                         .CommandType = CommandType.Text
  72.                         .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =?"
  73.                     End With
  74.                     cmd.Parameters.AddWithValue("?ProvinceID", OleDbType.Integer)
  75.                     Dim ds As New DataSet()
  76.                     Dim da As New OleDbDataAdapter()
  77.                     da.SelectCommand = cmd
  78.                     cn.Open()
  79.                     da.Fill(ds)
  80.                     cn.Close()
  81.                     If ds.Tables(0).Rows.Count > 0 Then
  82.                         ComboBox3.DataSource = ds.Tables(0)
  83.                         ComboBox3.DisplayMember = "City"
  84.                         ComboBox3.ValueMember = "CityID"
  85.                     End If
  86.                 Catch ex As Exception
  87.  
  88.                 End Try
  89.             End Using
  90.         End Using
  91.     End Sub
  92.  
  93.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  94.         If ComboBox1.SelectedValue.ToString() <> "" Then
  95.             Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
  96.             LoadProvince(CountryID)
  97.             ComboBox3.SelectedIndex = 0
  98.         End If
  99.     End Sub
  100.  
  101.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  102.         Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
  103.         LoadCity(ProvinceID)
  104.     End Sub
  105.  
  106.  
  107.  
  108. End Class
Apr 10 '19 #1
1 2644
SioSio
272 256MB
In Subroutine LoadProvince()

Expand|Select|Wrap|Line Numbers
  1. With Cmd
  2.   .Connection = cn
  3.   .CommandType = CommandType.Text
  4.   .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =" & comboBox1.SelectedValue.ToString
  5. End With
And
In SUbroutine LoadCity()

Expand|Select|Wrap|Line Numbers
  1. With cmd
  2.   .Connection = cn
  3.   .CommandType = CommandType.Text
  4.   .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =" & comboBox2.SelectedValue.ToString
  5. End With
Dec 16 '19 #2

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

Similar topics

0
by: cognoscento | last post by:
I'm currently putting together a database for my work (not an expert by any stretch, so muddling through as best as I can... you know the story...) and I could use some advice and hand-holding ...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
2
by: ShadowHawk | last post by:
Hi Everyone. I've been having a little touble with a form I'm working on. (I'm a MS Access hobbiest). I've set up the cascading combo boxes, which is working, (I took the code from Microsoft) on a...
4
klarae99
by: klarae99 | last post by:
Hello, I am working on an Access 2003 Database. The tables that pertain to this issue are tblOrg, tblState, tblCity, and tblZip. I have posted the table structure with only the pertinant fields...
3
kcdoell
by: kcdoell | last post by:
I have 5 cascading combo boxes on a form. Below is a sample of my vb in the first combo box: Private Sub CboDivision_AfterUpdate() 'When the Division is selected, the appropriate Segment...
7
by: Toireasa | last post by:
Hi, Newbie Access developer here, and my first post on this forum, so I might not get everything right - thanks in advance for your help and your patience! I'm using Access 2007, in XP. I'm...
1
by: bluclouds9 | last post by:
I am new to Access and have been charged with creating a database for our course alumni. I currently have a "Contacts" form and am trying to create a subform to hold the course alumni...
3
by: MOCaseA | last post by:
I have a record lookup form that has several combo box filters set up. However I noticed a slight problem. The filters are working correctly, but there are now over 2000 entries and when filtering...
2
by: ncsthbell | last post by:
I am using MS Access 2007. I have a form that has two combo boxes, one for divisions, the other for groups. I am trying to create ‘cascading’ boxes that will allow a division to be selected and...
4
by: rhuseman | last post by:
On my form I have 24 combo boxes ( 12 of which are conditional/cascading combo boxes dependent on the users input of the other 12 combo boxes). I've found ways to do it by code each individual...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.