Results 1 to 11 of 11

Thread: Combobox source data

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Combobox source data

    I have an access database that I am using in a visual basic shell. I have several forms with comboboxes that use lookup tables as their data source. My problem is this, I know that you can input your value instead of selecting from the combo list. However, I could swear that I had some setup a couple of years ago such that the user input would be added to the combo list. I have researched until my eyes hurt and messed with my combo boxes so much that I can hardly recognize them anymore and am still unable to find out how I did this in the past (perhaps I am just imagining that I did that). If anyone is familiar with this property I would sure appreciate your response.

    By the way, anyone using comboboxes and lookup tables for their data source might consider putting all of their lookups in a single table and just use the field that is needed for a specific combobox. It really makes it easier to keep track of the lookup data. I had over 100 single field lookup tables and have reduced that to just 4.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Combobox source data

    I could see adding the typed in value to the lookup if the combobox wasn't bound to anything, but if it is bound, I don't see any way that it would work. After all, if it is bound, then the objects in the Items collection are DataRowView objects. You'd be adding something totally different if you just added a string. The only way I could see doing it would be to add the string to the datatable that the CB was bound to, but that's not quite what you described.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox source data

    I could swear that I had this setup a couple of years ago in some application I was working on, but cannot find it at all. It actually could have been when I was using VBA for the database applications (didn't use it long cause I didn't like it much). But yeah, what I have now is an administrator form where the lookup table(s) can be updated. Another thing I have done is to have a button next to the combobox such that the user can update the lookup table on the spot. But none of those options appeal to me if I can figure out how to do what I want. I wonder if it might be worthwhile for me to consider using an array or a list instead of lookup tables. Are those easily updated/modified by the user? Can the user easily edit an unbound collection?

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Combobox source data

    Maybe I'm misunderstanding but if you want the user to be able to update a bound combobox item list then you just update the underlying datatable, heres a simple example
    Code:
    Public Class Form7
    
        Private Sub CropsBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles CropsBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.CropsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.WaterDbDataSet)
    
        End Sub
    
        Private Sub Form7_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'WaterDbDataSet.Crops' table. You can move, or remove it, as needed.
            Me.CropsTableAdapter.Fill(Me.WaterDbDataSet.Crops)
    
            Me.ComboBox1.DataSource = Me.WaterDbDataSet.Crops
            Me.ComboBox1.DisplayMember = "crop"
    
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
            Dim row As WaterDbDataSet.CropsRow
            row = Me.WaterDbDataSet.Crops.NewCropsRow
            row("Crop") = Me.TextBox1.Text
            Me.WaterDbDataSet.Crops.Rows.Add(row)
    
        End Sub
    End Class

  5. #5
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Combobox source data

    when you add an item to your dataset rebind/refill your combo box to retrieve the latest values

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Combobox source data

    Quote Originally Posted by Leary222 View Post
    when you add an item to your dataset rebind/refill your combo box to retrieve the latest values
    thats not necessary. Sense the datatable is bound to the combobox, the changes to the datatable will show in the item list automatically. If you want to keep the changes, then you must call the the "Update Method" on the tableadapter before disposing of it.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Combobox source data

    Yes but that's not a clean way if doing it, data could be easily lost if the update method isn't called.

    I would have thought it would have made more logical sense to insert the record into the database then rebind the data source ? This is how I apply a similar thing I have in my Developments

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Combobox source data

    Quote Originally Posted by Leary222 View Post
    Yes but that's not a clean way if doing it, data could be easily lost if the update method isn't called.

    I would have thought it would have made more logical sense to insert the record into the database then rebind the data source ? This is how I apply a similar thing I have in my Developments
    No it's not cleaner that way. Your just bloating your code with unnecessary call to the database. But if you want to develop your programs that way that's fine with me.

  9. #9
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Combobox source data

    Are you saying their isn't a risk of data loss doing things this way ? And that it wouldn't cause sync issues with a multi user application?

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Combobox source data

    You just simply call the "Update" method after adding the row to the datatable. If some other user has modified the table then you would get a concurrency error and you would have to handle it. But this type of thing can happen even if you insert the new record using an insert command, then refill the datatable. If you do that and some other user tries to save there updates, then they will get a concurrency error. Refilling the datatable after calling the "Update" method serves no purpose. The datatable already has every thing in it.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox source data

    I think I figured out how to update the datasource table from one of the posts above. I think that all that would be necessary is to code, using the update method for the datasource table (not the receiving table), in a subroutine for when the user types in the data (that might be a dialog property that I should use). I am going to give it a try and see how that works. But it sure sounds good off the top of my head.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width