Results 1 to 8 of 8

Thread: Combo box items clear problem in excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Combo box items clear problem in excel

    I am using the following code to populate a combo box placed on Sheet1 of an excel file to add items from Access database table named Company.
    Code:
    Private Sub cboCompany_DropButtonClick()
        Dim con As Object
        Dim rs As New ADODB.Recordset
        Set con = CreateObject("ADODB.Connection")
        con.Open = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & ActiveWorkbook.Path & "\ServiceTaxData.mdb"
        With rs
                .ActiveConnection = con
                .CursorLocation = adUseClient
                .CursorType = adOpenDynamic
                .LockType = adLockOptimistic
                .Source = "select * from company"
                .Open
        End With
        cboCompany.Clear
        If rs.RecordCount < 0 Then
        MsgBox "No company name found"
        Else
        For i = 1 To rs.RecordCount
        cboCompany.AddItem rs.Fields(1)
        rs.MoveNext
        Next i
        End If
    End Sub
    The problem is that it adds the items in the combo box every time the arrow button is clicked ie if there are forur items in the table, on first click it adds 4 items, on 2nd click it again adds four items results in 8 items.
    So I have added the line cbocompany.clear
    Now the problem is that it clears the combo box items as soon as an item is selected from the list.
    With debugging break mode, what I have seen that after adding the last item ie after Next I for last item it again moves to cbocompany.clear line.
    How can I fix this?

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Combo box items clear problem in excel

    Why are you adding the code to add data in
    Code:
    Private Sub cboCompany_DropButtonClick
    you should have it in form load event
    Edit: I believe its
    Private Sub UserForm_Activate()
    in VBA
    Last edited by VBFnewcomer; Feb 5th, 2008 at 07:59 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Combo box items clear problem in excel

    I am using the same in excel vba.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Combo box items clear problem in excel

    You didn't answer VBFnewcomer's question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Combo box items clear problem in excel

    Of which question? This one?
    I believe its in VBA
    Regarding this one:
    Why are you adding the code to add data in
    Code:
    Private Sub cboCompany_DropButtonClick
    Otherwise where should I add the code?

    Also I am not adding the data to a combo box on a User Form. I alreday mentioned in the first query that that the combo box is on sheet1 which I have added through Control Forms.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Combo box items clear problem in excel

    Otherwise where should I add the code?
    Code:
    Private Sub UserForm_Activate()
    combo box is on sheet1 which I have added through Control Forms.
    where is this control forms

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Combo box items clear problem in excel

    Sorry, the same should be read as Control Toolbox under View\Toolbars\Control Toolbox.

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Red face Re: Combo box items clear problem in excel

    Ok now I have found your combobox. Simply right click the sheet tab->view code. Now click object combo you might find in the code window select worksheet. Now click property combo you might find next to it and select Activate. Or you may simply copy
    Private Sub UserForm_Activate()

    End sub
    and paste it in the code window. Now add the code to populate the combo here.
    Last edited by VBFnewcomer; Feb 6th, 2008 at 02:34 AM.

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