Refreshing thing in a combo box with thing from a database...

Hi, I'm pretty new to programming and I have a question about Visual Basic 2005 Express edition. I appreciate any input you can give me.

I'm writing a program that uses a Microsoft Access database, and I'm linking that database to a combobox. I can fill up the combo box with the contents of my "Name" column of the database, but I want the combobox to refresh either automatically (I guess through a timer) or through the click of a button, either is fine.

So... the users of my program should be able to access a dbgrid on another form (I have that set up too) and make changes to the database, and I want those changes in the "Name" column to appear in that combobox on the other form. Again, either through a timer or a command button.

Can any one help

Thanks!


Answer this question

Refreshing thing in a combo box with thing from a database...

  • Shehnaz

    Hi Techtro,

    if you're using typed datasets/datatables and tableadapters to bind to your combo box all you need to do is re-fill the datatable when you want the values in the ComboBox refreshed.

    E.g. if you're filling the table in your Form_Load event:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'NorthwindDataSet.Categories' table. You can move, or remove it, as needed.

    Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories)

    End Sub

    Then you can just re-do the fill when you want the refresh to happen:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories)

    End Sub

    Let me know if this doesn't help.

    Lorenzo Minore

    VB .Net Development


  • loris

    hi,

    click on the combobox, click on the little lighting bolt in the bottom-right corner

    These are the events of the combobox, now if i understand you correctly, you'll want to do the following:

    Put the following code in 'dropdownclosed'


    combobox1.items.clear
     


    then in 'dropdown' put the code in that you use to fill up the combobox,

    hope this helps :)



  • kled

    I got an error... Sad

    "Items collection cannot be modified when the DataSource property is set."

    I don't think that's it, I don't really want this to effect any event with the combo box. I want it to happen by clicking a button.

    I think that it's on the right track though, getting rid of all the values already inside it and filling it back up again I see what you meant.

    So, click command button, combo box deletes and replaces values...

    I hope I make sence

    Thanks, though! Big Smile


  • Bob Shaw

     

    If you want to periodically refresh the contents of this combobox - then you could place a timer on the form.   Set its timer interval to the duration that you want to check - so say every 15 minutes would set it to whatever 15 minutes is in milliseconds (15*1000*60) and start the timer.

    In the timer tick event you would need to code the following actions.    Reload the datasource from the database, reset this as the datasource property on the combobox.

    This could be manually initiated with the use of a refresh/Reload button on the form if you didn't want to do a timer refresh event.

    I would go with the refresh button first which simply will reload the datasource for the control - then if that wasn't enough and you wanted a refresh to occur every x number of minutes - implement a timer event.   

    You could also in you application set a flag variable accessible from both forms that would be set when a change has occured and read this value first to determine if there has been any changes and you need to refresh the combobox.    Once this is updated then you reset the flag variable to indicate you have just retrieved the latest details.

    Just some ideas ....


  • Sanghmitra

    It worked! Thank you!

    Sorry it took me so long to reply.

    Regarding the timer, I will work on that later on. The button will suffice for now, but the timer is something I definetely want to add. Thank you for that, too.

    Thanks again every one!


  • Refreshing thing in a combo box with thing from a database...