?
Solved

ItemData ptoperty in a ComboBox

Posted on 2002-06-04
9
Medium Priority
?
636 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
[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
  • 4
  • 4
9 Comments
 
LVL 143

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 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 143

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 143

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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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