-
Oct 8th, 2018, 03:18 AM
#1
Thread Starter
Hyperactive Member
Fill Combobox
Hello VBForums
Please if you can help me to resolve this problem
in my Table named " FACTORY " i have 3 fields ( Id - Marque - Type ) like this :
1 Domain Solde
2 Domain Pen
3 Domain Ruler
4 Domain Book
5 Session Processor
6 Session Phone
7 Session Regulator
8 Organisme Manifactur
9 Organisme Local
I filled ComboBox1 like this :
Code:
Private Sub Fill_Combobox()
Dim sqlstr As String
Dim InfoAdapter As OleDbDataAdapter
Dim InfoTable As DataSet
ComboBox1.Items.Clear()
sqlstr = "Select Distinct [Marque] from [Factory]"
InfoAdapter = New OleDbDataAdapter(sqlstr, constr)
InfoTable = New DataSet
InfoTable.Clear()
InfoAdapter.Fill(InfoTable, "[Factory]")
For Each rw As DataRow In InfoTable.Tables("[Factory]").Rows
ComboBox1.Items.Add(rw(0).ToString())
Next
End Sub
And in Form1 Load :
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Fill_Combobox()
End Sub
I want when i choose by ComboBox1 for examle ( Domain ) then appears in ComboBox2 (Solde-Pen-Ruler-Book)
If i choose by ComboBox1 for examle ( Session ) then appears in ComboBox2 (Processor-Phone-Regulator)
If i choose by ComboBox1 for examle ( Organisme ) then appears in ComboBox2 (Manifactur-Local)
The words in these two fields are variable .. I can not put method select case
Thank you in advance for help
MADA
Last edited by MADA BLACK; Oct 8th, 2018 at 03:25 AM.
-
Oct 8th, 2018, 03:36 AM
#2
Re: Fill Combobox
I suggest that you follow the CodeBank link in my signature below and check out my thread on Parent/Child data-binding. You simply have to fill two DataTables with the appropriate data. In your case, the parent table would be filled using:
Code:
SELECT DISTINCT Marque FROM Factory
while the child table would be filled using:
Code:
SELECT * FROM Factory
and then you would relate the tables on the Marque column.
-
Oct 8th, 2018, 03:39 AM
#3
Re: Fill Combobox
You can use the same kind of code, but change the SQL statement to something like this:
Code:
sqlstr = "Select [Type] from [Factory] WHERE [Marque] = '" & ComboBox1.SelectedItem &"'"
Ideally you would use a parameter instead of appending the value to the string.
For an explanation of why you should be using parameters (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).
edit: jmcilhinney's way is the more correct way to do it, but this way may be a bit easier for you.
-
Oct 8th, 2018, 07:03 AM
#4
Thread Starter
Hyperactive Member
Re: Fill Combobox
Thank you Si_The_Geek
I only have one problem left. how to make that when i choose by ComboBox1 the second or third time i find ComboBox2 empty of the elements that i have already chosen before
Cordially
MADA
-
Oct 8th, 2018, 10:55 AM
#5
Re: Fill Combobox
You could drop the DISTINCT statement on the database and kind of hodgepodge datasources as needed (works well for me). Here is an example.
Code:
Public Class ComboExample
Dim DT As New DataTable
Private Sub ComboExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DT
.Columns.Add("ID", GetType(System.Int32))
.Columns("ID").AutoIncrement = True
.Columns.Add("Marque", GetType(System.String))
.Columns.Add("Type", GetType(System.String))
End With
DT.Rows.Add(Nothing, "Domain", "Solde")
DT.Rows.Add(Nothing, "Domain", "Pen")
DT.Rows.Add(Nothing, "Session", "Processor")
DT.Rows.Add(Nothing, "Session", "Phone")
'End sample data
ComboBox1.DataSource = DT.DefaultView.ToTable(True, "Marque")
ComboBox1.DisplayMember = "Marque"
ComboBox1.ValueMember = "Marque"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
DT.DefaultView.RowFilter = "Marque = '" & ComboBox1.Text & "'"
Dim Combo2Source As DataTable = DT.DefaultView.ToTable(True, "Type")
ComboBox2.DataSource = Combo2Source
ComboBox2.ValueMember = "Type"
ComboBox2.DisplayMember = "Type"
End Sub
End Class
-
Oct 8th, 2018, 11:42 AM
#6
Thread Starter
Hyperactive Member
Re: Fill Combobox
Thank you very much kpmc for best example
But ..
Unfortunately this example will be useful if the data is constant while the data in my database is variable
I do not want to write the words (Domain - session) in the codes in my Form1 .. I just want to read from my Table.
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\COMBO_DB.accdb"
Dim DT As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DT
.Columns.Add("ID", GetType(System.Int32))
.Columns("ID").AutoIncrement = True
.Columns.Add("Marque", GetType(System.String))
.Columns.Add("Type", GetType(System.String))
End With
DT.Rows.Add(Nothing, "Domain", "Solde")
DT.Rows.Add(Nothing, "Domain", "Pen")
DT.Rows.Add(Nothing, "Session", "Processor")
DT.Rows.Add(Nothing, "Session", "Phone")
'End sample data
ComboBox1.DataSource = DT.DefaultView.ToTable(True, "Marque")
ComboBox1.DisplayMember = "Marque"
ComboBox1.ValueMember = "Marque"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
DT.DefaultView.RowFilter = "Marque = '" & ComboBox1.Text & "'"
Dim Combo2Source As DataTable = DT.DefaultView.ToTable(True, "Type")
ComboBox2.DataSource = Combo2Source
ComboBox2.ValueMember = "Type"
ComboBox2.DisplayMember = "Type"
End Sub
End Class
Thank you a lot master
Cordially
MADA
-
Oct 8th, 2018, 12:21 PM
#7
Re: Fill Combobox
Originally Posted by MADA BLACK
Thank you Si_The_Geek
I only have one problem left. how to make that when i choose by ComboBox1 the second or third time i find ComboBox2 empty of the elements that i have already chosen before
Cordially
MADA
You never mentioned you wanted to append the new results each time. We need to see your current code.
-
Oct 8th, 2018, 12:27 PM
#8
Re: Fill Combobox
Unfortunately this example will be useful if the data is constant while the data in my database is variable
Mada... My friend... um.... you dont use th.... uh... /sigh
Ok... Don't include this in your final code...
Code:
With DT
.Columns.Add("ID", GetType(System.Int32))
.Columns("ID").AutoIncrement = True
.Columns.Add("Marque", GetType(System.String))
.Columns.Add("Type", GetType(System.String))
End With
DT.Rows.Add(Nothing, "Domain", "Solde")
DT.Rows.Add(Nothing, "Domain", "Pen")
DT.Rows.Add(Nothing, "Session", "Processor")
DT.Rows.Add(Nothing, "Session", "Phone")
instead use YOUR datatable that is filled with all YOURdata, and doesnt have to be distinct as the rest of the example crunches it out..
If youll excuse me, im going to go bang my head against the wall...
-
Oct 8th, 2018, 12:33 PM
#9
Thread Starter
Hyperactive Member
Re: Fill Combobox
Hiii Wes4dbt ..
This is all my code in my Form1
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\COMBO_DB.accdb"
Private Sub Fill_Combobox()
Dim sqlstr As String
Dim InfoAdapter As OleDbDataAdapter
Dim InfoTable As DataSet
ComboBox1.Items.Clear()
sqlstr = "SELECT DISTINCT [MARQUE] FROM [FACTORY]"
InfoAdapter = New OleDbDataAdapter(sqlstr, constring)
InfoTable = New DataSet
InfoTable.Clear()
InfoAdapter.Fill(InfoTable, "[FACTORY]")
For Each rw As DataRow In InfoTable.Tables("[FACTORY]").Rows
ComboBox1.Items.Add(rw(0).ToString())
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Fill_Combobox()
End Sub
Private Sub Cascade_ComboBox()
Dim sqlstr As String
Dim InfoAdapter As OleDbDataAdapter
Dim InfoTable As DataSet
ComboBox2.Items.Clear()
sqlstr = "SELECT [TYPE] FROM [FACTORY] WHERE [MARQUE] = '" & ComboBox1.SelectedItem & "'"
InfoAdapter = New OleDbDataAdapter(sqlstr, constring)
InfoTable = New DataSet
InfoTable.Clear()
InfoAdapter.Fill(InfoTable, "[FACTORY]")
For Each rw As DataRow In InfoTable.Tables("[FACTORY]").Rows
ComboBox2.Items.Add(rw(0).ToString())
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.Items.Clear()
Call Cascade_ComboBox()
End Sub
End Class
Thank you in advance for help me or please or if you can see my code if there are faults
Cordially
MADA
-
Oct 8th, 2018, 12:42 PM
#10
Re: Fill Combobox
MADA, we have been down this road before where I thought you were situated on when and where to use a dataset vs datatable. This is like teaching you how to fish, giving you a fish, then eating the fish for you
-
Oct 8th, 2018, 12:49 PM
#11
Thread Starter
Hyperactive Member
Re: Fill Combobox
Thank you kpmc but really in my first post I wrote that the data are variable .. thank you very much master
-
Oct 8th, 2018, 12:50 PM
#12
Re: Fill Combobox
Originally Posted by MADA BLACK
Thank you kpmc but really in my first post I wrote that the data are variable .. thank you very much master
I give up. Have a happy day
-
Oct 8th, 2018, 12:58 PM
#13
Re: Fill Combobox
If you want to append items to the combobox list then you need to stop doing this "ComboBox2.Items.Clear()" .
btw - the code kpmc posted was just an example. The data was was for demo purposes. You were meant to replace the data section with your own data.
-
Oct 8th, 2018, 01:24 PM
#14
Thread Starter
Hyperactive Member
Re: Fill Combobox
Thank you a lot kpmc .. happy and nice day .. now it is 08 : 25 of night at home
Thank you very much wes4dbt
Cordially
MADA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|