Reputation: 381
I am trying to use Combo boxes in a visual studio 2015 (I think this means its VB6 code)
I am trying to build a list of values from a MySQL ADODB.recordset I have been able to insert them into the combo box.
However, I have no idea how to get or set the value in the Combobox.
so cmbGenre.setValue(new Object / id assoiated with a object)
or MsgBox("Current Select value in Combo box is = " & cmbGenre.selectObject)
The objGenre is just a class which has to variable ID & Name (this is all the columns in the MySQL table
Public Sub refreshCmbGenre(ByVal id As Long)
Call rsSetup(TABLE_GENRES)
cmbGenre.Items.Clear()
rsGenres.Sort = FIELD_NAME
rsGenres.MoveFirst()
Do Until rsGenres.EOF
cmbGenre.Items.Add(New objGenre(rsGenres.Fields(FIELD_ID).Value, rsGenres.Fields(FIELD_NAME).Value))
rsGenres.MoveNext()
Loop
'=====this is the problem code=========================
rsGenres.MoveFirst()
rsGenres.Find("[id]=" & id)
cmbGenre.SelectedText = rsGenres.Fields(FIELD_NAME).Value
'=============================
End Sub
Upvotes: 0
Views: 299
Reputation: 2750
If I understand what you are trying to do, it sounds like you are looking for this:
cmbGenre.SelectedIndex = cmbGenre.FindStringExact(rsGenres.Fields(FIELD_NAME).Value)
This will find the index of the combobox item that matches the field name, and then set the selection that item.
You can then get the values out using
cmbGenre.SelectedItem
This will return the object bound to the that item, containing both the ID and the FieldName.
This link should help with a better solution for working with comboboxes. It has a similar question in C# but is very easy to convert: https://stackoverflow.com/a/15983066/6144259
Upvotes: 1