VB and VBA Users Source Code: Use a VB listbox's ItemData property to store ID numbers
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Use a VB listbox's ItemData property to store ID numbers
By:
James Conn
Email (spam proof):
Email the originator of this post
Date:
Friday, October 06, 2000
Hits:
1250
Category:
Windows Forms/GUI/Controls/Graphics
Article:
Often, when you fill a listbox with data from a database, you want the control to display descriptive values; but you may also need it to store key values as well. For example, if you fill a listbox with employee names, no doubt you'll also want to store each employee 's ID number. Fortunately, if you use numbers as an ID, Visual Basic makes it easy to so with listbox's ItemData property. This property lets you store an additional number for each item in the list. To see how this works, add a listbox to a standard form and attach the following code. When you run the project, the listbox displays only the employee names. Click an item to see a message box that shows both the selected employee name as well as the stored ID number. Private Sub Form_Load() 'Fill List1 and ItemData array with 'corresponding items in sorted order. With List1 .AddItem "Mallik Murthy" .ItemData(.NewIndex) = 42310 .AddItem "Chien Lieu" .ItemData(.NewIndex) = 52855 .AddItem "Mauro Sorrento" .ItemData(.NewIndex) = 64932 .AddItem "Cynthia Bennet" .ItemData(.NewIndex) = 39227 End With End Sub Private Sub List1_Click() With List1 'Append the employee number and the employee name. MsgBox .ItemData(.ListIndex) & " " & .List(.ListIndex), vbInformation End With End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder