I am trying to go through a list of comboboxs and set the selected value from the itemdata.
So far I have this working for a single combo. I have a few combo's that I need to do this for and was trying to find a way to pass the combobox through to a sub to do this. I am not sure how this would work so any help is greatly appreciated

'No idea how to set the combo = control on form
Private Sub SelectCBO(ItemID As Long, combo As "comboboxControl")
    Dim i As Long
    combo.ListIndex = -1
    combo.Text = ""
    For i = 0 To combo.ListCount - 1
        If combo.ItemData(i) = ItemID Then
            combo.ListIndex = i
            combo.Text = combo.List(i)
            Exit For
        End If
End Sub

SelectCB0(.itemID, cboCombobox)
SelectCBO(.itemID2, cboCombobox2)

Edited by ProfessorPC: n/a

5 Years
Discussion Span
Last Post by ChrisPadgham

As an example, here's a function that you pass in the name of a text box and highlight the contents.

Public Function selectTextBox(Ctl As Control)
    Ctl.SelStart = 0
    Ctl.SelLength = Len(Ctl)

End Function

That should give you an idea how to pass in your combo box name.



Your Procedure declaration should be like this :

Private Sub SelectCBO(ItemID As Long, combo As ComboBox)

Rest of the code looks OK.
While calling the procedure call :
Call SelectCB0(100, cboCombobox)

Not sure what is .itemID ..?



The ItemID is coming from the saved combo.itemdata(combo.listindex) that is being saved in the database. I wish it was nice and easy and just the listindex but with what is required that is not an option. I am running into an issue when trying to use either method from above. They both result in the same error of "Type mismatch" at the call.

Private Sub SelectCBO(cbo As Combobox) 'or (cbo As Control)
    Dim i As Long
    cbo.ListIndex = -1
    cbo.Text = ""
    For i = 0 To cbo.ListCount - 1
        If cbo.ItemData(i) = m_nPharmType Then
            cbo.ListIndex = i
            cbo.Text = cbo.List(i)
            Exit For
        End If
End Sub

Private Sub LoadData()
     m_nPharmType = data.PharmType 'combo.itemdate(combo.listindex) saved previously
End Sub

Edited by ProfessorPC: n/a


The error means that the object you are passing to the subroutine is not a combo box or the subrountine declaration is not correct.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.