Consulting

Results 1 to 20 of 27

Thread: Solved: Populate a comboxbox and Listbox in a excel userform with an sql table data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Solved: Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi All,

    I have a excel userform which has some combo boxes and list boxes and I want to populate this boxes with the data which I have on my sql server database tables. Just to give an example. My first combo box is for region which needs to be populated from a sql table called Region_Mapping and my first list box should be linked to the same sql table but it should populate the countries on the basis of the region which user will select in the combo box. For eg : In my sql table I have following countries which are mapped against America
    1) Argentina
    2) Brazil
    3) Mexico
    4) Canada.
    Now if the user selects the region America in Combo box one then the List box one should get populated with the above mentioned countries with check boxes so that user can remove the unwanted countries while extracting data. Please expedite.

  2. #2

    Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi All,

    Please expedite.

  3. #3
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    What have you got so far?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4

    Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi,

    I have the below code so far. Following are things which I am looking for :

    1) While establishing connection to sql server database I want to enter User ID and password in my code.
    2) My Combobox gets populate with the data but it shows all instances of each region. For e.g. America is reflecting 4 times in my combo dropdown same in case of other regions. It should take only one instance of each region.
    3) As mentioned earlier I want to add a list box to my userform which will get populated on the basis of the value which i had selected in my combo box for example If I select America in my combo box my list box should get populated with the countries which are mapped against America in my sql server table and they should get populated with check boxes in listbox.

    Private Sub UserForm_Initialize()
    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stDB As String, stConn As String, stSQL As String
    Dim xlCalc As XlCalculation
    Dim vaData As Variant
    Dim k As Long
    Set cnt = New ADODB.Connection
    stConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=sap_data;Data Source=DB-77716EFB0313\SQLEXPRESS"
    cnt.ConnectionString = stConn
    'your  SQL statement
    stSQL = "SELECT Region FROM Region_Mapping"
    With cnt
        .CursorLocation = adUseClient 'Necesary for creating disconnected recordset.
        .Open stConn 'Open connection.
        'Instantiate the Recordsetobject and execute the SQL-state.
        Set rst = .Execute(stSQL)
    End With
    With rst
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
        k = .Fields.Count
        'Populate the  array with the whole recordset.
        vaData = .GetRows
    End With
    'Close the connection.
    cnt.Close
    'Manipulate the Combobox's  properties and show the form.
    With frmdata
        With .ComboBox1
            .Clear
            .BoundColumn = k
            .List = Application.Transpose(vaData)
            .ListIndex = -1
        End With
    End With
    'Release objects from memory.
    Set rst = Nothing
    Set cnt = Nothing
    End Sub
    Please expedite.
    Last edited by Aussiebear; 04-19-2023 at 02:03 PM. Reason: Adjusted the code tags

  5. #5

    Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi,

    Please expedite...

  6. #6

    Excel Combo box to be filled with sql table column data

    Hi All,

    I want to populate my excel userform combo box with one of the sql database table column unique values. I am using the following sql statement in my code but it shows me all values present in the sql table column for e.g. I can see America region name 4 times in the drop down I want that the combo box should show it only once. Following is the sql statement used by me in my macro code :

    stSQL = "SELECT Region FROM [Region_Mapping]"

    Please expedite. Thanks for your help in advance.
    Last edited by Aussiebear; 04-19-2023 at 02:04 PM. Reason: Adjusted the code tags

  7. #7
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You won't get help faster by posting the same question multiple times.

    Threads merged.

    Please be patient, someone will take a look at this when they are able.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    'your SQL statement
    stSQL = "SELECT DISTINCT Region FROM Region_Mapping"
    You'll have to copy this sub to the Combo1 exit event to populate the listbox without the Distinct filter for all the data.

    Hope this helps!
    Last edited by Aussiebear; 04-19-2023 at 02:05 PM. Reason: Adjusted the code tags

    David


  9. #9

    Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi Tinbendr,

    Can you please provide me an example for the post which you have made. I am unable to figure out what you are trying to say. Please..

  10. #10
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    From your first message...
    My first combo box is for region which needs to be populated from a sql table called Region_Mapping
    I assumed you want to search the database for UNIQUE list of Americas, yes?

    To get unique items from a Table, Use DISTINCT. This will get you the list without duplicates.

    and my first list box should be linked to the same sql table but it should populate the countries on the basis of the region which user will select in the combo box.
    Then, you use the value of the combobox to fill the listbox.

    So, Double click your combobox, then select the Exit event (upper right)
    Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    End Sub
    Inside this sub write another query to fill the listbox just like your example for the combobox.
    Last edited by Aussiebear; 04-19-2023 at 02:06 PM. Reason: Adjusted the code tags

    David


  11. #11

    Populate a comboxbox and Listbox in a excel userform with an sql table data

    Hi Tinbendr,

    I am facing the following error when i use the below sql statement in my macro.

    stSQL = "SELECT DISTINCT Region FROM Region_Mapping"
    Error Encountered : "Run-time error '-2147217900 (80040e14)':
    The text data type cannot be selected as DISTINCT because it is not comparable."
    Last edited by Aussiebear; 04-19-2023 at 02:06 PM. Reason: Adjusted the code tags

Posting Permissions

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