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

Combo Box bound column question

Status
Not open for further replies.

VBill57

Programmer
Oct 12, 2004
12
US
Hi,

I guess I've been using Access much more than VB lately. But now I'm trying to use a simple combo box in VB, and need some help.
All I want to do is to have a combo box to show a list of names, for example. If my table has 2 fields, EmployeeID and Employee, I want to show the employees in the drop-down combo box, but refer to the EmployeeID number (the bound field).
Also, I see the regular combo box, plus a DataCombo or a DBCombo. Can someone give me an idea of which to use? All I want is a very simple Access-like combo box:
for example, if the record has an EmployeeID of 99, and the Employee value = "Fred", then I want to be able to select "Fred" from the entire list of names. However I want to pass the ID value of 99 to another control or variable for use later, instead of the actual value "Fred".
I know this is easier than what I'm thinking.
Any help will be greatly appreciated. Thanks in advance!
 
I think you are looking for ItemData ;-)

Code:
Private Sub Combo1_Click()
  Text1 = Combo1.ItemData(Combo1.ListIndex)
End Sub

Private Sub Form_Load()
  With Combo1
  .Clear
  .AddItem "Fred"
    .ItemData(.ListCount - 1) = 99
  .AddItem "John"
    .ItemData(.ListCount - 1) = 98
  .AddItem "Dave"
    .ItemData(.ListCount - 1) = 97
  .AddItem "Larry"
    .ItemData(.ListCount - 1) = 96
  End With
End Sub

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
CubeE101,

Thanks for the ItemData tip. That will work, but it sure is easier in Access, when you can have 2 fields in a combobox, one that the user sees, and the bound hidden one that is the actual value you want to use.
There's no way to refer to a recordsource and field(s) without having to manually populate the combo box?
 
Something like

Dim conX as ADODB.Connection
Dim rstX as ADODB.Recordset

Set conX = New ADODB.Connection
conX.Open "Connection string"

Set rstX = New ADODB.Recordset
rstX.Open "SELECT Name, RefID FROM SomeTable ORDER BY Name;", conX, dbOpenTable, dbForwardOnly

If Not (rstX.BOF And rstX.EOF) Then
Do While Not rstx.EOF
With Combo1
.AddItem rstX("Name")
.ItemData(.NewIndex) = rstX("RefID")
End With
rstX.MoveNext
Loop
End If

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Thanks Andy,

I came up with an identical method using DAO, but I think it's time I start using ADO. Thanks for the example.

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top