+ Reply to Thread
Results 1 to 26 of 26

VBA refreshing listbox in userform

  1. #1
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    VBA refreshing listbox in userform

    Hello all,

    I have a question regarding the refreshing of a listbox in vba. I cannot manage to find it online or fix it myself.

    The picture below shows my userform, and the listbox is connected to an access database via ADO.
    test.png

    How do I refresh the listbox after I add a new item or delete an item?

    Thanks in advance!

    Cheers,

    Devatu
    Last edited by devatu; 08-05-2015 at 03:44 AM. Reason: grammar

  2. #2
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA refreshing listbox in userform

    This should help you...

    http://p2p.wrox.com/access/1688-list...y-refresh.html
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA refreshing listbox in userform

    How are you populating the listbox?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Thanks for your quick reply!

    The given line on that website "me.Counties.Requery " gives an error. I've replaced Counties with the name of my access table, so the line atm is me.Table1.Requery.

    Here is my code for the add item to database button:

    Please Login or Register  to view this content.
    And here is the code for the making of the list:

    Please Login or Register  to view this content.
    Last edited by devatu; 08-05-2015 at 04:40 AM.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    Extract the making of the list into a sub like this:
    Please Login or Register  to view this content.
    Then:
    Please Login or Register  to view this content.
    And simply call refreshListbox after the insert.

    P.S I Fixed a few issues with your code

    A few pointers, as a rule don't do Select *, name your fields in the Query, selecting all the columns is slow - naming them makes the query faster and means that you can have the columns in any order you want, so you don't have to loop through the recordset:
    Please Login or Register  to view this content.
    Can then be replaced with:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-05-2015 at 05:07 AM.

  6. #6
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Thanks for your reply Kyle123, I will look into the meaning of the code in a second. At the moment your code does the trick indeed when I have added something. However, when I've deleted something from my listbox (and thus from my database), it still shows it in the listbox after I call refreshlistbox. Is there a way to do this for the delete function aswell? Thanks in advance!

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    There's no reason it should show when you've deleted something, please post your code for that, including the line where you call refreshListbox

  8. #8
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Here it is. The method I use is probably ugly in the eyes of a real VBA expert haha, sorry for that.

    Please Login or Register  to view this content.
    And thanks for the feedback btw. I really benefit from the feedback of experienced users

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA refreshing listbox in userform

    Are you calling the RefreshListbox sub after you've run the DELETE query?

  10. #10
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Dear Kyle, you are totally right. I just found out that I switched up rs(1) and rs(2). Don't really know how that happend, but because of that there was nothing deleted from the database, hence why there was no change in the listbox. Thanks for helping out!

  11. #11
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    At the moment, the refreshing function after the delete works for all records, except for the last remaining record. If i try to delete that one, it will remain visible in the listbox, but it is indeed deleted from the access file. Any ideas why?

    Please Login or Register  to view this content.
    Making of list:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Anyone ?

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    There doesn't seem to anything obviously wrong, please post your workbook

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    Please upload it here, many people can't access sharing sites from work

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    I'll have a look now

  16. #16
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    I don't know how

  17. #17
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Thanks Kyle !

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    The code in the workbook isn't the same as you have posted?

  19. #19
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Not anymore, I'm sorry for that. It is for my work so I had to proceed. But the deleting still closes the file / gives an error when I delete the last remaining record.

  20. #20
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    The code works fine for me (if I add in the refresh listbox call after deleting) - you also seem to have an empty record in the database - that's why you have a blank row.

    What error are you getting?

    To attach a workbook.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  21. #21
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    Quote Originally Posted by devatu View Post
    If i try to delete that one, it will remain visible in the listbox, but it is indeed deleted from the access file. Any ideas why?
    Quote Originally Posted by devatu View Post
    But the deleting still closes the file / gives an error when I delete the last remaining record.
    These seem different, which is it? What's actually happening?

  22. #22
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    You should also be deleting by the primary key of the table rather than the answer - deleting by a free text field is just asking for trouble - especially if someone enters the same answer multiple times

  23. #23
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Hmm.. I will try to run it again and look what happens. Then I'll get back to you.

  24. #24
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Okay so if i try to delete the empty row, it will give a Null error. (Or if I adjust the empty row in the access database manually, the same error happens). Any ideas?

  25. #25
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA refreshing listbox in userform

    Because you need to delete by id, your code should look like this:
    Please Login or Register  to view this content.
    The Delete routine:
    Please Login or Register  to view this content.
    Note that now Listbox1.List(selected,0) is the ID of the Record not the part. You'll need to adjust your other code to account for this

  26. #26
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: VBA refreshing listbox in userform

    Thanks for your reply, I will take a look at this now !

+ 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] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  2. [SOLVED] copy contents of userform listbox to another userform listbox
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2014, 11:39 AM
  3. Transfer Data From One Userform Listbox to Another Userform Listbox with 11 columns
    By sparkoft in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-18-2013, 10:54 AM
  4. userform listbox
    By tsiguy96 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-14-2013, 11:09 PM
  5. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  6. Listbox not refreshing on update
    By tdw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2006, 12:10 PM
  7. Refreshing a userform/listbox
    By toocold in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 04:10 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