Solved

ItemData ptoperty in a ComboBox

Posted on 2002-06-04
9
631 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 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