the code is as follows:
Expand|Select|Wrap|Line Numbers
- Imports System.Data
- Imports System.Configuration
- Imports System.Data.OleDb
- Public Class Form1
- Dim strConn As String = "Provider= Microsoft.Ace.Oledb.12.0; Data source =" & Environment.CurrentDirectory & "\Practice_Country_combo1.accdb"
- Dim cn As New OleDbConnection(strConn)
- Dim Cmd As OleDbCommand
- Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- LoadCountry()
- End Sub
- Private Sub LoadCountry()
- Using cn As New OleDbConnection(strConn)
- Using cmd As New OleDbCommand()
- Try
- With cmd
- .Connection = cn
- .CommandType = CommandType.Text
- .CommandText = "SELECT CountryID, Country FROM Country"
- End With
- Dim ds As New DataSet()
- Dim da As New OleDbDataAdapter()
- da.SelectCommand = cmd
- cn.Open()
- da.Fill(ds)
- cn.Close()
- ComboBox1.ValueMember = "CountryID"
- ComboBox1.DisplayMember = "Country"
- ComboBox1.DataSource = ds.Tables(0)
- Catch ex As Exception
- End Try
- End Using
- End Using
- End Sub
- Private Sub LoadProvince(ByVal countryID As Integer)
- Using cn As New OleDbConnection(strConn)
- Using Cmd As New OleDbCommand()
- Try
- With Cmd
- .Connection = cn
- .CommandType = CommandType.Text
- .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"
- End With
- Cmd.Parameters.AddWithValue("?CountryID", OleDbType.Integer)
- Dim ds As New DataSet()
- Dim da As New OleDbDataAdapter()
- da.SelectCommand = Cmd
- cn.Open()
- da.Fill(ds)
- cn.Close()
- If ds.Tables(0).Rows.Count > 0 Then
- ComboBox2.ValueMember = "ProvinceID"
- ComboBox2.DisplayMember = "Province"
- ComboBox2.DataSource = ds.Tables(0)
- End If
- Catch ex As Exception
- End Try
- End Using
- End Using
- End Sub
- Private Sub LoadCity(ByVal ProvinceID As Integer)
- Using cn As New OleDbConnection(strConn)
- Using cmd As New OleDbCommand()
- Try
- With cmd
- .Connection = cn
- .CommandType = CommandType.Text
- .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =?"
- End With
- cmd.Parameters.AddWithValue("?ProvinceID", OleDbType.Integer)
- Dim ds As New DataSet()
- Dim da As New OleDbDataAdapter()
- da.SelectCommand = cmd
- cn.Open()
- da.Fill(ds)
- cn.Close()
- If ds.Tables(0).Rows.Count > 0 Then
- ComboBox3.DataSource = ds.Tables(0)
- ComboBox3.DisplayMember = "City"
- ComboBox3.ValueMember = "CityID"
- End If
- Catch ex As Exception
- End Try
- End Using
- End Using
- End Sub
- Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
- If ComboBox1.SelectedValue.ToString() <> "" Then
- Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
- LoadProvince(CountryID)
- ComboBox3.SelectedIndex = 0
- End If
- End Sub
- Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
- Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
- LoadCity(ProvinceID)
- End Sub
- End Class