Results 1 to 12 of 12

Thread: [RESOLVED] Combobox modification

  1. #1

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

    Resolved [RESOLVED] Combobox modification

    I am looking for a means to take a combobox and bind to a column other than the one displayed. i.e., I do not want a multi column combobox, unless there is one out there that I would not have to spend a large amount purchasing. The combobox is bound to a table that has two columns with values that I need. One column has the name, the other the ID. I need to display the name, but use the undisplayed ID.

    I have seen something about this before but have been unable to find it again.

    Can anyone steer me to how I can either get or make the control do what I want?

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

    Re: Combobox modification

    The datasource is the datatable. The selecteditem is going to be the row in the datatable, no matter what field you display, so you have everything that the table has. Therefore, I may be misunderstanding the question.
    My usual boring signature: Nothing

  3. #3

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

    Re: Combobox modification

    OK, here is what I am after. I have a table with a set of columns, two of which I need. One of the columns is an ID (incremental integer starting with 1), and the other is a name. By the way, after thinking about it, I really am not interested in a multi-column combobox.

    The combobox has the SelectedItem property set to the Name column. The value that I want is the ID from another column, which I do not want to display in the combobox. The combobox is populated as seen in the code below.

    Would my problem be resolved simply by setting the properties for DisplayMember and SelectedItem? That would be a lot easier than my, not so great, idea of setting up a keypress event that runs a query to extract the ID from the table using value of the SelectedItem. I am really hoping for something better than that.

    Code:
        Public Sub GetOwnerComboBox()
            MyError = "Manager query failed."
            cboOwner.Items.Clear()
            MasterBase.AddParam("@active", True)
            MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
            If RecordCount > 0 Then
                For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows
                    cboOwner.Items.Add(r("colFullName"))
                Next
            End If
        End Sub

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Combobox modification

    Adapted from https://stackoverflow.com/questions/...-combobox-item, but untested:

    Code:
    cboOwner.Items.Add(New DictionaryEntry(r("colFullName"), r("colID")) ' Your code doesn't seem to indicate the name of the ID column so I'm just guessing
    To retrieve the ID:

    Code:
    CType(cboOwner.SelectedItem,DictionaryEntry).Key
    Of course, you'll need to modify the existing SQL query to include the ID field so that it can be referenced using the code above.

    Edit to add: If you were doing traditional database interactions + binding, then this would all be moot and you would just assign the DataSource, DisplayMember, and ValueMember properties of the ComboBox appropriately.

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

    Re: Combobox modification

    Edit to add: If you were doing traditional database interactions + binding, then this would all be moot and you would just assign the DataSource, DisplayMember, and ValueMember properties of the ComboBox appropriately.
    This seems like the answer. Then you could access the ID using the "SelectedValue" property. Why isn't the cbo datasource not set to MasterBase.ListDataSet.Tables(0)??

  6. #6

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

    Re: Combobox modification

    I am not quite sure how to make that work, but on the assumption I can figure it out, it seems to me that this would display both of the values in the combobox. This is not what I want to do.

    What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.

    Currently, I am able to extract that value by running the routine below. However, I believe it would be a lot more efficient to just be able to grab that value at the same time I make a selection from combobox.

    While the method does work, colFullName column is not unique.

    Code:
        Private Sub txtComplete_Validating(sender As Object, e As CancelEventArgs) Handles txtComplete.Validating
            If (CInt(txtComplete.Text) < 0 Or CInt(txtComplete.Text) > 100) Then
                MsgBox("Please enter an integer from 0 to 100.", vbCritical)
                txtComplete.Text = ""
                txtComplete.Focus()
            End If
        End Sub

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Combobox modification

    What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.
    That's exactly what valuemember does. Lots of examples of how valuemember and selectedvalue work.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Combobox modification

    don't:
    Code:
        Public Sub GetOwnerComboBox()
            MyError = "Manager query failed."
            cboOwner.Items.Clear()
            MasterBase.AddParam("@active", True)
            MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
            If RecordCount > 0 Then
                For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows
                    cboOwner.Items.Add(r("colFullName"))
                Next
            End If
        End Sub
    Do:
    Code:
        Public Sub GetOwnerComboBox()
            MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
             cboOwner.DisplayMember = "colFullName"
             cboOner.ValueMember = "ID"
             cdoOWner.Datasource = MasterBase.ListDataSet.Tables(0)
        End Sub
    By the way, this is where your use of a single consolidated data access class lie this is goign to start to fail you ....

    Bottom line, set the ValueMember to the name of hte cl with the ID in it... set the DisplayMember to the name of the col you want to display, then set the dataSourcde to the datatable with the data in it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Combobox modification

    Quote Originally Posted by gwboolean View Post
    I am not quite sure how to make that work, but on the assumption I can figure it out, it seems to me that this would display both of the values in the combobox. This is not what I want to do.

    What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.

    Currently, I am able to extract that value by running the routine below. However, I believe it would be a lot more efficient to just be able to grab that value at the same time I make a selection from combobox.

    While the method does work, colFullName column is not unique.

    Code:
        Private Sub txtComplete_Validating(sender As Object, e As CancelEventArgs) Handles txtComplete.Validating
            If (CInt(txtComplete.Text) < 0 Or CInt(txtComplete.Text) > 100) Then
                MsgBox("Please enter an integer from 0 to 100.", vbCritical)
                txtComplete.Text = ""
                txtComplete.Focus()
            End If
        End Sub
    Did you try the code I posted? Or am I just wasting my time?

  10. #10

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

    Re: Combobox modification

    Did you try
    Yes, I did, and several variations. I could not make that work. Not to say it can't, I just couldn't do it.

    Bottom line, set the ValueMember
    Nice. That looks familiar... then I noticed it uses a dataset. I got no problem with using a dataset, although I haven't for a awhile.

    That's exactly what valuemember does.
    True. Thanks.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: [RESOLVED] Combobox modification

    It's too bad you chose to not post the code you tried and said what issues you were having with it. Good luck with your future endeavors.

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Combobox modification

    Nice. That looks familiar... then I noticed it uses a dataset. I got no problem with using a dataset, although I haven't for a awhile.
    Not sure why you say that. You program uses MasterBase.ListDataSet. Whether you use a datset datatable or a datatable that's not part of a dataset makes no difference.

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