We help IT Professionals succeed at work.

selecting a value from combo box

renjith
renjith asked
on
Knowing only the Item Data for a combo box how can I make the corresponding value for that Item Data selected ?

regards
renjith
Comment
Watch Question

Commented:
You can get the index of the selected item with the
.ListIndex property.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
Besides the fact that i don't use the DataCombo (maybe wrong attitude :-) i suggest that you have a collection with the itemdata as key and the Listindex values as data. This will allow you to move very fast to the required item...

Otherwise you have too loop through the items and compare the ItemData values...

Cheers

Commented:
angelllll -
If you set the combobox style to 2 - dropdown list, then you can set the combobox text to the data and it will focus automatically (and quickly) on the correct row, and you will be able to get the correct listindex property.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
Of course, but as renjiht said, he has the ItemData value, which is typically the Key value... and not the text value...

CHeers

Author

Commented:
angell, i couldn't follow the first response you posted. This is the problem. Iam having member Information in  a table. The department etc are stored as ids(integers) in this table. So in the form when i choose a member i want the other details like department etc to be populated.
I have the department names in a combo box and ItemData is the department-ids. so depending upon the member name selected iam getting the corresp: dept-id from table. Now i need to make the dept name with that dept-id as the itemdata 'selected'. the dept names can be duplicates thatz why i want to do the selection based on dept-id.
i'll stop this short story.

regards
renjith



Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
If the ComboBox is bound to the Member Recordset (ie the field dept-id), this would be automatic.

Otherwise, looping example:

dim lngLoop as long
FOR lngLoop = 0 to cboDepartments.ListCount -1
  if cboDepartments.ItemData(lngLoop) = lngDepartmentID then
    cboDepartments.ListIndex = lngLoop
    goto ExitFound
  end if  
NEXT
ExitFound:


The first method requires that after you filled the Combobox, you build a mapping between the ItemData and the Listindex values:

-- This is at the top of the module
Private colMapping as Collection

-- this code needs to be called after the Departments Combo is populated
dim lngLoop as long
set colMapping = new collection
FOR lngLoop = 0 to cboDepartments.ListCount -1
  colmapping.Add lngLoop, "K" &  cboDepartments.ItemData(lngLoop)  
NEXT

-- This code is to select using the "ItemData":
cboDepartments.Listindex = colMapping("K" & lngDepartmentID)


Cheers






CERTIFIED EXPERT
Top Expert 2014

Commented:
I think the simplest solution would be to do a DepartmentName query, based on the Department ID (already known from the Person).
set rsDeptName = db.openrecordset("Select DeptName from Departments where DepartmentID =" & DeptID)

cboDepartment.Text = rsDeptName!DeptName

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
simplest <> most effective

Author

Commented:
That is simplest but there can be duplicate department names. After the values get populated iam again updating the table. So i want the right Ids :)

regards
renjith
CERTIFIED EXPERT
Top Expert 2014

Commented:
I would recommend filling the combobox controls with unique data (people and departments).

Hint: "Select Distinct ..." SQL statement

Explore More ContentExplore courses, solutions, and other research materials related to this topic.