Listbox

benschol

Member
Joined
Jun 8, 2005
Messages
22
Programming Experience
Beginner
I am novice in Vb.Net and I need to create a ListBox or ComboBox for read two columns of a Access Database. Please, what is the code or what I can do?

Thanks,

Ben Scholer
 
Sorry. I need to display data (Id, Name) and then, when user click in a item, process with a SelectedIndex event using the ID field (first column). Thanks for your fast reply.

Ben
 
Thanks. Excellent! Only one thing: if I use a ListBox, I can have two columns, right? The exact case is: the project is a Recipes Catalogue. So, when the user click in a Recipe ID (the Name is only for information of which Recipe info load. This is the reason of two columns), a Datagrid show the Ingredients of this Recipe. Sorry if I did not explain well from the beginning.

Ben
 
Last edited:
Mr kulrom:

I think that I can solve my problem based on your code with this Sub

VB.NET:
Private Sub cboRec_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRec.SelectedIndexChanged
	Me.cboId.SelectedIndex = Me.cboRec.SelectedIndex
	Dim Flag As Integer = Me.cboId.SelectedItem
	dAd = New OleDbDataAdapter("Select * from Ingredients where ID = '" & Flag & "' order by Product", conString)
	ds = New DataSet
	dAd.Fill(dst, "Ingredients")
	Me.gridIng.CaptionText = "Ingredients of " & Me.cboRec.SelectedItem
	Me.gridIng.DataSource = ds
	Me.gridIng.DataMember = "Ingredients"
	Me.gridIng.Refresh()
End Sub

But when I try to run the Sub generate this error:

.....Type Mismatch in Criteria Expression.....

In the DB:
ID is defined like NUMBER (long Integer)

Please, what is the cause?

Note: In the properties of cboID I use Visible=False and Enabled= False. The user only see the cboRec (Recipe Name) but your code allows me to capture the ID to filter the data.

Thanks

Ben
 
As kulrom is currently offline I'll answer. When you include the field values explicitly in an SQL statement you only put single quotes around string values. If ID is a number you should remove the single quotes so the database doesn't think it is a string.
 
Thanks. The query now work fine but my code above don't work. The problem is: in the demo of Mr. kulrom, he use one table and I use two tables and two different items lists (One for the Recipes Names and one for the Ingredients ID). So, my code read the Index of the Combobox Items but NOT the ID of the table INGREDIENTS.

Really, my code is bad. How I can apply the idea of Mr. Kulrom to two tables? :confused:

Thanks again

Ben
 
Ha ha ha ... I think it was low punch:D Jim

Ben, As you'll see ListBox has so many limitations. If u want to have some thing with multi columns, the best way to go is with a listview control. Of course you can make (even it is really hard to be done) a multi columns ListBox but it will never be as good as Listview really is. So, Learn how to use a Listview. It is vastly simpler than trying to coordinate multiple columns of list or combo box, and it is intended to easily and cleanly display the kind of data you want to display.
Later, i'll be back with a demo (with listview) ... hey Jim what you think about idea? attachment! again! :p

Cheers ;)
 
Thanks Mr. Kulrom (and Mr. Jim too..) . Please, I wait for the demo. I'm, really, very, very novice with VB.NET and your help is gold to me :) A question: using ListView I can fill textboxes with Recipe Name, time, portions, Preparation, difficulty,etc,etc? In other words, the full info of the recipe.
 
According to your time, please don't forget the demo :) or at least, if you are busy, an little guide about how I can populate the listview with the Access DB data. (Most of the information that I have obtained, don't use databases and is very confused) Thanks...
 
Back
Top