Cascading Combo Box Advanced in Visual Studio using MS Access Database
Hello Everyone, i just started building my own Windows Form in VB.NET Project (Ver 2017),
and i just Wanted to know how to make VB.NET Cascading Combobox via OLEDB/Connect Ms Access Database which Similiar like Cascading Ms.Access method.
Follow me....
SO let's get started.
I am using following services: -
1. Visual Studio 2008(You can use any higher version but not lower than it)
2. MS Access 2007(You can use any higher version but not lower than it)
Let's go guyz.
First of all lets design the form.! :)
Create our database which we will be needing named: [dat.accdb]
We will create this database in the debug i.e. DataDirectory folder of the application.
For DataBase..
We will be having a table with State and City Columns.
Follow the Code..
1. Connecting Access Database
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim constring As String
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
2. Loading Data at Form Startup..
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
constring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dat.accdb"
With con
.ConnectionString = constring
.Open()
End With
Me.Label4.Text = "Connection State : Active!"
Me.Label4.ForeColor = Color.DarkGreen
FillStates()
End Sub
3. Filling data into Combo Box 1
Public Sub FillStates()
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "SELECT DISTINCT tbldat.State from tbldat"
End With
dr = cmd.ExecuteReader()
While dr.Read()
cbostates.Items.Add(dr("State"))
End While
End Sub
4. Filling data into Combo Box 2
Public Sub FillCities()
Dim cmd1 As New OleDbCommand
Dim dr1 As OleDbDataReader
With cmd1
.Connection = con
.CommandType = CommandType.Text
.CommandText = "SELECT * from tbldat where [State] ='" & Me.cbostates.Text & "'"
End With
dr1 = cmd1.ExecuteReader()
While dr1.Read()
cbocities.Items.Add(dr1("City"))
End While
End Sub
3. Closing Connection of Form Closing.
Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
con.Close()
Dispose()
End Sub
3. Filtering Data in Combo Box 2 Using Combo Box 1
Private Sub cbostates_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbostates.SelectedIndexChanged
If Me.cbocities.SelectedIndex > -1 Then
Me.cbocities.Items.Clear()
FillCities()
Else
FillCities()
End If
End Sub
End Class
*If you find this video helpful plz like, share, subscribe and comment for your queries and feedbacks.
Download Source Code







0 comments:
Post a Comment