Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo boxes again ;)

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
0
0
AT
this is how the combo is loaded

Do While Not DBRec.EOF
cmbTeacher.AddItem DBRec!Teacher_Name
cmbTeacher.ItemData(cmbTeacher.NewIndex) = DBRec!TeacherID
DBRec.MoveNext
Loop

lets say i am adding
Smith, 45
Jones, 46

later on i need to set the value of the combo box based on the Index that I have.

So if I had the number 45 how would i set the combos value to Smith?

cmbTeacher.ListIndex = 45 doesnt work

thanks ;)
 
Hi,

Private Sub SelectItemByItemData(ByVal lItemData As Long)
Dim lIndex As Long
Dim bFound As Boolean

bFound = False
For lIndex = 0 To Combo1.ListCount - 1
bFound = (Combo1.ItemData(lIndex) = lItemData)
If bFound Then
Exit For
End If
Next lIndex

If bFound Then
Combo1.ListIndex = lIndex
Else
Combo1.ListIndex = -1
End If
End Sub
 
thanks LJR but Combo1.ListIndex = Index doesnt set the value of the combo based on the index i put into it

Lets say I have a table with 2 fields, Colors and ID, the values are Red, Green, Blue and the IDs are 3, 4, 5

To fill the combo I would do

Do While Not Rec.EOF
combo.AddItem Rec!Color
combo.ItemData(combo.NewIndex) = Rec!ColorID
Rec.MoveNext
Loop

lets say I had 3 as an ID
using Combo1.ListIndex = 3
it would set the value of the combo to Blue since it counts from the top, 1 being red, 2 green, 3 blue. When 3 should be red

I tried using
combo.ItemData(combo.ListIndex) = ID
but it does nothing, doesnt crash but also doesnt do anything
 
Better check my code again. It does *not* say:
Combo1.ListIndex = Index
but it says:
Combo1.ListIndex = lIndex

lIndex (mind the letter 'l' in front of it) lIndex is the index in the combobox found by the for-loop of the Itemdata that is passed to the function.

cheers
 
oops sorry :) is that that the letter i or | ?
 
Hmmm,

This is supposed to be Hungarion notation so it is the lowercase letter L, which identifies the variable as a Long.

Best to copy the code from the message and paste it into VB(that's what I did to post the message).

So in order to select your Smith guy call:

SelectItemByItemData 45

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top