[AccessD] How to set the value, not the position, of a combobox

Bill Benson bensonforums at gmail.com
Wed Dec 24 17:34:40 CST 2014


Actually, It is not .List property (that is Excel), it is Column(iColIndex,iRowIndex)

This routine is working if I pass the combo's name, the value I am seeking in the required column, and the column index. Again, I just want to check there is no direct way, thanks.



Private Sub PositionCombo(Ctr As String, iCol As Long, varValue As Variant)
Dim Ctrl As ComboBox
Dim i As Long
Set Ctrl = Controls(Ctr)
Ctrl.Requery
For i = 0 To Ctrl.ListCount - 1
    If Ctr = "txtWineType" Then Debug.Print Ctrl.Column(iCol, i)
    If Ctrl.Column(iCol, i) = CStr(varValue) Then
        Ctrl.Value = i
        Exit For
    End If
Next
End Sub

-----Original Message-----
From: Bill Benson [mailto:bensonforums at gmail.com] 
Sent: Wednesday, December 24, 2014 6:23 PM
To: 'Access Developers discussion and problem solving'
Subject: How to set the value, not the position, of a combobox

I have an access form with an unbound combo that have been populated via rowsource query such as 

    Select vintageID, Vintage from TblVintage 

I only just noticed a problem with how my control values were being shown when I attempted to set the control to a particular value - thinking that if the bound column is 0, that this was the equivalent value to the combo itself. But apparently that is not so, what is returned (or set) with syntax like

      iVal = cboVintage  <<== returns the ListIndex

    cboVintage = iVal    <<== positions the combo on the ith item (assuming there is one)

I know I can loop through the combo's list, testing until cboVintage.List(i,0) = iVal = true, then set cbovintage.Selected(i) = TRUE. Perhaps this cannot be done because there is no guarantee that there is only one such value in any particular column of the ComboBox. But looping seems expensive, so is there any way to position the combo on the first qualifying item directly?

TIA.






More information about the AccessD mailing list