Solved

listindex and itemdata

Posted on 2001-07-31
5
400 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now