Solved

listindex and itemdata

Posted on 2001-07-31
5
406 Views
Last Modified: 2008-03-06
I am loading a combobox using the following code

Do Until rsData.EOF
                             
cboFloor.AddItem rsData("DESC_Floor").Value & ""
cboFloor.ItemData(cboFloor.NewIndex) = rsData("PKID_Floor").Value & ""
rsData.MoveNext
                             
Loop

In this case it loads the floors of a building and the combobox would contain the following data:

ListIndex   List                 ItemData
0           First Floor          111
1           Second Floor         112
2           Third Floor          113

The combobox data loads without a problem.  However, in some cases I have a floor preselected.  In these cases I want the combobox to load completely and to have that floor selected by the program.  This is easily done with "cboFloor.ListIndex = mi_ListIndex" but the data I have is the ItemData.   How can I convert the ItemData number (which I have) to the corresponding ListIndex number (which I need to make this work).

Thanks
0
Comment
Question by:tlw2651
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:casassus
ID: 6338050
for i= 1 to cboFloor.ListIndex
  if cboFloor.itemdata(i) = NumBerIWantToFind then
    ' Make what you want
    Exit for
  end if
next
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6338060
Private Sub Combo1_Change()
  Combo1_Click
End Sub

Private Sub Combo1_Click()
  MsgBox Combo1.ItemData(Combo1.ListIndex)
End Sub
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6338214
To be more efficient, combine your loading loop with the selection loop proposed by casassus.

Zaphod.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6339988
Dim intFloorToSelect As Integer

Do Until rsData.EOF
                           
  cboFloor.AddItem rsData("DESC_Floor").Value & ""
  cboFloor.ItemData(cboFloor.NewIndex) = rsData("PKID_Floor").Value & ""
  If cboFloor.ItemData(cboFloor.NewIndex) = FloorToPreselect Then ' Test for matching floor
    intFloorToSelect = cboFloor.NewIndex
  End If
  rsData.MoveNext
                           
Loop

cboFloor.ListIndex = intFloorToSelect

If the desired floor is found in the itemdata property for each newly added row then this will be preselected when the combo is fully populated. If the matching floor is not found at all then the selected item will be index 0.
0
 

Author Comment

by:tlw2651
ID: 6341309
Had to change the flow a little, but that was it.  Excellent solution.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question