+ Reply to Thread
Results 1 to 13 of 13

Use listbox selection and match function to populate controls on userform from dataset

  1. #1
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Use listbox selection and match function to populate controls on userform from dataset

    Hi everyone.
    I am back now with another request for help with my Update/Edit procedure.
    I am using a userform(frmSearch) to search for a record in the dataset (NumbersDB) to UPDATE the record for now. The user form has a Listbox with textbox and Combobox controls on it. This part is working correctly but it is not really what I want to achieve..

    I am looking for help with the following:

    1) When I make a selection (Dble_Click) in the Listbox I want to highlight the correct Row on the Sheet(NumberDB) AND populate the controls DIRECTLY FROM THE Sheet(NumberDB) and NOT from the Listbox. I know you must use the Match function for this but I do not know how to do this.
    2) I also want to show the selected Row number from the Sheet(NumberDB) in the txtMatchRowNo. Currently, I am showing the SNo in the Dataset and this will differ from the Row number in the Sheet as the dataset is starting on Row 6

    I am including my workbook for easy reference.
    Thanking you all in advance for helping me.
    Attached Files Attached Files
    Last edited by DawidV; 10-16-2021 at 08:35 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Use listbox selection and match function to populate controls on userform from dataset

    Why do you want to retrieve the values from the worksheet? That doesn't seem very efficient to me? It is better to use a table (listobject) to collect data. Why do you use an 'inputform' and a Userform. It is better to arrange everything via a Userform.
    Messages have been translated from Dutch to English by means of google translate.

  3. #3
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    Hi
    I want to Update the selected record with the UserForm.
    The reason I want to retrieve the data from the worksheet is that all the columns from the worksheet are not listed in the ListBox but I would like to update the controls on the userform
    The ListBox is ONLY to search for a record
    The InputForm is only to add the data to the dataset.
    The Edit Record, therefore, calls the Search UserForm for the Update. I will be using the same userform later to delete a record.
    That is why I need the Match Function to work with the correct record that was selected.
    Thanx

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: Use listbox selection and match function to populate controls on userform from dataset

    Very sound advice from @Vraag, make life easy for yourself, there is no need for complex search routines, there is a direct link between the listbox.listindex and the data row of a table - do not bother renaming textboxes etc., leave the default name - place in the same sequence as your table columns and with three or four lines of code you can add/edit/update/remove with ease.
    The link below is one of my recent contributions on a similar case.
    I have added a pdf with the code fully indented for ease of reading.
    Attached Files Attached Files
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  5. #5
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @torachan, how do you export the code to a .pdf so neatly?

  6. #6
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    Hi Torachan
    Thanx for the feedback and your files.
    I will have a look at them.

  7. #7
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @torachan, if I do not want to use a table but a dataset, how will the code then differ?

  8. #8
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @Vraag, using an addin named "VBAcodePRINT" I think it cost approx £20 as a download 6 -7 years ago.

  9. #9
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @DawidV, I was putting forth what in my opinion was best option, I would be doing you a disservice by assisting an inferior approach.

  10. #10
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @torachan, thanks for the reply.
    The dataset that I actually want to build this on has 48 columns(TextBox and Combobox) and I understand when the when you go above 10 columns in a Listbox it becomes problematic.
    I was trying to be selective with what Columns I place on the user form for the update so as to only place certain columns in the Dataset for Search.
    My idea then was to then only populate certain column data from the Dataset, eg only those columns that will need to be updated as it is not all the columns.
    Was trying to do it on a smaller scale with this one then to progress to the larger dataset.
    Hope I explain it better now.
    I understand your suggestion and I will adapt my project to that.

  11. #11
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: Use listbox selection and match function to populate controls on userform from dataset

    The column count of a listbox only becomes problematic if you use 'rowsource' or 'additem' methods to populate it - the 'additem' is a throwback to early VBA and as its name implies it is used to add item singular, and is very inefficient when handling mass data, the less read/write to the sheet you do the speedier your program becomes.
    e.g. remove all formula/calculations/formatting from the sheet - you do not want your sheet unnecessarily calculating at every cell data change.
    action calcs etc., at the point of data insertion will speed up your workings.
    I have attached a search demo that i put together some time ago - basically the same subject, a poster on this forum having difficulty with listbox columns, wanting to keep stock figures with over twenty plus stores - some of the search methods may assist you.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    @torachan, Thanx
    I will have a look at all the help you send me, really appreciate it.

  13. #13
    Registered User
    Join Date
    02-25-2017
    Location
    Bloemfontein South Africa
    MS-Off Ver
    2019
    Posts
    56

    Re: Use listbox selection and match function to populate controls on userform from dataset

    Thanks for everyone's help with my query.
    I will be able to resolve my problems with all your help.

+ 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] Populate controls based on selected multicolumn Listbox item
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-13-2017, 04:30 PM
  2. Replies: 0
    Last Post: 03-29-2017, 10:09 AM
  3. [SOLVED] Userform Multi-Select Listbox Selection Condition to unhide Frame and controls
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2016, 10:05 AM
  4. Populate (ca 200) Txtboxes depending on listbox selection, live-filter for listbox &1 more
    By InternInNeed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2016, 09:56 AM
  5. Replies: 1
    Last Post: 05-30-2015, 12:15 PM
  6. Populate a Userform from a selection on a popup search listbox
    By ahmadassaad in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-16-2012, 09:00 AM
  7. populate listbox based on selection in previous listbox
    By sarahng86 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2007, 10:55 PM

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