Solved

ItemData ptoperty in a ComboBox

Posted on 2002-06-04
9
622 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

867 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

21 Experts available now in Live!

Get 1:1 Help Now