+ Reply to Thread
Results 1 to 7 of 7

Can I populate a ComboBox from a SQL query?

  1. #1
    Registered User
    Join Date
    11-27-2017
    Location
    Surrey, England
    MS-Off Ver
    Office 365
    Posts
    65

    Can I populate a ComboBox from a SQL query?

    Hi all,

    I am currently pulling some simple SQL code into the worksheet, (it is a select top 10 productnames from one of our company tables). It is working perfectly, but I would like to be able to populate my ComboBox1 with the result.

    I've been reading lots of tutorials and trying examples but I can't seem to get it to work for my example. I can't post the exact code as I am not sure I am allowed to give the server/database details, so I have just starred out where I have details.

    Please Login or Register  to view this content.
    The initial sub produces the top 10 in sheet2, but I can't seem to get it to populate the combobox. The examples online seem to all work for others but I can't get the same result

    Is someone able to advise? Thanks in advance if so!

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Can I populate a ComboBox from a SQL query?

    Haven't tested this, but something like this

    On a regular code module -

    Please Login or Register  to view this content.
    While Initializing the Userform

    Please Login or Register  to view this content.
    Let me know if you run into any issue.
    Last edited by NeedForExcel; 12-17-2019 at 01:04 AM.
    Cheers!
    Deep Dave

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,464

    Re: Can I populate a ComboBox from a SQL query?

    Try change to
    Please Login or Register  to view this content.

  4. #4
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Can I populate a ComboBox from a SQL query?

    Thats a great code Jindon..

    Always learn so much from you.

    I was trying a similar code with List & Listrows Methods, instead on Column

    I have one doubt though. How will the Procedure initialize the UF?

    Will it maybe need something like a UserForm1.Show line to make the UF show?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,464

    Re: Can I populate a ComboBox from a SQL query?

    It should be called from Initialize event, or OP should know how to use it somehow...

  6. #6
    Registered User
    Join Date
    11-27-2017
    Location
    Surrey, England
    MS-Off Ver
    Office 365
    Posts
    65

    Re: Can I populate a ComboBox from a SQL query?

    Thanks both so much! Apologies for the late reply, something came up and I wasn't allowed to
    Jindon, your code seems like it wants to work.. but I am getting the error 70 - Permission Denied. Sounds like maybe it is restricted due to my companies SQL server? Or could it be something else...
    NeedforExcel.. yours seems to work without error but the combobox remains empty so am confused.

    The other thing I was thinking.. is because I have managed to get the query to successfully print into the sheet - could I instead write something which just fills the combobox with the rows after refreshing them? Would that be easier or would that start to delay the document (picture that it could end up with thousands and thousands of rows)?

  7. #7
    Registered User
    Join Date
    11-27-2017
    Location
    Surrey, England
    MS-Off Ver
    Office 365
    Posts
    65

    Re: Can I populate a ComboBox from a SQL query?

    Thanks both so much! Apologies for the late reply, something came up and I wasn't allowed to
    Jindon, your code seems like it wants to work.. but I am getting the error 70 - Permission Denied. Sounds like maybe it is restricted due to my companies SQL server? Or could it be something else...
    NeedforExcel.. yours seems to work without error but the combobox remains empty so am confused.

    The other thing I was thinking.. is because I have managed to get the query to successfully print into the sheet - could I instead write something which just fills the combobox with the rows after refreshing them? Would that be easier or would that start to delay the document (picture that it could end up with thousands and thousands of rows)?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] ComboBox to populate Listbox and 2nd Combobox based on 1st combobox dropdown choice
    By bbqqsmokeman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2017, 08:21 PM
  2. [SOLVED] Code required to Auto Populate 3rd Combobox based on 02nd Combobox Selection
    By bimmy80 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-14-2015, 03:56 AM
  3. Populate A Form Combobox With A SQL Query?
    By accell in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-09-2015, 03:28 PM
  4. Userform Combobox to populate form: Can I change the order of the columns in the combobox?
    By CraigWiggins in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2014, 12:14 PM
  5. [SOLVED] Excel Userform: Populate other controls (i.e. textbox & combobox) based on combobox select
    By MileHigh_PhD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 04:50 PM
  6. Populate combobox from ODBC sql query?
    By kuraitori in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2010, 06:40 PM
  7. Replies: 2
    Last Post: 09-14-2009, 08:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1