Results 1 to 8 of 8

Thread: A ComboBox is KILLING ME!!! [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    A ComboBox is KILLING ME!!! [RESOLVED]

    I'm trying to populate my combobox from a dataset using the following code that I picked up from the forums thnx

    VB Code:
    1. Private Sub PopCBOFromDB(ByRef cbo As ComboBox)
    2.         Dim Conn As New OleDb.OleDbConnection(ConnectionString)
    3.         Dim Cmd As OleDb.OleDbCommand = Conn.CreateCommand
    4.         Cmd.CommandText = SQLString
    5.         Conn.Open()
    6.         Dim DA As New OleDb.OleDbDataAdapter
    7.         DA.SelectCommand = Cmd
    8.         DA.Fill(DS)
    9.         cbo.DataSource = DS.Tables("Table")
    10.         cbo.DisplayMember = Field
    11.     End Sub
    12.  
    13.     Private Sub cboBON_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBON.DropDown
    14.         PopCBOFromDB(cboBON)
    15.     End Sub

    The problem is that when I change anything to the actual database (like delete a record in the table I'm pulling my DisplayMember field from) it still shows that same record in the combobox... I'm having trouble going the other way too. e.g. when I add a record to the table it doesn't show the new record.

    However, if I close the form and open it again, it will show any changes that were made while it was open the last time. ????? I don't know ?????

    Can anyone of you friendly genious types help me?

    Thanks,

    Squirrelly1
    Last edited by squirrelly1; Nov 20th, 2004 at 11:50 PM.
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi
    Your forgot to clear the content the dataset before filling it

    VB Code:
    1. Private Sub PopCBOFromDB(ByRef cbo As ComboBox)
    2.         Dim Conn As New OleDb.OleDbConnection(ConnectionString)
    3.         Dim Cmd As OleDb.OleDbCommand = Conn.CreateCommand
    4.         Cmd.CommandText = SQLString
    5.         Conn.Open()
    6.         Dim DA As New OleDb.OleDbDataAdapter
    7.         DA.SelectCommand = Cmd
    8.         DS.Tables.Clear
    9.         DA.Fill(DS)
    10.         cbo.DataSource = nothing
    11.         cbo.DataSource = DS.Tables("Table")
    12.         cbo.DisplayMember = Field
    13.     End Sub

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    I tried that before and I just tried it again as you posted it. It's doing the same thing

    Any other ideas?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Hi squirrelly1,

    I think the problem is that the DropDown event is fired soon after the list portion of the combobox is shown.

    If the combobox must be refreshed each time the user click the arrow to drop-down the list portion, I think you should consider using the Enter event instead.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Good Suggestion.

    I tried that before too, believe it or not. And it didn't work then any more than it worked now... I even implimented the datatable clearing thinking it could have actually been two causes for one problem.

    Any other ideas?
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this now . The thing is you have refill the dataset again to get changes made to the source . You could do a boolean var , set it to true whenever changes made and call this sub if it's true.
    VB Code:
    1. Private Sub PopCBOFromDB(ByRef cbo As ComboBox)
    2.         Dim Conn As New OleDb.OleDbConnection(ConnectionString)
    3.         Dim Cmd As OleDb.OleDbCommand = Conn.CreateCommand
    4.         Cmd.CommandText = SQLString
    5.         Conn.Open()
    6.         Dim DA As New OleDb.OleDbDataAdapter
    7.         DA.SelectCommand = Cmd
    8.         DS.Tables.Clear
    9.         DA.Fill(DS)
    10.         cbo.Items.Clear()
    11.         cbo.DataSource = DS.Tables("Table")
    12.         cbo.DisplayMember = Field
    13.  
    14. DA.Dispose()
    15.  
    16.     End Sub

  7. #7
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    I think the problem is not with PopCBOFromDB. It is with the actual event that calls the method. It seems the method is called after the list is dropped.

    Why not call the PopCBOFromDB method just after the DB is updated.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Ok guys, thanks for the posts. I got it fixed.

    I just decided to declare my oledb database objects within my form and just use the same ones over and over when i was updating the comboboxes and when i'm deleting the records.. this way everything is on the same page.

    Thanks for your help guys,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

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