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

.AddItem Problem

Status
Not open for further replies.

llafretaw

MIS
Oct 2, 2001
64
GB
Hi,
I used to using MS Access comboboxes, where you can have 3 columns in the dropdown and hide the first column by setting its width to be zero.
However I now have a problem with a VB app which references data returned from a stor proc and uses the .AddItem method to populate the combobox
I cannot seem to get the combobox to be populated in the format:
key(and hide it as well), code and then new line/next record.
It keeps getting populated in the format:
key, new line, code, new line etc.
Any help would be appreciated
 
Store the key value in the .itemdata property.

Code:
Combo1.Additem "whatever"
Combo1.Itemdata(0) = <thekey>

The itemdata property is an array that contains one element for each item in the combo boxes .list property. You can access it using the .listindex property.

Code:
debug.print combo1.itemdata(combo1.listindex)


zemp
 
I notice that it's over a year since you last got an answer to your questions that you found valuable. It is worth your time to review faq222-2244 to find out how to ask clearer questions, and how to acknowledge answers.

For this question, the VB Combobox is a single column combo. You can use the Itemdata property of each added item to store a numeric value, so if your query brings back a recordnumber and a value:

With rst
Do While Not .EOF
myCount = myCount + 1
myCombo.AddItem .Fields(0)
myCombo.ItemData(myCount) = .Fields(1)
.MoveNext
Loop
End With

Alternatively you could reference the Microsoft Forms controls (FM20.DLL) which will give the same functionality as you used in VBA. Be aware however that you may not directly redistribute these controls (Google for FM20.DLL for details)

________________________________________________________________
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?'
 
Thanks for the pointer, realise now in hindsight, I frequently ask questions but seldom give feedback....

Anyway the code uses the itemdata method, but its when it tries to find a match for a non unique code selected from the dropdown that it always pulls back the first key associated with that code as opposed to the actual key associated with that particular instance of the code..
but cheers for your help
 
llafretaw,

I use this to select\pre-select an CBX|LBX item:

'Select New Category
Dim sNewCategory as String, i As Integer, iIndex As Integer
sNewCategory = "Apples"
For i = 0 To lbxCategories.ListCount - 1
lbxCategories.ListIndex = (i)
If lbxCategories.Text = sNewCategory Then
iIndex = lbxCategories.ListIndex
End If
Next i
lbxCategories.ListIndex = iIndex


Hope this helps,

Michael42
 
Thanks for all your assistance, its been appreciated.
Adrian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top