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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lists and combos

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
GB
From a DB I get back the following data

Code Description
a1 Door
b5 Wheels
bn5 Plugs
zx5 Seat


What I want is to populate a combo box so that it displays the descriptions. However what I also want to is to find out the code when the user selects a description.

For example when the user selects Plugs from the combo box a message box is displayed showing "bn5"
 
I had a similar problem. Here is what I did:

I have a combobox - cmb1. What I do is grab the codes and descriptions from the database and put them together like this:
cmb1.additem code & " - " & description

This will put the codes into the combo box with their description. When a user decides to choose an item,you simply use this to determine the code:
dim temp() as string
temp = split(cmb1.text,"-")
'The code is in temp(0) and the description is in temp(1)

Hope this helps...
Troy Williams B.Eng.
fenris@hotmail.com

 
One way to do this is to use two arrays to hold your returned data from the database. When you populate the arrays from the database recordset you should be in a position where Code(i) and Description(i) correspond to how the data was stored in the database, that is Code(0) = a1 will mean Description(0) = Door.

Once you have this done, you can use the subscript value of the array to access the Description from the Code and vice versa.

Remember that the List property of a combo box works just like an array, so once you load the Descriptions into the combo box in the same order, the subscript of the combo box will be the same as the subscript of the Description/Code from the original arrays.

When a user selects, just check the selection against the List property and this should give you the subscript number you want.

This code would give you a message box with the Code assuming the Description selected in the combo box.

For i = 0 the (Form1.Combo1.ListCount-1)
If Form1.Combo1.Text = Form1.Combo1.List(i) Then
'Code here, the value of i is your
'subscript number
Exit For
'Stop the value of i incrementing
End If
Next i

MsgBox Code(i)

Hope this helps. (Better yet, hope this works!)

Brendan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top