Solved

ItemData ptoperty in a ComboBox

Posted on 2002-06-04
9
619 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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

758 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

16 Experts available now in Live!

Get 1:1 Help Now