Solved

listindex and itemdata

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 100
Protecting vb6 & .Net code Obfuscation 18 181
VB 6 error 5 in windows 10 but not in XP 7 67
backup program with robocopy 6 69
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

685 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