473,626 Members | 3,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cascading combo box not working

1 New Member
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 2660
SioSio
272 Contributor
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
2065
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 I've got a subform with a series of cascading combo boxes (thanks to the Access tutorials on fontstuff.com) that let the user assign categories to items, in this case photos. This is being done to help constrain user selections and keep the...
4
64582
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 determines the available options in the other. TERMINOLOGY Row Source: The table/query from which the Combo Box or List Box gets its values. Note: There are other types of row sources that can be used but for simplicity we will stick with Tables...
2
2313
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 single item form. However as the form I need is a of tabular type, every time I update cbo1 to a value, the non matching cbo1's cbo2 field blanks out. The data remains as but is hidden, and not what I need. The data is all linked to bound...
4
3482
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 below. tblOrg OrgID, AutoNumber, PK ZipID, Number, FK tblState StateID, AutoNumber, PK
3
3975
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 list will 'display in the drop down list of CboSegment With Me! If IsNull(Me!cboDivision) Then
7
5827
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 currently trying to set up a whole pile of cascading combo boxes of different levels of complexity, so I started with the easiest set - and can't even get that to work (even using the tutorial on this site at...
1
1677
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 information. I would like the first combo box to contain the course and the second to show the approriate years. Everything seems to be working except the years are not showing in the second combo box. My tables are set up as: CourseNames CourseID...
3
2607
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 there are too many possible selection even with other filters applied. Example 1: POC Doe, John wants to look up a record he created on 01/01/2010. He selects his name and after updating he accidentally selects 01/02/2010 (despite that fact that...
2
6669
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 will populate the ‘group’ combo box with all the groups for that division. I have read on the internet about cascading combo boxes and have created mine the way I have seen in some other examples. My problem is that the ‘group’ combo box is never...
4
2893
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 combo box ( http://stackoverflow.com/questions/7624318/vb-net-cascading-comboboxes-connected-to-dataset-from-access-changing-units). But I would really like to lean the code out and have it all in one spot. I was thinking about putting this code...
0
8269
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.