Hello everyone,
This should be simple, at least it was simple when I was working with Access VBA...
I've got a listbox that is bound to an access 2007 table. This table contains 2 columns, ID and Name. I've set my listbox to show the Name field but I want to retrieve the ID when the user clicks on an item.
I've got two questions :
1) What is the best way to obtain the value of the selecteditem? Many ways that I thought would work ends up returning System.DataRowView or something similar
2) How do I get the ID field value instead of the name field value? Do I have to set the listbox as having multiple columns or something?
Here's my code for now, if it can help :
_________________________________________________________________________
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strFilter AsString
Dim i AsInteger
i = ListBox1.SelectedIndex
cmd = New OleDbCommand("SELECT ID_Unite_Construite FROM Unite_Construite WHERE ID_Unite_Construite IN (SELECT ID_Unite_construite FROM Decoupage_Construite WHERE ID_Unite_Decoupage IN (SELECT ID_Unite_Decoupage FROM Unite_de_decoupage_sur_Terrain WHERE ID_Terrain IN (SELECT ID_Terrain FROM Terrain_Propriete WHERE ID_Propriete = " + ListBox1.GetItemText(ListBox1.Items.Item(i)) + ")))", cn)
dr = cmd.ExecuteReader
strFilter = ""
While dr.Read()
strFilter = strFilter + "ID_Unite_Construite = " + dr(0) + " OR "
EndWhile
strFilter = Mid(strFilter, strFilter.Length - 4)
Me.Unite_construiteBindingSource.Filter = strFilter
__________________________________________________________________________
As you can see, the idea is to run a query based on the listbox value. This query gives me a number of value from other tables that will allow me to filter a bindingsource for my form.
Note that the code is far from finished, so don't look at it in too much details.
Thanks,
Ggilmann
This should be simple, at least it was simple when I was working with Access VBA...
I've got a listbox that is bound to an access 2007 table. This table contains 2 columns, ID and Name. I've set my listbox to show the Name field but I want to retrieve the ID when the user clicks on an item.
I've got two questions :
1) What is the best way to obtain the value of the selecteditem? Many ways that I thought would work ends up returning System.DataRowView or something similar
2) How do I get the ID field value instead of the name field value? Do I have to set the listbox as having multiple columns or something?
Here's my code for now, if it can help :
_________________________________________________________________________
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strFilter AsString
Dim i AsInteger
i = ListBox1.SelectedIndex
cmd = New OleDbCommand("SELECT ID_Unite_Construite FROM Unite_Construite WHERE ID_Unite_Construite IN (SELECT ID_Unite_construite FROM Decoupage_Construite WHERE ID_Unite_Decoupage IN (SELECT ID_Unite_Decoupage FROM Unite_de_decoupage_sur_Terrain WHERE ID_Terrain IN (SELECT ID_Terrain FROM Terrain_Propriete WHERE ID_Propriete = " + ListBox1.GetItemText(ListBox1.Items.Item(i)) + ")))", cn)
dr = cmd.ExecuteReader
strFilter = ""
While dr.Read()
strFilter = strFilter + "ID_Unite_Construite = " + dr(0) + " OR "
EndWhile
strFilter = Mid(strFilter, strFilter.Length - 4)
Me.Unite_construiteBindingSource.Filter = strFilter
__________________________________________________________________________
As you can see, the idea is to run a query based on the listbox value. This query gives me a number of value from other tables that will allow me to filter a bindingsource for my form.
Note that the code is far from finished, so don't look at it in too much details.
Thanks,
Ggilmann