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!

Make a Combo Box Selection with VBA

Status
Not open for further replies.

Silverbear

Programmer
Jan 4, 2003
2
US
I'm trying to make a selection in an Access 2002 combo box using VBA. I don't know how to set the pointer to a particular row without forcing the user to click on the control.

I have created the row source for the combo box with a sql query. That works well. If the row source query brings back two or more rows I set the focus to the control and allow the user to select.

If only one row is returned, I want to select that row with VBA so that I can bypass the field. How do I do this?

I've checked the ListIndex after the requery and it is -1. The returned row is in row 0 (zero). How do I select row 0 using code?
 
How about:
Code:
Me.cboCombo.Requery
If Me.cboCombo.ListCount = 1 Then
    Me.cboCombo= Me.cboCombo.ItemData(0)
End If
 
Remou,

Thanks very much for the code. I'll try it tomorrow at work. I finally worked out the syntax I needed to set the pointer today. I didn't think I could set the ListIndex since it is supposed to be read-only, but this works for my application:
Code:
If strRowCount = 1 Then
   cboUnit.SetFocus
   Forms("frmMyForm").Controls("cboUnit").ListIndex = 0
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top