Solved

ItemData ptoperty in a ComboBox

Posted on 2002-06-04
9
625 Views
Last Modified: 2010-05-18
How can I select an item in a combobox using itemdata property ? (VB6, windows common controls 6.0)
0
Comment
Question by:computech1
  • 4
  • 4
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053406
You can do so only indirectly by using a loop:
dim l as long
for l = 0 to cboItems.ListCount-1
  if cboItems.ItemData(l) = yourvalue then
    cboItems.ListIndex = l
    l = cboItems.Listcount
  endif
next

CHeers
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7053515
hi computech1,

was wondering why do you want to select an item from a combobox thru Item data property. Just choose it directly from the drop down. OR are you looking for something else, maybe EE can help ..

-priya

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053522
priya_pbk, this is typically the case when you display the names, but keep the ID values in the ITEMDATA property. This is often used for database-related operations, where the names are there for user-readable information, while the ID is short (numeric) in the database for space saving issues...

CHeers
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:priya_pbk
ID: 7053541
>>while the ID is short (numeric) in the database for space saving issues...

ok, still wondering..if the id(just assuming its a primary-key) is linked to the Itemdata of the combobox, so that the list contains readable information(assuming some text), does'nt the database already contain the id , so what is the space saving issue here???
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053567
Say you have 2 tables: EMPLOYEE and DEPARTMENT
in the form where you edit the employee, you load the combobox with all the DEPARTMENTS, displaying the d-name, and storing the d-id in the itemdata.
The employee will go into 1 department, thus the editing user will choose one. This d-id will be stored in the database, instead of the d-name, as this one could change.
When reopening the employee, the combo of the departments being loaded, you have only the d-id from the employee record, so you need to locate that one in the list, selecting it, and by this showing the correct d-name to the user.
Cheers
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7053586
ok, is'nt this simlar to the Boundtext in the datacomboBox??? I use the datacombo often, so was wondering if the normal combo provides something more which I might have missed. If it is so, I got it!!

thanks

-priya
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053591
the datacombo does the things "automatically", and can be used if all the controls are data bound.
I don't use (and don't recommend) bound controls for production software, because this takes too much control from the developer. For application design, this is fine&cool, as there is only few code to write :-)

CHeers
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7053601
yup, got it. I use the datacombo, since the info/data entered by my clients are very critical, and can't effort to lose them. As they say, it depends on what the client wants and how effieciently one can deliver that with minimum usage of resources!
anyways, thanks for the update.
-priya
0
 
LVL 18

Accepted Solution

by:
mdougan earned 50 total points
ID: 7053742

Assuming that you have loaded the combo or listbox and put the key to the record in the item data, then I use a function like this to position a combo on the correct item when I display a particular record:

' to load the combo
While Not RS.EOF And Not RS.BOF
   cboSupervisor.AddItem RS("user_lastname").Value & ", " & RS("user_firstname").Value
   cboSupervisor.ItemData(cboSupervisor.NewIndex) = RS("user_no").Value
   RS.MoveNext
Wend


' to position the combo using the foreign key value
FindInData cboSupervisor, oRS("user_supervisor")


Private Sub FindInData(lst As Control, lData As Long)
Dim i As Long
    On Error GoTo ErrorRtn

    If lst.ListCount < 1 Then Exit Sub
       
    For i = 0 To lst.ListCount
        If i = lst.ListCount Then
            lst.ListIndex = -1
            Exit For
        End If
       
        If lst.ItemData(i) = lData Then
            If lst.ListIndex = i Then
                lst.ListIndex = -1
                lst.ListIndex = i
            Else
                lst.ListIndex = i
            End If
           
            Exit For
        End If
    Next i

ExitRtn:
    Exit Sub
   
ErrorRtn:
 ' whatever your error handler is
    GoTo ExitRtn
   
End Sub
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now