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

Combo box question

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
AT
I am editing a form, in the form the combo is loaded with data from an access table. Then the combo is set to the value thats in the record. Using this code...

strYN = DBRec("Location")
For idx = 0 To cmbLocation.ListCount - 1
If cmbLocation.ItemData(idx) = strYN Then
cmbLocation.ListIndex = idx
Exit For
End If
Next idx

now later on i am trying to get the ItemData of the item in the combo

strLoc = cmbLocation.ItemData(cmbLocation.ListIndex)

now this should give me the ItemData of the selected item in the combo box, but it doesnt, it gives me the ItemData of the item thats loaded first in the combo box, but not the one selected. What can I do to get the ItemData of the selected Item?

Hope this makes sense, thanks for any help :)
 
If I understand you correctly you should be using this

strLoc = cmbLocation.Text

or did I miss something? If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
I am trying to get the ItemData that I loaded with the Items when I was loading the Combo

But even if I use strLoc = cmbLocation.Text to get the Text, I would get the first text loaded into the combo box and not the text that is selected.

 
The .ItemData property of a combo box is an array of type Long. It only then contains numbers.

The variable you're using to search is called strYN which implies it to be a string, therefore its possible that you're not finding a match.

You may want to consider searching the .List property array rather than .ItemData

strYN = DBRec("Location")
For idx = 0 To cmbLocation.ListCount - 1
If cmbLocation.List(idx) = strYN Then
cmbLocation.ListIndex = idx
Exit For
End If
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
ok...here is how the combo is loaded.

Do While Not DBRec.EOF
cmbRoom.AddItem DBRec("Name")
cmbRoom.ItemData(cmbRoom.NewIndex) = DBRec("RoomID")
DBRec.MoveNext
Loop

this loads the combo with the names and roomids

then based on the id i have, i need to set the value of the combo to the id that i have in access for that record.

If Not IsNull(DBRec("RoomID")) Then
strYN = DBRec("RoomID")
For idx = 0 To cmbRoom.ListCount - 1
If cmbRoom.ItemData(idx) = strYN Then
cmbRoom.ListIndex = idx
Exit For
End If
Next idx
End If

this sets the value of the combo to whatever value is in the access table.

Now later if I try to get the Text of the Combo, I dont get the Text of the selected Item, I get the Text of the Item that was loaded first into the Combo.

hope this helps, thanks
 
Hi,

Try this example: Modiy as needbe.

Private Sub cmbLocation_Click()
Dim strLoc As String

strLoc = cmbLocation.ItemData(cmbLocation.ListIndex)
Debug.Print strLoc
End Sub

Private Sub Form_Load()

Dim conn As New Connection
Dim rs As New Recordset
Dim strConnect As String

strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[Source];Persist Security Info=False"
strConnect = Replace(strConnect, "[Source]", "C:\db1.mdb")
conn.Open strConnect
rs.Open "SELECT * FROM tblLocation", conn, adOpenKeyset, adLockOptimistic

'Populate Combo/list box
cmbLocation.Clear
With rs
Do While Not .EOF
cmbLocation.AddItem .Fields("Location")
cmbLocation.ItemData(cmbLocation.NewIndex) = rs.Fields("LocationID")
.MoveNext
Loop
.Close
End With
Set rs = Nothing: conn.Close: Set conn = Nothing
End Sub

have a good one!
BK
 
How about this

If Not IsNull(DBRec("RoomID")) Then
strYN = DBRec("RoomID")
For idx = 0 To cmbRoom.ListCount - 1
If cmbRoom.ItemData(idx) = CLng(strYN) Then
cmbRoom.ListIndex = idx
Exit For
End If
Next idx
End If If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
How is strYN declared: - Is is a string, or a Long?
What type of field is "RoomID" - is it text or a number?

The ItemData can only hold numeric values - it is an array of Longs.

Assuming that RoomID is a number, you still may be comparing a string to a number, if strYN is a string (which is implied by its name).

You might also try:

If cmbRoom.ItemData(idx) = Val(strYN) Then Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top