I have Access 2010 form which has a ComboBox cmbSubTopic which lists two columns (SubTopicID and SubTopic). The combo box is bound to a field containing SubTopicID. The SubTopicID column in the combo box is hidden, it only shows the SubTopic. When the user selects a SubTopic from the drop down the corresponding SubTopicID is stored in the table. I wrote some VBA code for the on load event of the form to look up the SubTopicID in the table and the corresponding SubTopic is selected in the ComboBox. My current code is something like this:
Set rsST = dbs.OpenRecordset(strSqlst)
For i = 0 To Me.cmbSubTopic.ListCount - 1
If Me.cmbSubTopic.Column(0, i) = rsST.Fields("SubTopicID").Value Then
Me.cmbSubTopic.SetFocus
Me.cmbSubTopic.Selected(i) = True
Exit For
End If
Next i
This gives the error saying:
The text you entered isn't an item in the list
I also tried using this:
Me.cmbSubTopic = Me.cmbSubTopic.Selected(i)
This selects the item in the ComboBox but it also writes the value of I in to the ID field of the table which I don't want.