Select Distinct To Populate Combo Box

  • Hi all,
    I want to populate 2 combo box's with SQL query'd values, so i do it like so



    BUT...........


    Even though this works a treat, the combo box display all the duplicates aswell.


    e.g. [DEPOT] D,D,D,D,HHI,HHU,HHH,HHH,HHH,I,I,E,R,R,E
    I want
    e.g. [DEPOT] D,HHI,HHU,HHH,I,E,R


    e.g [STOCK] sdf6546,asd456,zxc123,lpo456,sdf6546,asd456,zxc123,lpo456
    I Want
    e.g [STOCK] sdf6546,asd456,zxc123,lpo456


    I've tried putting the DISTINCT after the SELECT but this wont work because i need to DISTINCT it AFTER i've query'd the recordset, otjherwise i wont get the results from the SQL staement i need.


    Does anyone know how to re-order this or add something to get the results


    Thanks
    Error#9

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Hi,


    you can try to loop through the combobox to check if the stock is already added. If it's in the combobox => don't add otherwise add.

  • Re: Select Distinct To Populate Combo Box


    Sorry i dont know what you mean?
    I already loop through the recordset to retrieve the data, so wont that fall over on the first loop?

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    You have to loop through the combobox:



    Hope this gives you an idea.

  • Re: Select Distinct To Populate Combo Box


    Sorry, i really dont understand.
    DEPOT is a 3 Character field so i dont so how looping and comparing to the counter number is going to dedupe the combo boxs.


    my appolgies on being thick if its just staring me in the face

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Ok, I'll try to explain a little more. The example I provide is only for 1 combobox, but you can use this for the other as well.


    The global thing that I do is, before I add a new element in the combobox I check if the item I want to add already exists in the combobox. If the item is founded in the combobox I don't add the item. If the Item isn't found in the combobox I add the item.




    I hope this helps you to understand what the code actually does: checking before adding the item, if the item is in the combobox.

  • Re: Select Distinct To Populate Combo Box


    Ahhhhhhhhh :)


    Thank you vmuch, it works a treat, how to i get it to loop through both combo boxs, do i need to create another sub routine?

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Yes,


    you can do just the same for the other combobox. Just add another loop and do the same.

  • Re: Select Distinct To Populate Combo Box


    Ah good skills worked a Gem!


    Another things though, is it now possible that when they select a DEPOT can it you populate the other combo box's values by the filter of DEPOT?

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Yes this is possible. Just use the same code as you use to populate the comboboxes. Only you populate 1 combobox an d you have have to se the filter in your sql-statement, something like this:


    Quote

    SELECT [DEPOT],[STOCK],[COMPLETED],[TIME_FLAG]
    FROM tblBreaks WHERE (DEPOT='" & txtDepotSearch.text & "'
    [ALLOCATED_TO] = '" & sBreakName$ & sSelect

  • Re: Select Distinct To Populate Combo Box


    Hi,
    The syntax is correct but it gets lots in an infinate loop


    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    I dont know if this makes a differnece but STOCK is a Number and Text field
    it has numbers like


    BFD34342
    73844312
    99454201
    BSA93499

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Now you can use distinct and you don't have to use the loop I've provided.

    Code
    rsComb.Open "SELECT [COLOR="Red"]DISTINCT[/COLOR] [DEPOT],[ .......
    Do While Not rsComb.EOF 
            frmAgent.txtStockSearch.AddItem rsComb!STOCK 
            rstComb.movenext
    Loop
  • Re: Select Distinct To Populate Combo Box


    GOLLEM YOUR A TOP MAN!!!!!!!!!!!!
    NICE 1


    You dont mess about...... i like it


    Cheers

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    do you know how to export a spreadsheet from access using sql in excel?

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    I don't understand your question.


    Do you mean:
    - get data from an access table into excel
    - or get data from excel into access
    ...


    Can you be more specific?

  • Re: Select Distinct To Populate Combo Box


    Transfer data from an Access table into a new excel spreadsheet using Excel VBA via a button press.


    Somehow using ADO cause i want it run in the background.

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    Hi this can be a global procedure:



    This part is perhaps easier for you, instead of using:


    Use:


    Code
    Do While rstSQL.EOF = False
            ActiveSheet.Cells(lngRow, 1).Value = rstSQL.Fields!Name                
            ActiveSheet.Cells(lngRow, 2).Value = rstSQL.Fields!Des                
            ActiveSheet.Cells(lngRow, 3).Value = rstSQL.Fields!Test
            lngRow = lngRow + 1
            rstSQL.MoveNext
        Loop
  • Re: Select Distinct To Populate Combo Box


    Oh, i know this way, cool,done it before, but i'm gonna have to create a excel spreadsheet with the headers on it already.


    no worries, good skills!


    Cheers

    [FONT="Verdana"]Any help appreciated
    Thank you,[/FONT]
    [COLOR="RoyalBlue"][FONT="Arial Black"]
    Error#9[/FONT][/COLOR]

  • Re: Select Distinct To Populate Combo Box


    To get the name of a field(col-header):


    Code
    rstsql.fields("test").name


    This should help you to get the col-names.


    To see how you can add sheets, workbooks, ... you can take a look at the macro recorder. Record a macro and see what code is used.


    Hope this helps.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!