+ Reply to Thread
Results 1 to 33 of 33

Populate listbox based on a database search using a textbox

  1. #1
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Populate listbox based on a database search using a textbox

    Hello,

    I have searched on multiple forums to try to find an answer to this solution but cannot find anything that fits what I need.

    I have a database which gives the user a list of all stock we currently hold, and information about the stock (company, quantity, item etc)
    I already have a userform set up whereby the user can enter the Model Number & it will display the Item, Company & Quantity. This works by searching for the Model Number & then returning the row data associated with that model number into 3 text boxes. The user can also edit the quantity from this userform as well.

    What I need is an extension on this userform which allows for more than one item to be displayed with the same model number. As we can have the same model number on multiple rows with different costs.
    I know my current userform will not be sufficient for this, as my results are populated into text boxes. What I am thinking is that the user can enter the model number, hit search & then multiple results can be displayed in a listbox, and then somehow the user can select & then edit the quantity.

    I have little VBA knowledge, and what I have I have learned online & modified to suit what I need.

    Any help/advice would be brilliant.

    Thank you.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: Populate listbox based on a database search using a textbox

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.


    To add a file to a post

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Populate listbox based on a database search using a textbox

    In very general terms- making a few assumptions as to what you use currently- while we await a workbook, you would want
    Please Login or Register  to view this content.
    to populate the listbox. Then to populate your textboxes, something like
    Please Login or Register  to view this content.
    and reverse that logic to repopulate the listbox.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Hi Guys,

    I've attached an example of my workbook. I have also got an Add New Item form, but I've left that off as that's working fine.

    Thanks.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Sorry, I've never come across or used a recordset before. I'm unsure of how this would work.

    Thanks.

  6. #6
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by xlnitwit View Post
    In very general terms- making a few assumptions as to what you use currently- while we await a workbook, you would want
    Please Login or Register  to view this content.
    to populate the listbox. Then to populate your textboxes, something like
    Please Login or Register  to view this content.
    and reverse that logic to repopulate the listbox.
    Sorry, I've never come across or used a recordset before. I'm unsure of how this would work.

    Thanks.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Let's try this for starters.
    After search click in listbox to transfer selected item to textboxes
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Let's try this for starters.
    After search click in listbox to transfer selected item to textboxes
    Thank you, that works good. However, it does not bring up any results if I enter a model number which appears more than once in the list.

  9. #9
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by Dotreena View Post
    Thank you, that works good. However, it does not bring up any results if I enter a model number which appears more than once in the list.
    Sorry, I've figured it out. I was entering the model number lowercase & on the list it is uppercase. Is there any way to able to search either?

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Please Login or Register  to view this content.
    Brilliant, works perfectly!

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Glad to help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  13. #13
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    I'm having a small problem with the Edit function now, whereby if I select the 2nd option in the list of a model number that appears twice & try to edit the quantity it will edit the quantity for the 1st item in the list, even though the 2nd item for instance is selected & it is the 2nd items details populated in the textboxs on the userform.

    I've had a look at the code myself yesterday but can't seem to see why this is happening?

    Thanks.

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Try this one.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Try this one.
    When they appear in the listbox what is the final column that has numbers in, where is that information coming from? E.g. if i enter hlv5407 they have 3 and 6 in the final column?

    Thanks

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Because model numbers can occur more then once I've added the row numbers where they're located.
    Please Login or Register  to view this content.
    In turn these numbers are used to edit the correct selected item in ListBox.
    You can hide that column by setting columnwidths for all columns and setting lastcolmnwidth to zero.

  17. #17
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75
    Quote Originally Posted by bakerman2 View Post
    Because model numbers can occur more then once I've added the row numbers where they're located.
    Please Login or Register  to view this content.
    In turn these numbers are used to edit the correct selected item in ListBox.
    You can hide that column by setting columnwidths for all columns and setting lastcolmnwidth to zero.
    Ok great, I was trying to figure out how to add a column to my database you see and couldn’t get the code to work.

    Also do you know how I can get my msgbox to appear if they enter a model number that’s not found. My original if r is nothing then etc is not working,

    Thanks

  18. #18
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Ok, try next one.
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Ok, try next one.
    That's brilliant.

    And finally, can you talk me through if I needed to add a new column to my database as I can't figure out what to modify in your code to facilitate it.

    Thanks.

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    As long as you add coloumns after F column you don't have to change anything.
    Otherwise I've marked what you need to change in the code.
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    As long as you add coloumns after F column you don't have to change anything.
    Otherwise I've marked what you need to change in the code.
    You are a true star. Thank you for all your help on this. I'll mark it as solved now.

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Glad to help.

  23. #23
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Glad to help.
    Sorry to re open this,

    I've just come to add a column in, and when doing so it then doesn't change the quantity of the item when i try to adjust it.

    Thanks

  24. #24
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Post example file with column added.

  25. #25
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Post example file with column added.
    Attached.

    Thanks
    Attached Files Attached Files

  26. #26
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    Brilliant, that's worked.

    Finally, how do I alter to search for a different column rather than column D which is model number. I'm trying to get it to pick up the 'Item' instead which is column B.

    I've changed

    Please Login or Register  to view this content.
    To

    Please Login or Register  to view this content.
    No luck?

    Thanks.

  28. #28
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Populate listbox based on a database search using a textbox

    Please Login or Register  to view this content.
    Changing to 2 was good 1st step red part had also to be changed to 1

  29. #29
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Quote Originally Posted by bakerman2 View Post
    Please Login or Register  to view this content.
    Changing to 2 was good 1st step red part had also to be changed to 1
    Ah! Brilliant.

    Thank you again, really appreciate it.

  30. #30
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Do you know how to return a date in the listbox as UK format and not US?

  31. #31
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Populate listbox based on a database search using a textbox

    generally wrap returned value with format required
    Please Login or Register  to view this content.

  32. #32
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75
    Quote Originally Posted by nigelog View Post
    generally wrap returned value with format required
    Please Login or Register  to view this content.
    Sorry could you give some more detail as to where that would go?

  33. #33
    Registered User
    Join Date
    01-09-2018
    Location
    West Midlands, England
    MS-Off Ver
    2016
    Posts
    75

    Re: Populate listbox based on a database search using a textbox

    Any other ideas?

+ 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. Populate ComboBox from Database based on ListBox selection
    By kukreja.v in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2015, 09:53 AM
  2. [SOLVED] (ActiveX) Populate Listbox With Textbox
    By elsg in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-25-2015, 02:22 PM
  3. [SOLVED] VBA userform search for listbox entry based on Textbox value
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-12-2015, 08:29 AM
  4. VBA - Adding Search to Listbox based on Textbox value
    By nimesh29 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2014, 06:47 PM
  5. [SOLVED] Need to populate data in listbox based on textbox value change
    By Naveed Raza in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-20-2014, 07:10 AM
  6. [SOLVED] Populate a Listbox in a UserForm based on a Search Result
    By clapforthewolfman in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 07-17-2013, 07:53 PM
  7. [SOLVED] Populate Listbox from 2 columns based on search keyword from text box
    By sivakumar123 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2012, 12:22 AM

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