Results 1 to 6 of 6

Thread: Select ComboBox Item

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    46

    Select ComboBox Item

    I'm loading a combo box like this

    Code:
    Private Sub LoadProjectInfos()
        
        Set projectInfos = GetProjectInfosForChangeOrder()
    
        For Each projectInfo In projectInfos
    
            cboProject.AddItem CStr(projectInfo.ProjectID) & ";" & _
                               projectInfo.Project & ";" & _
                               projectInfo.Company & ";" & _
                               projectInfo.City
    
        Next
    
    End Sub
    I've tried a few different ways to select the combo box item using the project Id but I can't get it to work. Sometimes it errors, sometimes nothing happens.

    What's the right way to do this?

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,710

    Re: Select ComboBox Item

    cboProject.ListIndex = x
    where x is the 0-based index of the item you want selected

    You can't set it like that using the contents of the item. When you're adding the items to the list, check for the one you want selected then save the index for after the loop.
    Or, you can add a .index field, fill it in as you add them to the list, then search each member later when you want to set it.

    If the user is going to be modifying things, you might just have to do a search, but that's the least efficient way of doing things.

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Select ComboBox Item

    Quote Originally Posted by Zephaneas View Post
    I'm loading a combo box like this

    Code:
    Private Sub LoadProjectInfos()
        
        Set projectInfos = GetProjectInfosForChangeOrder()
    
        For Each projectInfo In projectInfos
    
            cboProject.AddItem CStr(projectInfo.ProjectID) & ";" & _
                               projectInfo.Project & ";" & _
                               projectInfo.Company & ";" & _
                               projectInfo.City
    
        Next
    
    End Sub
    I've tried a few different ways to select the combo box item using the project Id but I can't get it to work. Sometimes it errors, sometimes nothing happens.

    What's the right way to do this?
    Code:
    For Each projectInfo In projectInfos
    
        cboProject.AddItem...
        cboProject.ItemData (cboProject.NewIndex) = projectInfo.ProjectID
    Next
    And to select a item by Project ID:
    Code:
    Private Sub Command1_Click()
        Dim iPpprojectID as Long
    
        iProjectID = Clng(InputBox ("Enter the project ID:")
        If not SelectInComboByItemData(iProjectID) Then
            Msgbox "Not Found"
        End If
    End Sub
    
    Private Function SelectInComboByItemData(nCombo As ComboBox, nItemData As Long) As Boolean
        Dim c As Long
        
        SelectInComboByItemData = True
        For c = 0 To nCombo.ListCount - 1
            If nCombo.ItemData(c) = nItemData Then
                nCombo.ListIndex = c
                Exit Function
            End If
        Next c
        SelectInComboByItemData = False
    End Function

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Select ComboBox Item

    The string being added looks like MS Access combobox. Is that the case?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    46

    Re: Select ComboBox Item

    OK, so I tried what you gave me
    Code:
    LoadPhases
    For C = 0 To cboPhases.ListCount - 1
        If cboPhases.ItemData(C) = selectedChangeOrder.JobID Then
            cboPhases.ListIndex = C
            Exit For
        End If
    Next
    On the "cboPhases.ListIndex = C" line I get the error "You've used the ListIndex property incorrectly".

    I tried this

    Code:
    cboPhases.selected(C) = True

    and it runs, but the item is not selected.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Select ComboBox Item

    To "select" that item in the combobox, use:

    Code:
    cboPhases.SelLength = Len(cboPhases.Text)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width