Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Boxes and such 2

Status
Not open for further replies.

mlocurci

MIS
Oct 17, 2001
210
US
I have a ComboBox that is filled from a db via the AddItem method. I need for the Box to be filled as it is, but reference another value from the database that is part of the Record set. Basicly, I want to Show the text, but need the value called the "ContentID". How do I establish that? How do I address that value later in the code?
 
What I have done is to load the text and ID values from the database into an array. Then load the arrays text values into the conbo box. Then you can correlate the array index value with the combobox listindex value to easily find the correct ID value.

zemp
 
The array needs to be more than one dimension or a UDT array (which I prefer).

zemp
 
Or you can store a Long in the ComboBox.ItemData
[tt]
strSQL = "Select myName, myID from myTable"
Set rst = cn.Execute(strSQL)
With rst
Do While Not .EOF
myCount = myCount + 1
myCombo.AddItem .Fields(0)
myCombo.ItemData(myCount) = .Fields(1)
.MoveNext
Loop
End If
End With
[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Or you could use a collection if the keys are unique ...

Patrick
 
johnwm,

thanks for that valuable post, I was looking for a way to keep the employee ID and have the Employee name in the combo box, you saved me a bunch of extra coding. when all I wanted was the Employee ID.


I can't program for the Unknown, I have A keyboard not a Ouija Board. (See an online ouija board at George Oakes
Check out this awsome .Net Resource!
 
Hi,

Hope you can help.

Using code similar to that posted above I have populated a ComboBox with an ID and Description. However, I am struggling to get selection made stored. I would like to store the ID, not the description, but obviously see the description.

I figure this shouldn't be as hard as it is proving to be :(

I would love it if the ComboBox was able to display whatever is stored in the field the next time the form is opened as well, not just a blank field I get at the moment,but one step at a time :)

Any help would be appreciated.

Thanks.
 
Rechmali,

Please explaim what you mean by: "However, I am struggling to get selection made stored."

We are here to help.

Cheers.

"Life is full of learning, and then there is wisdom"
 
Hi Koala15,

What I mean is that I want the user to select an item in the combobox and store that selection in the combobox and subsequently the database.

I have 2 tables;

tblPersonDetails

Fields
ID
Name (text) 50
Consultant (int) 4



tblConsultant
ID (int) 4
Description (text) 50


ID Description
1 Mr Smith
2 Mr Jones
3 Mr Rowland

I read the records in to the combobox from tblConsultant, storing both the ID and the Description in the combobox.

The user just gets to see the names and if they click on Mr Jones I expect to be able to store '2' in the tblPersonDetails.

I would like the combobox to display Mr Jones, not only just after the user clicks his name, but when the form is loaded again.

Hope this is clearer.

Thanks for your reply.
 
I think what you need to do is to load the combo box with the consutant name (via .additem) and the ID in the corresponding .itemdata property. Then when you get the persondetails record you need to loop through the combo box items and find the .itemdata that matches the consutant field. Use that index to set the combo's .listindex property.

something similar to this might work.
Code:
dim i as Integer

for i=0 to combo1.listcount-1
  if combo1.itemdata(i)=<Consultant value> then
     combo1.listindex=i
     exit for
  end if
next i



zemp
 
Thank you guys for your help.

I got side tracked into doing something else, hence the late reply.

This did the trick nicely with a bit of lateral thinking :)

My next step is to get the combo box to recognise text and position itself in the list, like an Access combobox would do.

Once again, thanks.

Chris
 
Rechmali,

Try using the Click event of the combo box and the ListIndex property of the same combo box.

Here is an example from MSDN:

ListIndex Property Example
This example displays the names of three players in a ListBox control and the corresponding salary of the selected player in a Label control. To try this example, paste the code into the Declarations section of a form that contains a ComboBox control and a Label control, and then press F5 and choose a name from the ComboBox.

Dim Player(0 To 2) ' Dimension two arrays.
Dim Salary(0 To 2)
Private Sub Form_Load ()
Dim I ' Declare variable.
AutoSize = True
Player(0) = "Miggey McMoo" ' Enter data into arrays.
Player(1) = "Alf Hinshaw"
Player(2) = "Woofer Dean"
Salary(0) = "$234,500"
Salary(1) = "$158,900"
Salary(2) = "$1,030,500"
For I = 0 To 2 ' Add names to list.
Combo1.AddItem Player(I)
Next I
Combo1.ListIndex = 0 ' Display first item in list.
End Sub

Private Sub Combo1_Click ()
' Display corresponding salary for name.
Label1.Caption = Salary(Combo1.ListIndex)
End Sub



&quot;Life is full of learning, and then there is wisdom&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top